AWS Lambda Advanced Cheat Sheet

Last Updated: December 24, 2024

Lambda Function Configuration

Memory: 128MB - 10,240MB
Memory allocation (CPU scales proportionally)
Timeout: 1s - 900s (15 min)
Maximum execution time
Concurrent executions: 1000 (default)
Reserved concurrency per function
Environment variables: 4KB limit
Configuration key-value pairs

Advanced Handler Patterns

// Node.js async handler with error handling
exports.handler = async (event, context) => {
    const requestId = context.requestId;
    
    try {
        // Parse event
        const body = JSON.parse(event.body);
        
        // Business logic
        const result = await processData(body);
        
        // Return success response
        return {
            statusCode: 200,
            headers: {
                'Content-Type': 'application/json',
                'X-Request-Id': requestId
            },
            body: JSON.stringify({
                success: true,
                data: result
            })
        };
    } catch (error) {
        console.error('Error:', error);
        
        // Return error response
        return {
            statusCode: 500,
            headers: {'Content-Type': 'application/json'},
            body: JSON.stringify({
                success: false,
                error: error.message
            })
        };
    }
};

Lambda Layers

# Create layer directory structure
mkdir -p layer/nodejs/node_modules
cd layer/nodejs
npm install aws-sdk axios moment

# Create layer zip
cd ..
zip -r layer.zip .

# Publish layer
aws lambda publish-layer-version \
    --layer-name my-dependencies \
    --description "Common dependencies" \
    --zip-file fileb://layer.zip \
    --compatible-runtimes nodejs18.x nodejs20.x

# Attach to function
aws lambda update-function-configuration \
    --function-name my-function \
    --layers arn:aws:lambda:us-east-1:123456789:layer:my-dependencies:1

Performance Optimization

Provisioned Concurrency
Pre-warm instances for consistent performance
Connection reuse
Initialize connections outside handler
/tmp caching
Cache data between invocations (512MB limit)
Lambda Power Tuning
Find optimal memory configuration
💡 Pro Tip: Use environment variables for configuration, implement proper error handling, and monitor with CloudWatch Logs Insights for debugging.
← Back to DevOps & Cloud | Browse all categories | View all cheat sheets