Amazon Cognito to Amazon API Gateway private REST API

Amazon Cognito → API Gateway → VPC Link → AWS Fargate

Create an API Gateway private REST API with private integration.

This SAM template implements an Amazon API Gateway private REST API with private integration. It can be used if you have a private API inside a VPC which is currently open to unauthenticated clients and you want to protect it by adding an authentication and authorization layer without having to modify the API itself.
The template creates a private REST API in Amazon API Gateway which sits in front of the original backend API. Requests will go through the API Gateway endpoint and will be authorized using a Cognito authorizer. The integration with the backend resource is done via a VPC link to connect to private resources inside the VPC. The API Gateway API is configured with a greedy proxy ("{proxy+}") which means that everything in the URL path will be passed to the backend without any modification. The API has an "ANY" method to accept all methods such as GET or POST. Finally, the OPTIONS method is configured so that the API can process preflight requests from browsers making cross-origin requests (CORS).

< Back to all patterns

GitHub icon Download this pattern (.zip)

GitHub icon View this pattern on GitHub


Launch Stack

Clone repo

git clone https://github.com/aws-samples/serverless-patterns/cd serverless-patterns/cognito-restapi-vpclink

Deploy

sam deploy -g --capabilities CAPABILITY_AUTO_EXPAND CAPABILITY_IAM


Testing

See the repo for detailed testing notes.

Cleanup

1. Delete the stack: sam delete --stack-name STACK_NAME.
2. Confirm the stack has been deleted: aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus"

Created by:

Jose E. Montilla L.

Jose E. Montilla L.

Security Consultant at Amazon Web Services.

Follow on LinkedIn