Skip to main content

Lambda-backed API Gateway

A minimal AWS Python Pulumi program

This example lives in the pulumi/examples repository. Check out just this directory to use it:

Get started with this example
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examples
git -C pulumi-examples sparse-checkout set aws-py-apigateway-lambda-serverless
cd pulumi-examples/aws-py-apigateway-lambda-serverless

This example provides API endpoints which are executed by AWS Lambda using Python. The example sets up two Lambda-backed API Gateways: an API Gateway V1 (REST) and an API Gateway V2 (HTTP). AWS provides some information on the differences between these two API Gateway types: Announcing HTTP APIs for Amazon API Gateway and API Gateway V2 FAQs.

This sample uses the following AWS products:

Prerequisites#

  1. Install Pulumi
  2. Configure AWS credentials
  3. Install Python

Deploying the example#

  1. Create a new stack:

    Terminal window
    pulumi stack init dev
  2. Set the AWS region to deploy into:

    Terminal window
    pulumi config set aws:region us-east-2
  3. Install dependencies:

    Terminal window
    python3 -m venv venv
    source venv/bin/activate
    pip install -r requirements.txt
  4. Deploy the stack:

    Terminal window
    pulumi up
  5. To view the runtime logs of the Lambda function, use the pulumi logs command. To get a log stream, use pulumi logs --follow.

Test the endpoints#

Use a HTTP tool like curl or httpie (pip3 install httpie) to query the API Gateway endpoints using the Pulumi stack outputs.

Example using curl:

Terminal window
curl $(pulumi stack output apigateway-rest-endpoint)
curl $(pulumi stack output apigatewayv2-http-endpoint)

Example using httpie:

Terminal window
http $(pulumi stack output apigateway-rest-endpoint)
http $(pulumi stack output apigatewayv2-http-endpoint)

Output should include "Cheers from AWS Lambda!!".

Cleaning up#

Once you’re finished experimenting, you can destroy your stack and remove it to avoid incurring any additional cost:

Terminal window
pulumi destroy
pulumi stack rm

Summary#

In this tutorial, you built a lambda-backed API on AWS using API Gateway, lambda functions, and Pulumi. This serverless solution is highly scaleable, resilient, and stateless.

Next steps#

Related

The infrastructure as code platform for any cloud.