Skip to content

Latest commit

 

History

History

alb-lambda-rest-api-sam-py

AWS Lambda RESTful API with Amazon ALB and Path Based Listener Rules

This configuration pattern creates an Application Load Balancer with path-based listener rules, paired with an AWS Lambda RESTful API function as the target. It uses Python 3.9 and the Serverless Application Model (SAM) CLI.

Learn more about this pattern at Serverless Land Patterns.

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:

    cd alb-lambda-rest-api-sam-py
    
  3. Install dependencies:

    pipenv install
    pipenv requirements > requirements.txt
    
  4. From the command line, use AWS SAM to validate, build and deploy the AWS resources for the pattern as specified in the template.yml file:

    sam validate && sam build && sam deploy --guided
    
  5. During the prompts:

    • Enter a stack name
    • Enter the desired AWS Region
    • Enter VPC ID
    • Enter comma seperated Subnet IDs for e.g, subnet-1,subnet-2
    • Allow SAM CLI to create IAM roles with the required permissions.
  6. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing.

How it works

This setup orchestrates the deployment of an Application Load Balancer, configures path-based routes directing traffic to a Python-based AWS Lambda function, and leverages the AWS Lambda Powertools for Python library. The Lambda function, serving as the target, records details of the incoming ALB event, along with the API and context objects, logging them to an Amazon CloudWatch Logs log group and Amazon X-Ray.

Note: ALB has no authentication or authorization and should only be used for demo purposes.

Testing

SAM CLI for Local API Testing

The SAM CLI seamlessly extends the capabilities of the AWS CLI, introducing essential features for constructing and validating Lambda applications. Leveraging the power of Docker, it orchestrates the execution of functions within an Amazon Linux environment aligned with the Lambda runtime specifications, all sourced from sam/build-python3.9. This emulation replicates the Lambda environment.

To simulate an ALB (Application Load Balancer) event, the CLI relies on the configuration stored in event.json. This file corresponds to an ALB event, and it is generated by the SAM command: sam local generate-event alb request.

Now, to build and execute your function, execute the following command:

pipenv install
pipenv requirements > requirements.txt

sam build
sam local invoke -e events/event.json

Deploy and Test API in AWS

Once the application is deployed, retrieve the Application Load Balancer endpoint value from CloudFormation Outputs.

curl --request GET --header "Client-Correlation-Id:bb245" --url http://{ALB_ID}.{REGION}.elb.amazonaws.com/hello'

Alternatively, you can use xh utility, too as:

xh http://{ALB_ID}.{REGION}.elb.amazonaws.com/hello Client-Correlation-Id:bb245

Response:

HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 37
Content-Type: application/json
Server: awselb/2.0

{
    "message": "Hi from API behind ALB"
}

When an invalid resource (/nf-test) is used as:

curl --request GET --header "Client-Correlation-Id:not-found-test-1" --url http://{ALB_ID}.{REGION}.elb.amazonaws.com/nf-test'

OR

with xh utility as:

xh http://{ALB_ID}.{REGION}.elb.amazonaws.com/nf-test Client-Correlation-Id:'not-found-test-1'

Response:

HTTP/1.1 404 Not Found
Connection: keep-alive
Content-Length: 27
Content-Type: text/plain; charset=utf-8
Date: Fri, 12 Jan 2024 18:44:09 GMT
Server: awselb/2.0

404 Error!!! Page Not Found

Cleanup

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

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

SPDX-License-Identifier: MIT-0