Amazon API Gateway with an API Key

Tram Ho

Create a REST API Gateway with an API Key associated with a Usage Plan.

An API Key is created and added to a Usage Plan (no throttling, no quota). This Usage Plan is then associated with the Prod stage of a REST API.
The GET method of this REST API is configured to require an API Key associated with the Usage Plan.
If the API key is provided, then API Gateway allows the request and sends it to the Lambda integration.
If the API is not provided, API returns a 403 Forbidden.

Amazon API Gateway REST API with API Key

The SAM template deploys an Amazon API Gateway REST API endpoint that uses an API Key.

An API Key is created and added to a Usage Plan (no throttling, no quota). This Usage Plan is then associated with the Prod stage of a REST API. The GET method of this REST API is configured to require an API Key associated with the Usage Plan. If the API key is provided, then API Gateway allows the request and sends it to the integration Lambda. If the API is not provided, API returns a 403 Forbidden.

Note: when deploying this pattern, CAPABILITY_IAM is required.

Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/apigw-api-key-sam

Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage – please see the AWS Pricing page for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.

Requirements

Deployment Instructions

  1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
  2. Change directory to the pattern directory:
  3. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:
  4. During the prompts:
    • Enter a stack name
    • Select the desired AWS Region
    • Allow SAM to create roles with the required permissions if needed.

    Once you have run guided mode once, you can use sam deploy in future to use these defaults.

  5. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing.

Testing

The stack will output the api endpoint. You can retrieve the API key contents from the API Gateway console. Make an HTTP request to the endpoint using curl including the API key in the headers to test the API Key REST API.

Cleanup

  1. Delete the stack
  2. Confirm the stack has been deleted
Share the news now

Source : Viblo