Control API access with your AWS SAM template (original) (raw)

Controlling access to your API Gateway APIs helps ensure your serverless application is secure and can only be accessed through the authorization you enable. You can enable authorization in your AWS SAM template to control who can access your API Gateway APIs.

AWS SAM supports several mechanisms for controlling access to your API Gateway APIs. The set of supported mechanisms differs between AWS::Serverless::HttpApi andAWS::Serverless::Api resource types.

The following table summarizes the mechanisms that each resource type supports.

Mechanisms for controlling access AWS::Serverless::HttpApi AWS::Serverless::Api
Lambda authorizers
IAM permissions
Amazon Cognito user pools ✓ *
API keys
Resource policies
OAuth 2.0/JWT authorizers

* You can use Amazon Cognito as a JSON Web Token (JWT) issuer with theAWS::Serverless::HttpApi resource type.

Choosing a mechanism to control access

The mechanism that you choose to use for controlling access to your API Gateway APIs depends on a few factors. For example, if you have a greenfield project without either authorization or access control set up, then Amazon Cognito user pools might be your best option. This is because when you set up user pools, you also automatically set up both authentication and access control.

However, if your application already has authentication set up, then using Lambda authorizers might be your best option. This is because you can call your existing authentication service and return a policy document based on the response. Also, if your application requires custom authentication or access control logic that user pools don't support, then Lambda authorizers might be your best option.

When you've chosen which mechanism to use, see the corresponding section in Examples for how to use AWS SAM to configure your application to use that mechanism.

Customizing error responses

You can use AWS SAM to customize the content of some API Gateway error responses. Only theAWS::Serverless::Api resource type supports customized API Gateway responses.

For more information about API Gateway responses, see Gateway responses in API Gateway in the API Gateway Developer Guide. For an example of customized responses, see Customized response example for AWS SAM.

Examples