Create and attach an API Gateway resource policy to an API
To allow a user to access your API by calling the API execution service, you must create an API Gateway resource policy and attach the policy to the API. When you attach a policy to your API, it applies the permissions in the policy to the methods in the API. If you update the resource policy, you'll need to deploy the API.
Prerequisites
To update an API Gateway resource policy, you'll need the apigateway:UpdateRestApiPolicy
permission and the apigateway:PATCH
permission.
For an edge-optimized or Regional API, you can attach your resource policy to your API as you create it, or after it has been deployed. For a private API, you can't deploy your API without a resource policy. For more information, see Private REST APIs in API Gateway.
Attach a resource policy to an API Gateway API
The following procedure shows you how to attach a resource policy to an API Gateway API.
Troubleshoot your resource policy
The following troubleshooting guidance might help resolve issues with your resource policy.
My API returns {"Message":"User: anonymous is not authorized to perform: execute-api:Invoke on resource: arn:aws:execute-api:us-east-1:********/****/****/"}
In your resource policy, if you set the Principal to an AWS principal, such as the following:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", ""Principal": { "AWS": [ "arn:aws:iam::account-id:role/developer", "arn:aws:iam::account-id:role/Admin" ] }, "Action": "execute-api:Invoke", "Resource": [ "execute-api:/*" ] }, ... }
You must use AWS_IAM
authorization for every method in your API, or else your API returns the
previous error message. For more instructions on how to turn on AWS_IAM
authorization for a
method, see Methods for REST APIs in API Gateway.
My resource policy is not updating
If you update the resource policy after the API is created, you'll need to deploy the API to propagate the changes after you've attached the updated policy. Updating or saving the policy alone won't change the runtime behavior of the API. For more information about deploying your API, see Deploy REST APIs in API Gateway.
My resource policy returns the following error: Invalid policy document. Please check the policy syntax and ensure that Principals are valid.
To troubleshoot this error, we first recommend that you check the policy syntax. For more information, see Access policy language overview for Amazon API Gateway. We also recommend that you check that all the principals specified are valid and haven’t been deleted.
In addition, if your API is in an opt-in Region, verify that all accounts in the resource policy have the Region enabled.