--- description: Deploy Rust Lambda function to AWS --- You are helping the user deploy their Rust Lambda function to AWS. ## Your Task Guide the user through deploying their Lambda function to AWS: 1. **Prerequisites check**: - Function is built: `cargo lambda build --release` completed - AWS credentials configured - IAM role for Lambda execution exists (or will be created) 2. **Verify AWS credentials**: ```bash aws sts get-caller-identity ``` If not configured: ```bash aws configure # Or use environment variables: # export AWS_ACCESS_KEY_ID=... # export AWS_SECRET_ACCESS_KEY=... # export AWS_REGION=us-east-1 ``` 3. **Basic deployment**: ```bash cargo lambda deploy ``` This will: - Use the function name from Cargo.toml (binary name) - Deploy to default AWS region - Create function if it doesn't exist - Update function if it exists 4. **Deployment with options**: **Specify function name**: ```bash cargo lambda deploy ``` **Specify region**: ```bash cargo lambda deploy --region us-west-2 ``` **Set IAM role**: ```bash cargo lambda deploy --iam-role arn:aws:iam::123456789012:role/lambda-execution-role ``` **Configure memory**: ```bash cargo lambda deploy --memory 512 ``` - Default: 128 MB - Range: 128 MB - 10,240 MB - More memory = more CPU (proportional) - Cost increases with memory **Set timeout**: ```bash cargo lambda deploy --timeout 30 ``` - Default: 3 seconds - Maximum: 900 seconds (15 minutes) **Environment variables**: ```bash cargo lambda deploy \ --env-var RUST_LOG=info \ --env-var DATABASE_URL=postgres://... \ --env-var API_KEY=secret ``` **Architecture** (must match build): ```bash # For ARM64 build cargo lambda deploy --arch arm64 # For x86_64 (default) cargo lambda deploy --arch x86_64 ``` 5. **Complete deployment example**: ```bash cargo lambda deploy my-function \ --iam-role arn:aws:iam::123456789012:role/lambda-exec \ --region us-east-1 \ --memory 512 \ --timeout 30 \ --arch arm64 \ --env-var RUST_LOG=info \ --env-var API_URL=https://api.example.com ``` ## IAM Role Setup If user doesn't have an IAM role, guide them: ### Option 1: Let cargo-lambda create it ```bash cargo lambda deploy --create-iam-role ``` This creates a basic execution role with CloudWatch Logs permissions. ### Option 2: Create manually with AWS CLI ```bash # Create trust policy cat > trust-policy.json <.zip` - Configure runtime: "Custom runtime on Amazon Linux 2023" - Set handler: "bootstrap" (not needed, but convention) - Configure memory, timeout, env vars in console ## Multi-Function Deployment For workspace with multiple functions: ```bash # Deploy all cargo lambda deploy --all # Deploy specific cargo lambda deploy --bin function1 cargo lambda deploy --bin function2 ``` ## Environment-Specific Deployment Suggest deployment patterns: **Development**: ```bash cargo lambda deploy my-function-dev \ --memory 256 \ --timeout 10 \ --env-var RUST_LOG=debug \ --env-var ENV=development ``` **Production**: ```bash cargo lambda deploy my-function \ --memory 1024 \ --timeout 30 \ --arch arm64 \ --env-var RUST_LOG=info \ --env-var ENV=production ``` ## Cost Optimization Tips 1. **Use ARM64**: 20% cheaper for same performance 2. **Right-size memory**: Test to find optimal memory/CPU 3. **Optimize timeout**: Don't set higher than needed 4. **Monitor invocations**: Use CloudWatch to track usage 5. **Consider reserved concurrency**: For predictable workloads ## Troubleshooting Deployment ### Issue: "AccessDenied" **Solution**: Check AWS credentials and IAM permissions ```bash aws sts get-caller-identity ``` ### Issue: "Function code too large" **Solution**: - Uncompressed: 250 MB limit - Compressed: 50 MB limit - Optimize binary size (see `/lambda-build`) ### Issue: "InvalidParameterValueException: IAM role not found" **Solution**: Create IAM role first or use --create-iam-role ### Issue: Function deployed but fails **Solution**: - Check CloudWatch Logs - Verify architecture matches build (arm64 vs x86_64) - Test locally first with `cargo lambda watch` ## Post-Deployment After successful deployment: 1. **Test the function**: ```bash cargo lambda invoke --remote --data-ascii '{"test": "data"}' ``` 2. **Monitor logs**: ```bash aws logs tail /aws/lambda/my-function --follow ``` 3. **Check metrics** in AWS CloudWatch 4. **Set up CI/CD**: Use `/lambda-github-actions` for automated deployment 5. **Configure triggers** (API Gateway, S3, SQS, etc.) via AWS Console or IaC Report deployment results including: - Function ARN - Region - Memory/timeout configuration - Invocation test results