|
| 1 | +#!/bin/bash |
| 2 | +# build-and-deploy.sh - Build and deploy Lambda function using virtualenv |
| 3 | + |
| 4 | +# Exit on any error |
| 5 | +set -e |
| 6 | + |
| 7 | +# Configuration |
| 8 | +FUNCTION_NAME="unicornstore-thread-dump-lambda" |
| 9 | +PACKAGE_NAME="lambda_function" |
| 10 | +PYTHON_VERSION="3.13" |
| 11 | +TEMP_DIR="build_temp" |
| 12 | +DIST_DIR="dist" |
| 13 | +OUTPUT_ZIP="$DIST_DIR/$PACKAGE_NAME.zip" |
| 14 | +REGION="${AWS_DEFAULT_REGION:-us-east-1}" |
| 15 | + |
| 16 | +# Colors for output |
| 17 | +RED='\033[0;31m' |
| 18 | +GREEN='\033[0;32m' |
| 19 | +YELLOW='\033[1;33m' |
| 20 | +BLUE='\033[0;34m' |
| 21 | +NC='\033[0m' # No Color |
| 22 | + |
| 23 | +# Print colored output |
| 24 | +print_status() { |
| 25 | + echo -e "${BLUE}=== $1 ===${NC}" |
| 26 | +} |
| 27 | + |
| 28 | +print_success() { |
| 29 | + echo -e "${GREEN}✅ $1${NC}" |
| 30 | +} |
| 31 | + |
| 32 | +print_warning() { |
| 33 | + echo -e "${YELLOW}⚠️ $1${NC}" |
| 34 | +} |
| 35 | + |
| 36 | +print_error() { |
| 37 | + echo -e "${RED}❌ $1${NC}" |
| 38 | +} |
| 39 | + |
| 40 | +# Check if AWS CLI is configured |
| 41 | +check_aws_config() { |
| 42 | + if ! aws sts get-caller-identity &>/dev/null; then |
| 43 | + print_error "AWS CLI is not configured or credentials are invalid" |
| 44 | + echo "Please run 'aws configure' or set AWS environment variables" |
| 45 | + exit 1 |
| 46 | + fi |
| 47 | + |
| 48 | + local account_id=$(aws sts get-caller-identity --query Account --output text) |
| 49 | + local current_region=$(aws configure get region || echo "not set") |
| 50 | + print_success "AWS configured - Account: $account_id, Region: $current_region" |
| 51 | +} |
| 52 | + |
| 53 | +# Check if Python 3.13 is available |
| 54 | +check_python() { |
| 55 | + if ! command -v python3 &> /dev/null; then |
| 56 | + print_error "Python 3 is not installed" |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | + |
| 60 | + local python_version=$(python3 --version) |
| 61 | + print_success "Python available: $python_version" |
| 62 | +} |
| 63 | + |
| 64 | +# Build the Lambda package |
| 65 | +build_package() { |
| 66 | + print_status "Building Lambda Deployment Package" |
| 67 | + echo "Function name: $FUNCTION_NAME" |
| 68 | + echo "Python version: $PYTHON_VERSION" |
| 69 | + echo "Target region: $REGION" |
| 70 | + |
| 71 | + # Create clean directories |
| 72 | + print_status "Creating clean build directories" |
| 73 | + rm -rf "$TEMP_DIR" "$DIST_DIR" |
| 74 | + mkdir -p "$TEMP_DIR/package" "$DIST_DIR" |
| 75 | + |
| 76 | + # Create and activate virtual environment |
| 77 | + print_status "Creating virtual environment" |
| 78 | + python3 -m venv "$TEMP_DIR/venv" |
| 79 | + source "$TEMP_DIR/venv/bin/activate" |
| 80 | + |
| 81 | + # Upgrade pip |
| 82 | + print_status "Upgrading pip" |
| 83 | + pip install --upgrade pip --quiet |
| 84 | + |
| 85 | + # Install dependencies if requirements.txt exists |
| 86 | + if [ -f "requirements.txt" ]; then |
| 87 | + print_status "Installing dependencies" |
| 88 | + pip install -r requirements.txt --target "$TEMP_DIR/package" --quiet |
| 89 | + print_success "Dependencies installed" |
| 90 | + else |
| 91 | + print_warning "No requirements.txt found, skipping dependency installation" |
| 92 | + fi |
| 93 | + |
| 94 | + # Copy function code |
| 95 | + print_status "Copying function code" |
| 96 | + if [ -d "src" ]; then |
| 97 | + cp src/*.py "$TEMP_DIR/package/" 2>/dev/null || true |
| 98 | + print_success "Source files copied" |
| 99 | + else |
| 100 | + print_warning "No src directory found" |
| 101 | + fi |
| 102 | + |
| 103 | + # Create deployment package |
| 104 | + print_status "Creating deployment package" |
| 105 | + cd "$TEMP_DIR/package" |
| 106 | + zip -r "../../$OUTPUT_ZIP" . > /dev/null |
| 107 | + cd - > /dev/null |
| 108 | + |
| 109 | + # Calculate package size |
| 110 | + local package_size=$(du -h "$OUTPUT_ZIP" | cut -f1) |
| 111 | + print_success "Package created: $OUTPUT_ZIP ($package_size)" |
| 112 | + |
| 113 | + # Clean up virtual environment |
| 114 | + deactivate |
| 115 | + rm -rf "$TEMP_DIR" |
| 116 | +} |
| 117 | + |
| 118 | +# Deploy to AWS Lambda |
| 119 | +deploy_function() { |
| 120 | + print_status "Deploying to AWS Lambda" |
| 121 | + |
| 122 | + # Check if function exists |
| 123 | + if aws lambda get-function --function-name "$FUNCTION_NAME" --region "$REGION" &>/dev/null; then |
| 124 | + print_status "Updating existing function code" |
| 125 | + aws lambda update-function-code \ |
| 126 | + --function-name "$FUNCTION_NAME" \ |
| 127 | + --zip-file "fileb://$OUTPUT_ZIP" \ |
| 128 | + --region "$REGION" \ |
| 129 | + --output table |
| 130 | + print_success "Function code updated successfully" |
| 131 | + else |
| 132 | + print_error "Function '$FUNCTION_NAME' does not exist" |
| 133 | + echo "Please deploy the CDK stack first to create the function" |
| 134 | + exit 1 |
| 135 | + fi |
| 136 | + |
| 137 | + # Wait for function to be updated |
| 138 | + print_status "Waiting for function update to complete" |
| 139 | + aws lambda wait function-updated \ |
| 140 | + --function-name "$FUNCTION_NAME" \ |
| 141 | + --region "$REGION" |
| 142 | + print_success "Function update completed" |
| 143 | + |
| 144 | + # Get function info |
| 145 | + print_status "Function Information" |
| 146 | + aws lambda get-function \ |
| 147 | + --function-name "$FUNCTION_NAME" \ |
| 148 | + --region "$REGION" \ |
| 149 | + --query '{FunctionName:Configuration.FunctionName,Runtime:Configuration.Runtime,Handler:Configuration.Handler,CodeSize:Configuration.CodeSize,LastModified:Configuration.LastModified}' \ |
| 150 | + --output table |
| 151 | +} |
| 152 | + |
| 153 | +# Test the deployed function |
| 154 | +test_function() { |
| 155 | + print_status "Testing deployed function" |
| 156 | + |
| 157 | + local test_payload='{"test": true, "message": "Hello from build script"}' |
| 158 | + |
| 159 | + aws lambda invoke \ |
| 160 | + --function-name "$FUNCTION_NAME" \ |
| 161 | + --region "$REGION" \ |
| 162 | + --payload "$test_payload" \ |
| 163 | + --cli-binary-format raw-in-base64-out \ |
| 164 | + response.json |
| 165 | + |
| 166 | + if [ -f "response.json" ]; then |
| 167 | + print_success "Function test completed" |
| 168 | + echo "Response:" |
| 169 | + cat response.json | python3 -m json.tool |
| 170 | + rm -f response.json |
| 171 | + fi |
| 172 | +} |
| 173 | + |
| 174 | +# Main execution |
| 175 | +main() { |
| 176 | + print_status "Lambda Build and Deploy Script" |
| 177 | + |
| 178 | + # Parse command line arguments |
| 179 | + BUILD_ONLY=false |
| 180 | + SKIP_TEST=false |
| 181 | + |
| 182 | + while [[ $# -gt 0 ]]; do |
| 183 | + case $1 in |
| 184 | + --build-only) |
| 185 | + BUILD_ONLY=true |
| 186 | + shift |
| 187 | + ;; |
| 188 | + --skip-test) |
| 189 | + SKIP_TEST=true |
| 190 | + shift |
| 191 | + ;; |
| 192 | + --region) |
| 193 | + REGION="$2" |
| 194 | + shift 2 |
| 195 | + ;; |
| 196 | + --help) |
| 197 | + echo "Usage: $0 [OPTIONS]" |
| 198 | + echo "Options:" |
| 199 | + echo " --build-only Only build the package, don't deploy" |
| 200 | + echo " --skip-test Skip function testing after deployment" |
| 201 | + echo " --region REGION Set AWS region (default: $REGION)" |
| 202 | + echo " --help Show this help message" |
| 203 | + exit 0 |
| 204 | + ;; |
| 205 | + *) |
| 206 | + print_error "Unknown option: $1" |
| 207 | + echo "Use --help for usage information" |
| 208 | + exit 1 |
| 209 | + ;; |
| 210 | + esac |
| 211 | + done |
| 212 | + |
| 213 | + # Run checks |
| 214 | + check_python |
| 215 | + |
| 216 | + if [ "$BUILD_ONLY" = false ]; then |
| 217 | + check_aws_config |
| 218 | + fi |
| 219 | + |
| 220 | + # Build package |
| 221 | + build_package |
| 222 | + |
| 223 | + if [ "$BUILD_ONLY" = false ]; then |
| 224 | + # Deploy and test |
| 225 | + deploy_function |
| 226 | + |
| 227 | + if [ "$SKIP_TEST" = false ]; then |
| 228 | + test_function |
| 229 | + fi |
| 230 | + fi |
| 231 | + |
| 232 | + print_success "Script completed successfully!" |
| 233 | +} |
| 234 | + |
| 235 | +# Run main function |
| 236 | +main "$@" |
0 commit comments