- Newest
- Most votes
- Most comments
It seems you're facing a series of authorization issues while trying to set up an Amplify Function to be invoked from GraphQL. Let's break down the problem and address each step:
-
The initial 401 Unauthorized error suggests that your authentication wasn't set up correctly. You've taken the right step by adding your Cognito User to an ADMINS group and associating it with an IAM role.
-
The "Custom operation sayHello requires both an authorization rule and a handler reference" error was correctly addressed by adding the authorization rule to your query.
-
The "InvalidIdentityPoolConfigurationException" indicates that there might be an issue with the IAM roles associated with your Cognito Identity Pool. Make sure that the Identity Pool has both authenticated and unauthenticated roles properly configured.
-
The final "Not Authorized to access sayHello on type Query" error suggests that while your authentication is now working, the permissions are not correctly set up to allow access to the specific GraphQL operation.
To resolve this, you need to ensure that:
-
The IAM role associated with your Cognito User Group (ADMIN-ROLE) has the necessary permissions to invoke the Lambda function associated with your GraphQL operation.
-
The Lambda function itself has a resource-based policy that allows invocation from AppSync. You can add this permission using the AWS CLI or the Lambda console.
-
In your AppSync API, make sure that the data source (Lambda function) is correctly linked to the resolver for the 'sayHello' query.
-
Check that the authorization mode for your AppSync API is set to "Amazon Cognito User Pools" and that it's correctly linked to your User Pool.
-
In your schema, ensure that the 'sayHello' query has the correct authorization directive, for example:
type Query { sayHello(name: String!): String @auth(rules: [{ allow: groups, groups: ["ADMINS"] }]) } -
If you're using fine-grained access control, make sure your resolvers are set up to check for the correct group membership.
Remember, the principle of least privilege is important. Instead of using 'AdministratorAccess-Amplify', create a more specific IAM policy that only grants the necessary permissions for invoking the Lambda function and accessing AppSync.
By carefully reviewing and adjusting these settings, you should be able to resolve the authorization issues and successfully invoke your Amplify Function from GraphQL.
Sources
Configuring authorization and authentication to secure your GraphQL APIs - AWS AppSync
Granting function access to an organization - AWS Lambda
Grant access to other resources - AWS Amplify Gen 2 Documentation

This is not the entire answer. It helped me along a little bit, but not the whole way.
I'm not sure what was the thing that made it work for me, but I did three things:
Not sure what really helped or if it's the combination of all these