Skip to content

Latest commit

 

History

History

apigw-http-api-lambda-ts-cdk

Amazon API Gateway HTTP API with AWS Lambda integration

This pattern in CDK offers a boilerlate to generate an Amazon API Gateway HTTP API endpoint with "ANY" method from the specified path. The Lambda function provided in TypeScript only returns a dummy "HelloWorld".

Amazon API Gateway has two ReSTful API products: REST APIs and HTTP APIs. REST APIs (v1) support more features than HTTP APIs, while HTTP APIs are designed with minimal features so that they can be offered at a lower price. For instance, calculator.aws shows that 10 millions of requests cost 10 USD/month (Region: US East) whereas REST APIs costs 35 USD/month.

Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/apigw-http-api-lambda-ts-cdk

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:
      git clone https://github.com/aws-samples/serverless-patterns
  2. Change directory to the pattern directory its source code folder:
      cd apigw-http-api-lambda-ts-cdk
  3. From the command line, use npm to install the development dependencies:
      npm install
  4. To deploy from the command line use the following:
      npm run deploy
  5. Optional If you do not use the default profile in your configuration for aws credentials, you must edit your scripts section in the package.json, replacing <YOUR_PROFILE_NAME> with your named profile:
      {
        "scripts": {
          "deploy": "cdk deploy --profile <YOUR_PROFILE_NAME>",
          "destroy": "cdk destroy --profile <YOUR_PROFILE_NAME>"
        }
      }

How it works

The solution works as follow:

  1. CDK will provision an Amazon API Gateway HTTP API and a sample Lambda function.
  2. It will integrate the Lambda function to the HTTP API using the route "/"
  3. Finally, it will return the HTTP API URL in the console. (So you can test the deployment)

Testing

  1. After deployment, the output shows the API Gateway URL with the Lambda integration, for example: HttpApiLambdaStack.HttpApiURL = https://<random id>.execute-api.<region>.amazonaws.com/.
  2. Accessing the URL in a browser, you see: Hello World.

Cleanup

  1. From the command line, use the following in the source folder
    npm run destroy
  2. Confirm the removal and wait for the resource deletion to complete.

Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: MIT-0