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.
- Create an AWS account if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
- AWS CLI installed and configured
- Git Installed
- AWS Serverless Application Model ( AWS SAM) installed
- Python 3 installed
- AWS Lambda Powertools for Python
- Docker
-
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
-
Change directory to the pattern directory:
cd alb-lambda-rest-api-sam-py
-
Install dependencies:
pipenv install pipenv requirements > requirements.txt
-
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
-
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.
-
Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing.
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.
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
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
- Delete the stack
sam delete
- 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