Closed 0afcode closed 2 years ago
Additional comment: I have also confirmed that if I go and add a custom-roles.json with my function's role's name in the admin property, now the connection is successfully established. However this is a workaround, since per the article here ( https://docs.amplify.aws/cli/graphql/authorization-rules/#grant-lambda-function-access-to-graphql-api ), the function created through the CLI as part of an amplify project, should not have to go through this extra step to have it able to access graphql through IAM credentials.
Hey @0afcode :wave: thanks for raising this! This appears similar to https://github.com/aws-amplify/amplify-category-api/issues/679. If this is an existing GraphQL API that we are granting access to, can you try adding a small comment or space to your schema to trigger an UPDATE to the API? In the linked issue the problem lies with the VTL resolvers not being updated after granting access
Hey @0afcode 👋 thanks for raising this! This appears similar to aws-amplify/amplify-category-api#679. If this is an existing GraphQL API that we are granting access to, can you try adding a small comment or space to your schema to trigger an UPDATE to the API? In the linked issue the problem lies with the VTL resolvers not being updated after granting access
Hey @josefaidt that appears to be the bug. I created another function to test it with, added CRUD permissions for the api. Did an amplify push command just to see what changed. only the new function is listed as "create" at this point. Then I added a new line to the schema.graphql file in the api to trigger a modify and ran the amplify push command. Now the api is listed as 'update' while the new function listed as 'create'. Continued with the build, and tried out the same function code listed above, and this time it worked.
Additional comment: I have also confirmed that if I go and add a custom-roles.json with my function's role's name in the admin property, now the connection is successfully established. However this is a workaround, since per the article here ( https://docs.amplify.aws/cli/graphql/authorization-rules/#grant-lambda-function-access-to-graphql-api ), the function created through the CLI as part of an amplify project, should not have to go through this extra step to have it able to access graphql through IAM credentials.
Where did you put this file, and can you show me the structure?
I tried putting it in my api's directory like this, but it didn't help my case:
{
"adminRoleNames": ["arn:aws:sts::<My App ID>:assumed-role"]
}
Additional comment: I have also confirmed that if I go and add a custom-roles.json with my function's role's name in the admin property, now the connection is successfully established. However this is a workaround, since per the article here ( https://docs.amplify.aws/cli/graphql/authorization-rules/#grant-lambda-function-access-to-graphql-api ), the function created through the CLI as part of an amplify project, should not have to go through this extra step to have it able to access graphql through IAM credentials.
Where did you put this file, and can you show me the structure?
I tried putting it in my api's directory like this, but it didn't help my case:
{ "adminRoleNames": ["arn:aws:sts::<My App ID>:assumed-role"] }
Your parameter's value is incorrect. You need to provide the role's name (excluding the env) instead of the ARN.
The file is in < project >/amplify/backend/api/< ApiName >/custom-roles.json
example:
{ "adminRoleNames": ["devLambdaRole10ae332f"] }
Hey @0afcode I don't believe adding these function roles is required for this to work, however I'm curious if the generated VTL resolvers include the role in the auth resolvers? This should be handled for us by the CLI
Hey @0afcode :wave: just wanted to follow up on this issue and see if the VTL resolvers included the Lambda role?
Closing due to inactivity
@josefaidt this was caused by the api stack not triggering a rebuild/update after the graphql resources were allowed access to a function with amplify update function
so in other words, if we want to allow a lambda function created through amplify to have access to a graphql api also created from the same amplify stack, we need to trigger a change on the schema.graphql file in order for the amplify push script to detect an update with the api as well, and trigger a rebuild of the schema/resolvers. This solution was discussed in another thread, but I cannot seem to find it to link here anymore.
Before opening, please confirm:
How did you install the Amplify CLI?
npm install -g @aws-amplify/cli
If applicable, what version of Node.js are you using?
14.x
Amplify CLI Version
8.4.0
What operating system are you using?
ubuntu
Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.
no
Amplify Categories
api
Amplify Commands
Not applicable
Describe the bug
Lambda function created through the amplify-cli errors out when trying to query a graphql api that is part of the project. Roles have been confirmed to show access to the api. similar functions that were created in the same way before CLI version 8.4.0 operate normally without issue.
Expected behavior
Amplify function has the permission to perform queries on the graphql api.
Reproduction steps
GraphQL schema(s)
Log output
Additional information
I created a lambda function through the "amplify add function" command. I gave it all the permissions to CRUD on my existing api just as any other function I've created in the past. I verified that the function, once the backend got pushed and built on the cloud, had the correct role set up as all my other previous lambda functions performing similar actions.
I used the same test code I always do to confirm the function will work, and on this instance it keeps failing with an unauthorized error. The only thing I can think of is that my other functions were created pre cli version 8.3:
My schema for SourceSync model:
And the code in the lambda function in question. This lambda has a layer were the packages are being imported from. I've confirmed the layer is operational, because I have the older lambda functions in this project that are using this particular layer and they still work.