This repository provides a simple demonstration of an AWS Lambda function built with Python, using API Gateway as the trigger and Lambda Layers to manage dependencies. This project uses the Serverless Framework to define and deploy the infrastructure as code.
- Serverless Architecture: Deploys a Python Lambda function without managing servers.
- API Gateway Integration: Exposes the Lambda function via a REST API endpoint.
- Lambda Layers: Efficiently bundles and manages Python dependencies, keeping the deployment package small and improving cold start times.
To deploy this project, ensure you have the following tools installed and configured:
- AWS CLI: Version
2.27.xor higher. - Python: Version
3.12.xor higher. - Node.js: Version
v23.1.xor higher. - npm: Version
11.5.xor higher. - Serverless Framework: Version
4.18.xor higher. - pip: Version
25.1.xor higher.
Follow these steps to set up the project locally and deploy it to your AWS account.
First, you need to configure your AWS environment.
-
Install the AWS CLI: If you haven't already, install the AWS CLI by following the official installation guide.
-
Configure AWS Credentials: Set up your credentials using
aws configure. This will allow the Serverless Framework to deploy resources to your account.aws configure
-
Update Configuration: Open
resources.ymland update the VPC security group and subnet IDs to match your AWS environment.- VPC Security Group IDs: Modify the values on line 94.
- Subnet IDs: Modify the values on lines 104-106.
-
Create an S3 Bucket: The Serverless Framework uses an S3 bucket to store deployment artifacts. Create one using the AWS CLI. Be sure to replace
<REGION>andsls-dev-demo-582305with your desired region and a unique bucket name.aws s3 mb s3://sls-dev-demo-582305 --region <REGION>
- Install Serverless Framework: Install the Serverless Framework globally via npm.
npm install -g serverless
- Set Up Python Environment: Create and activate a virtual environment to manage project dependencies. This project uses
pipenv.pipenv shell --python 3.12
- Install Development Dependencies: Install the Python packages needed for local development.
pip install -r src/requirements.txt
To keep the Lambda deployment package small, dependencies are packaged into a Lambda Layer.
- Package Dependencies: Install the required Python packages into the specific directory structure needed for AWS Lambda Layers.
mkdir -p packages/python/lib/python3.12/site-packages/ pip install -r src/requirements.txt --target=packages/python/lib/python3.12/site-packages/
Use the Serverless Framework to deploy the entire stack.
- Package and Verify: Run a dry-run to ensure the deployment package is correctly configured before deploying.
serverless package --stage dev --debug
- Deploy: Deploy the Lambda function, API Gateway, and Lambda Layer to your AWS account.
sls deploy --stage dev
To remove all resources and clean up your AWS account, run the following commands.
- Remove Serverless Stack:
sls remove --stage dev
- Delete S3 Bucket:
aws s3 rb s3://<BUCKET_NAME> --force
- Language: Python
- Framework: Serverless Framework
- Cloud Provider: Amazon Web Services (AWS)