#!/bin/bash # Build AWS Lambda Layers for WhatsApp Handler set -e echo "🚀 Building AWS Lambda Layers..." # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Function to print colored output print_status() { echo -e "${GREEN}[INFO]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } # Check if Python 3.12 is available if ! command -v python3.12 &> /dev/null; then print_error "Python 3.12 is not installed" exit 1 fi print_status "Python version: $(python3.12 --version)" # Create base directories print_status "Creating layer directories..." mkdir -p layers/wp_lambda_layer/python/lib/python3.12/site-packages mkdir -p layers/wp_additional_layer/python/lib/python3.12/site-packages mkdir -p layers/phonepe_layer/python/lib/python3.12/site-packages # Install wp_lambda_layer dependencies print_status "Installing wp_lambda_layer dependencies..." pip3.12 install --no-cache-dir --target layers/wp_lambda_layer/python/lib/python3.12/site-packages \ requests==2.31.0 \ boto3==1.34.0 \ botocore==1.34.0 \ urllib3==1.26.18 \ certifi==2023.11.17 \ charset-normalizer==3.3.2 \ idna==3.6 \ jmespath==1.0.1 \ python-dateutil==2.8.2 \ s3transfer==0.10.0 \ six==1.16.0 # Install wp_additional_layer dependencies print_status "Installing wp_additional_layer dependencies..." pip3.12 install --no-cache-dir --target layers/wp_additional_layer/python/lib/python3.12/site-packages \ pg8000==1.30.4 \ scramp==1.4.4 \ asn1crypto==1.5.1 # Install PhonePe SDK print_status "Installing PhonePe SDK..." pip3.12 install --no-cache-dir --target layers/phonepe_layer/python/lib/python3.12/site-packages \ --index-url https://phonepe.mycloudrepo.io/public/repositories/phonepe-pg-sdk-python \ --extra-index-url https://pypi.org/simple \ phonepe_sdk==1.1.0 # Remove unnecessary files to reduce layer size print_status "Cleaning up unnecessary files..." find layers/ -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true find layers/ -type f -name "*.pyc" -delete 2>/dev/null || true find layers/ -type f -name "*.pyo" -delete 2>/dev/null || true find layers/ -type d -name "*.dist-info" -exec rm -rf {} + 2>/dev/null || true find layers/ -type d -name "*.egg-info" -exec rm -rf {} + 2>/dev/null || true find layers/ -type f -name "*.so" -exec strip {} + 2>/dev/null || true # Create zip files print_status "Creating layer zip files..." cd layers # Create wp_lambda_layer.zip print_status "Creating wp_lambda_layer.zip..." cd wp_lambda_layer zip -r ../wp_lambda_layer.zip . -x "*.pyc" "*/__pycache__/*" cd .. # Create wp_additional_layer.zip print_status "Creating wp_additional_layer.zip..." cd wp_additional_layer zip -r ../wp_additional_layer.zip . -x "*.pyc" "*/__pycache__/*" cd .. # Create phonepe_layer.zip print_status "Creating phonepe_layer.zip..." cd phonepe_layer zip -r ../phonepe_layer.zip . -x "*.pyc" "*/__pycache__/*" cd .. # Show file sizes print_status "Layer sizes:" du -sh wp_lambda_layer.zip wp_additional_layer.zip phonepe_layer.zip # Verify layer structure print_status "Verifying layer structure..." echo "wp_lambda_layer contents:" unzip -l wp_lambda_layer.zip | head -20 echo -e "\nwp_additional_layer contents:" unzip -l wp_additional_layer.zip | head -20 echo -e "\nphonepe_layer contents:" unzip -l phonepe_layer.zip | head -20 print_status "✅ All layers created successfully!" print_status "📦 Layer files:" ls -la *.zip print_status "🚀 Ready to upload to AWS Lambda!" echo -e "\n${GREEN}Next steps:${NC}" echo "1. Upload wp_lambda_layer.zip to AWS Lambda as a layer" echo "2. Upload wp_additional_layer.zip to AWS Lambda as a layer" echo "3. Upload phonepe_layer.zip to AWS Lambda as a layer" echo "4. Add all three layers to your Lambda function" echo "5. Set Python runtime to 3.12"