The create_lambda_package.sh script is responsible for creating the deployment package for the AWS Lambda function. This script automates the process of packaging the Python code and dependencies into a ZIP file that can be deployed to AWS Lambda.
scripts/create_lambda_package.sh
- Creates a ZIP file containing the Lambda function code and dependencies
- Calculates the SHA256 hash for Terraform's source code tracking
- Excludes unnecessary files to keep the package size minimal
- Ensures consistent packaging across different environments
# Navigate to Lambda source directory
cd src/lambda
# Create ZIP file with all contents
zip -r ../../terraform/lambda_function.zip . -x "*.pyc" "__pycache__/*" "*.DS_Store"The script calculates a SHA256 hash of the ZIP file for Terraform to track changes:
# macOS
SHA256_HASH=$(shasum -a 256 lambda_function.zip | cut -d' ' -f1 | base64)
# Linux
SHA256_HASH=$(sha256sum lambda_function.zip | cut -d' ' -f1 | base64)The resulting ZIP file contains:
lambda_function.zip
├── lambda_function.py # Main Lambda handler
└── requirements.txt # Python dependencies
The script is automatically called during deployment:
./scripts/deploy.shIf you need to create the package manually:
# From project root
./scripts/create_lambda_package.sh
# Or using Makefile
make packageThe script outputs:
- Package creation status
- SHA256 hash for Terraform
- Package size
- File location
Example output:
Creating Lambda deployment package...
Zipping Lambda function...
Lambda package created: terraform/lambda_function.zip
SHA256 Hash: MTM5ZjIwZTlkOWE0NWU4YjA4MTk1MDhiOTc3MDkyNGI1MWM3OTNkNDc2YjU4N2JlOWRmZWU0ZjFiZDQ0ZTkyYgo=
Package size: 4.0K
The Terraform configuration uses the generated package:
# In terraform/lambda.tf
locals {
lambda_zip_path = "${path.module}/lambda_function.zip"
}
resource "aws_lambda_function" "todo_api" {
filename = local.lambda_zip_path
source_code_hash = filebase64sha256(local.lambda_zip_path)
# ... other configuration
}-
Permission Denied
chmod +x scripts/create_lambda_package.sh
-
ZIP Command Not Found
- Ensure
zipis installed on your system - On macOS:
brew install zip - On Ubuntu:
sudo apt-get install zip
- Ensure
-
Hash Calculation Fails
- The script automatically detects macOS vs Linux
- Ensure
shasum(macOS) orsha256sum(Linux) is available
To verify the package was created correctly:
# Check if file exists
ls -la terraform/lambda_function.zip
# View contents
unzip -l terraform/lambda_function.zip
# Verify hash
shasum -a 256 terraform/lambda_function.zipTo include additional Python packages:
- Add to
src/lambda/requirements.txt - Install locally:
pip install -r src/lambda/requirements.txt -t src/lambda/ - Run the package script
To exclude additional file types, modify the zip command:
zip -r ../../terraform/lambda_function.zip . \
-x "*.pyc" "__pycache__/*" "*.DS_Store" "*.log" "*.tmp"- Keep it Minimal: Only include necessary files
- Version Control: Don't commit the ZIP file (it's in .gitignore)
- Consistent Environment: Use the same Python version as Lambda runtime
- Dependencies: Use
requirements.txtfor dependency management - Testing: Verify the package works before deployment
src/lambda/lambda_function.py- Main Lambda function codesrc/lambda/requirements.txt- Python dependenciesterraform/lambda.tf- Terraform Lambda configurationscripts/deploy.sh- Deployment script that calls this scriptMakefile- Contains package target