-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
getAmplifyDataClientConfig() fails to check Amplify data variables when API name is defined #2320
Comments
Hello @lengieng, Thank you for the detailed notes on this issue. I am working on a fix and will post updates as I make progress. |
@lengieng, The fix for this was released with |
Not OP, I'm gettin the following error from 1.11.0
|
I just stepped through the docs guide with a newly create react app. I get the same error you're seeing when I attempt to use import { functionWithDataAccess } from '../functions/data-access/resource';
const schema = a.schema({
...
}).authorization((a) => [a.resource(functionWithDataAccess)]); If you've done this and you're still seeing the error, can you share your Thanks! |
Sure, though what I posted is most of that resouce.ts data/resource.ts
functions/listTables/resource.ts
|
@dlamon1 , Interesting. Your code from The function import { defineFunction } from '@aws-amplify/backend';
export const listTables = defineFunction({
name: 'listTables',
}); In If you are merging the function resource and handler definition into a single file like this, you'll need to separate them out. The When I merge these files like this, I get other import error |
You're very kind at calling out my obvious mistake, thank you, you're right.
|
*To clarify, this was the the resource.ts file I had already implement when receiving this error. |
The listTables.ts file
|
@dlamon1, Looking at the content for |
I'm having this issue for a Angular project, its a fresh project following the Create a user profile record data/resource.ts const schema = a.schema({
NLBUser: a.model({
id: a.string(),
...
profileOwner: a.string(),
...
}).authorization((allow) => [
allow.ownerDefinedIn("profileOwner")
]),
...
}).authorization((allow) => [allow.publicApiKey(), allow.resource(postConfirmation)]); auth/resource.ts import { defineAuth } from '@aws-amplify/backend';
import { postConfirmation } from './post-confirmation/resource';
/**
* Define and configure your auth resource
* @see https://docs.amplify.aws/gen2/build-a-backend/auth
*/
export const auth = defineAuth({
loginWith: {
email: true,
},
triggers: {
postConfirmation
}
}); auth/post-confirmation/resource.ts import { defineFunction } from '@aws-amplify/backend';
export const postConfirmation = defineFunction({
name: 'post-confirmation',
}); auth/post-confirmation/handler.ts import type { PostConfirmationTriggerHandler } from "aws-lambda";
import { type Schema } from "../../data/resource";
import { Amplify } from "aws-amplify";
import { generateClient } from "aws-amplify/data";
import { getAmplifyDataClientConfig } from '@aws-amplify/backend-function/runtime';
import { env } from "$amplify/env/post-confirmation";
const { resourceConfig, libraryOptions } = await getAmplifyDataClientConfig(
env
);
Amplify.configure(resourceConfig, libraryOptions);
const client = generateClient<Schema>();
export const handler: PostConfirmationTriggerHandler = async (event) => {
await client.models.NLBUser.create({
email: event.request.userAttributes.email,
profileOwner `${event.request.userAttributes.sub}::${event.userName}`,
});
return event;
}; On the statement Argument of type '{ invalidType: "Some of the AWS environment variables needed to configure Amplify are missing."; }' is not assignable to parameter of type 'ResourcesConfig | LegacyConfig | AmplifyOutputs'. On the statement const { resourceConfig, libraryOptions } = await getAmplifyDataClientConfig(
env
); Both
Where could I find those missing variables or how can i define them? Thanks! One solution I found while on sandbox mode was doing import outputs from '../amplify_outputs.json';
Amplify.configure(outputs); But when deployed it gives the error that Sorry If im making some obvious mistake, new around here :) |
Hi @jserrano-alq, Thank you for all the details. I think I've gone through the same process, starting an Angular app and setting up amplify with an auth trigger to create a profile record. I didn't encounter the error you're seeing, but I've capture my example app here. Locally, I'm using node v20.18.0 (npm v10.8.2), which versions are you building with? You're right that using I don't see any obvious mistakes or differences and I appreciate your help in working to track down the difference that is causing this error in your environment. |
ugh my whole morning was thrown away trying to troubleshoot this. i'm glad there was a patch but now i'm in the same place jseranno-alq is after the patch... i'll try to troubleshoot more tonight and will see if i can leverage claude. will post findings if something obvious comes out of it. |
in my case .env files are not being populated at all and therefore i get the above error previously reported |
Hello @dryhurst and @spyderbr6, I understand how frustrating it is when things don't work right. If you can provide either an example app repo that exhibits this problem or step by step instructions (including cli commands used and node version) to reproduce the problem, it would help accelerate our investigation. I've tried several approaches without running into the issue and will be investigating further today. Prior to releasing If your blocked on this issue and are looking for an workaround to get unblocked while we investigate, here are the docs for postConfirmation that use client.graphql.Run the command npx ampx generate graphql-client-code --out <path-to-post-confirmation-handler-dir>/graphql Then, create the corresponding handler file, import type { PostConfirmationTriggerHandler } from "aws-lambda";
import { type Schema } from "../../data/resource";
import { Amplify } from "aws-amplify";
import { generateClient } from "aws-amplify/data";
import { env } from "$amplify/env/post-confirmation";
import { createUserProfile } from "./graphql/mutations";
Amplify.configure(
{
API: {
GraphQL: {
endpoint: env.AMPLIFY_DATA_GRAPHQL_ENDPOINT,
region: env.AWS_REGION,
defaultAuthMode: "iam",
},
},
},
{
Auth: {
credentialsProvider: {
getCredentialsAndIdentityId: async () => ({
credentials: {
accessKeyId: env.AWS_ACCESS_KEY_ID,
secretAccessKey: env.AWS_SECRET_ACCESS_KEY,
sessionToken: env.AWS_SESSION_TOKEN,
},
}),
clearCredentialsAndIdentityId: () => {
/* noop */
},
},
},
}
);
const client = generateClient<Schema>({
authMode: "iam",
});
export const handler: PostConfirmationTriggerHandler = async (event) => {
await client.graphql({
query: createUserProfile,
variables: {
input: {
email: event.request.userAttributes.email,
profileOwner: `${event.request.userAttributes.sub}::${event.userName}`,
},
},
});
return event;
}; The full diff from the related docs update can be found here. |
Thank you so much @stocaaro ! I confirm this work around works just fine. |
i may have just needed to trigger a rebuild on my sandbox because (after the node packages were updated to this latest patch) vscode kept saying that error and so i just put logs in place in the handler before and after the retrieval to see what was there and miraculously it started working. thanks guys for the patch. |
there is something wonky with sandbox i think. i can't put my finger on it yet. at this point i have a stage branch that properly generates all the keys in .amplify/generated/env . i've branched from it and added a new function and now they all fail because amplify isnt generating any keys for anything in the new branch. |
i've been able to consistently reproduce this. while sandbox is running, i add an empty folder. it attempts to rebuild and starts failing everything. removing the folder yields an error but it appears to regenerate the files. Error If you don’t want to see a notice anymore, use "cdk acknowledge ". For example, "cdk acknowledge 31885". Resolution: Try removing '.amplify/artifacts' then running the command again. If it still doesn't work, see aws/aws-cdk#18459 for more methods. [Sandbox] Watching for file changes... |
i'm done mucking with it for the night. basically if anything is wrong with any function anywhere it kills all the generated environment files and nothing will work. basically replaces them with placeholders. makes it hard to troubleshoot because you cant get to a real error to see whats wrong with the function. |
ok. i'm closing out my engagement on this one. i think i'm in a good spot.
|
Glad you have a process that is working and I really appreciate your notes. If I'm reading correctly, the steps you're following are roughly in line with the "Resolution:" guidance from the sandbox error output you posted above. Do you have any recommendations on how we might improve upon this guidance? A couple questions for anyone encountering this,
|
Environment information
Describe the bug
The doc shows an example of how to use
defineAuth
anddefineFunction
to create Cognito post confirmation Lambda trigger. Another related section is grant Lambda function access to API.When name is given in the
defineData()
, the generatedenv
that start with theAMPLIFY_DATA
prefix are replaced with the given name.Without name given (default):
With name given:
In this case, in the
amplify/auth/post-confirmation/handler.ts
, this lineconst { resourceConfig, libraryOptions } = await getAmplifyDataClientConfig(env)
fails to generate theresourceConfig
because theisDataClientEnv(env)
used in thegetAmplifyDataClientConfig()
always returnsfalse
.Code below taken from here in the recent pull commit 2224.
Reproduction steps
Follow the example in the doc with an additional step to add the name to
defineData()
.defineData()
post-confirmation
resource fileamplify/auth/post-confirmation/resource.ts
:post-confirmation
handler fileamplify/auth/post-confirmation/handler.ts
:amplify/auth/resource.ts
The text was updated successfully, but these errors were encountered: