AWS Lambda: The Ultimate Serverless Function Cheatsheet

Introduction: What is AWS Lambda and Why It Matters

AWS Lambda is a serverless compute service that runs your code in response to events and automatically manages the underlying compute resources. With Lambda, you can run code without provisioning or managing servers, paying only for the compute time you consume.

Key Benefits:

  • Zero Server Management: No servers to provision, patch, or maintain
  • Continuous Scaling: Automatically scales with workload demands
  • Cost Efficiency: Pay only for compute time used (to the nearest millisecond)
  • Event-Driven: Executes code in response to various AWS service events
  • Flexible Integration: Works with virtually any application or backend service
  • Multiple Languages: Supports Node.js, Python, Java, Go, .NET, Ruby and custom runtimes

Core Lambda Concepts

Lambda Function Components

ComponentDescription
Code/FunctionThe code you want to execute
RuntimeThe language-specific environment that runs your code
HandlerThe method in your code that processes events
Event SourceThe AWS service or custom application that triggers your function
TriggerThe configuration that connects an event source to a Lambda function
Execution ContextThe environment where your function executes
LayersReusable components containing additional code or libraries
DestinationsWhere to send the results of asynchronous invocations

Lambda Execution Model

ConceptDescription
Cold StartInitial setup of a new execution environment (can cause latency)
Warm StartReuse of an existing execution environment (faster execution)
ConcurrencyNumber of function instances running simultaneously
Synchronous InvocationClient waits for the function to process and return a response
Asynchronous InvocationLambda queues the event and returns a response immediately
Event Source MappingLambda polls a source and invokes the function when records are detected

Lambda Limits and Quotas

ResourceDefault LimitNotes
Memory Allocation128 MB – 10,240 MBIn 1 MB increments
Ephemeral Storage512 MB – 10,240 MBIn 1 MB increments
Maximum Execution Time15 minutesMaximum timeout value
Deployment Package Size50 MB (zipped), 250 MB (unzipped)Including layers
Concurrent Executions1,000Soft limit, can be increased
Environment Variables4 KBTotal size for all environment variables
Function Resource-Based Policy20 KBMaximum size of policy document
Layers per Function5Maximum number of layers

Creating and Managing Lambda Functions

Step-by-Step: Creating a Basic Lambda Function

  1. Navigate to the Lambda console

    • Sign in to AWS Management Console
    • Go to Lambda service
  2. Create a new function

    • Click “Create function”
    • Choose “Author from scratch”, “Use a blueprint”, or “Container image”
  3. Configure basic settings

    • Enter function name
    • Select runtime (e.g., Python 3.9, Node.js 16.x)
    • Choose or create an execution role
  4. Write or upload function code

    • Use inline editor for simple functions
    • Upload a ZIP file for more complex functions
  5. Configure function settings

    • Set memory allocation (128 MB – 10,240 MB)
    • Set timeout value (up to 15 minutes)
    • Configure environment variables
  6. Set up triggers (optional)

    • Select event sources to trigger the function
  7. Configure destinations (optional)

    • Define where to send results of asynchronous invocations
  8. Save and test the function

    • Create a test event
    • Invoke the function to verify it works correctly

Lambda Function Handler Structures by Language

LanguageHandler FormatExample
Node.jsfile.functionindex.handler
Pythonfile.functionlambda_function.lambda_handler
Javapackage.class::methodcom.example.LambdaHandler::handleRequest
GoNot applicableBinary must implement handler function
.NETassembly::namespace.class::methodAssembly::Namespace.Class::Method
Rubyfile.functionlambda_function.lambda_handler

Sample Function Code by Language

Node.js (JavaScript)

exports.handler = async (event) => {
    console.log('Event: ', JSON.stringify(event, null, 2));
    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};

Python

import json

def lambda_handler(event, context):
    print('Event: ', json.dumps(event))
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

Java

package com.example;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;

public class LambdaHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
    @Override
    public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
        context.getLogger().log("Input: " + input);
        
        APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent();
        response.setStatusCode(200);
        response.setBody("Hello from Lambda!");
        return response;
    }
}

Go

package main

import (
    "context"
    "encoding/json"
    "fmt"
    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"
)

func handler(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
    fmt.Printf("Request: %+v\n", request)
    
    return events.APIGatewayProxyResponse{
        StatusCode: 200,
        Body:       "Hello from Lambda!",
    }, nil
}

func main() {
    lambda.Start(handler)
}

Lambda Integrations and Event Sources

Common Lambda Triggers

Trigger SourceDescriptionCommon Use Cases
API GatewayHTTP/REST API requestsWeb applications, microservices
S3 EventsObject created/deletedImage processing, file validation
DynamoDB StreamsTable updatesData replication, notifications
Kinesis Data StreamsReal-time data recordsAnalytics, monitoring, ETL
SQSMessage processingTask queues, workload distribution
SNSNotificationsEmail sending, alerts
EventBridgeScheduled eventsCron jobs, periodic tasks
CloudWatch LogsLog processingLog analysis, alerting
CognitoUser eventsCustom authentication flows
CodeCommitRepository eventsCI/CD workflows
IoTDevice dataIoT data processing
Alexa Skills KitVoice commandsVoice applications
CloudFormationStack eventsCustom resource provisioning

Event Source Mapping Types

Invocation TypeDescriptionKey Event Sources
Push-basedSource invokes function directlyAPI Gateway, S3, SNS, EventBridge
Poll-basedLambda polls for eventsSQS, DynamoDB Streams, Kinesis

Integration Configuration Examples

API Gateway Trigger

{
  "Type": "Api",
  "Properties": {
    "Path": "/items",
    "Method": "GET",
    "RestApiId": { "Ref": "MyApi" }
  }
}

S3 Event Trigger

{
  "Type": "S3",
  "Properties": {
    "Bucket": { "Ref": "MyBucket" },
    "Events": "s3:ObjectCreated:*",
    "Filter": {
      "S3Key": {
        "Rules": [
          { "Name": "prefix", "Value": "uploads/" },
          { "Name": "suffix", "Value": ".jpg" }
        ]
      }
    }
  }
}

EventBridge (CloudWatch Events) Schedule

{
  "Type": "Schedule",
  "Properties": {
    "Schedule": "rate(1 hour)",
    "Input": { "key": "value" }
  }
}

Lambda Deployment Strategies

Deployment Package Types

TypeDescriptionUse Cases
.zip archiveContains function code and dependenciesStandard deployments
Container imageDocker image containing function codeComplex dependencies, local testing
Lambda layersReusable code packagesShared libraries, frameworks

Deployment Tools and Methods

Tool/MethodDescriptionBest For
AWS Management ConsoleWeb interfaceLearning, simple functions
AWS CLI/SDKCommand-line toolsAutomation, CI/CD pipelines
AWS SAMServerless Application ModelServerless applications
AWS CloudFormationInfrastructure as codeComplex applications
TerraformThird-party IaC toolMulti-cloud environments
AWS CDKCloud Development KitTypeScript/Python/Java developers
Serverless FrameworkThird-party frameworkSimplified deployments

AWS Serverless Application Model (SAM) Example

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs14.x
      CodeUri: ./src/
      MemorySize: 256
      Timeout: 10
      Environment:
        Variables:
          TABLE_NAME: !Ref MyTable
      Policies:
        - DynamoDBCrudPolicy:
            TableName: !Ref MyTable
      Events:
        ApiEvent:
          Type: Api
          Properties:
            Path: /hello
            Method: get

  MyTable:
    Type: AWS::Serverless::SimpleTable
    Properties:
      PrimaryKey:
        Name: id
        Type: String

Lambda Performance and Optimization

Optimizing Cold Starts

StrategyDescription
Provisioned ConcurrencyPre-initialize execution environments
Smaller Deployment PackageReduce the size of your code and dependencies
Lower Memory LanguagesUse Node.js, Python instead of Java, .NET
Lambda SnapStart (Java)Save initialized state and reuse it
Reduced External DependenciesAvoid unnecessary network calls during initialization
Function WarmingPeriodically invoke functions to keep them warm

Memory and Performance Tuning

Memory (MB)Approx. CPU (vCPU)Use Case
128-2560.0625-0.125Simple API functions, small tasks
512-10240.25-0.5Web applications, basic processing
1024-17920.5-0.9Data processing, medium workloads
1792-35840.9-1.75CPU-intensive tasks
3585-102401.75-6Advanced computing, ML inference

Performance Testing and Monitoring

Tool/ServicePurpose
CloudWatch MetricsMonitor invocations, duration, errors
CloudWatch LogsAnalyze function logs
X-RayTrace requests through function and dependencies
Lambda InsightsAdvanced monitoring for performance
Amazon CodeGuruIdentify code inefficiencies
Lambda Power TuningOptimize memory/performance tradeoff

Common Challenges and Solutions

Error Handling and Retries

ChallengeSolution
Synchronous invocation failuresImplement try-catch blocks, use fallback logic
Asynchronous invocation failuresConfigure Dead Letter Queues (DLQ), use destinations
Event source mapping failuresConfigure maximum retry attempts, on-failure destinations
Idempotent functionsDesign functions to handle duplicate events safely

Network and VPC Integration

ChallengeSolution
VPC access latencyUse VPC endpoints for AWS services
Limited ENI capacityShare ENIs across functions with same VPC config
Internet access from VPCConfigure NAT Gateway or NAT instance
External service timeoutsAdjust function timeout, implement circuit breakers

Security Challenges

ChallengeSolution
Secret managementUse AWS Secrets Manager or Parameter Store
IAM permissionsFollow principle of least privilege
Code vulnerabilitiesScan dependencies, use Lambda layers
Input validationValidate all event data before processing

Lambda CLI Quick Reference

Function Management

# Create a function
aws lambda create-function \
  --function-name my-function \
  --runtime python3.9 \
  --zip-file fileb://function.zip \
  --handler lambda_function.lambda_handler \
  --role arn:aws:iam::123456789012:role/lambda-role

# Update function code
aws lambda update-function-code \
  --function-name my-function \
  --zip-file fileb://function.zip

# Update function configuration
aws lambda update-function-configuration \
  --function-name my-function \
  --timeout 30 \
  --memory-size 512 \
  --environment "Variables={KEY=VALUE}"

# List functions
aws lambda list-functions

# Get function details
aws lambda get-function --function-name my-function

# Delete function
aws lambda delete-function --function-name my-function

Invocation and Testing

# Invoke function synchronously
aws lambda invoke \
  --function-name my-function \
  --payload '{"key": "value"}' \
  output.txt

# Invoke function asynchronously
aws lambda invoke \
  --function-name my-function \
  --payload '{"key": "value"}' \
  --invocation-type Event \
  output.txt

# Get function logs
aws logs get-log-events \
  --log-group-name /aws/lambda/my-function \
  --log-stream-name 2023/05/14/[$LATEST]1234567890

Permissions and Triggers

# Add permission for S3 to invoke function
aws lambda add-permission \
  --function-name my-function \
  --statement-id s3-trigger \
  --action lambda:InvokeFunction \
  --principal s3.amazonaws.com \
  --source-arn arn:aws:s3:::my-bucket

# Create event source mapping (for poll-based services)
aws lambda create-event-source-mapping \
  --function-name my-function \
  --event-source-arn arn:aws:sqs:us-east-1:123456789012:my-queue \
  --batch-size 10

# List event source mappings
aws lambda list-event-source-mappings \
  --function-name my-function

Layers and Versions

# Publish a layer
aws lambda publish-layer-version \
  --layer-name my-layer \
  --zip-file fileb://layer.zip \
  --compatible-runtimes python3.8 python3.9

# Add layer to function
aws lambda update-function-configuration \
  --function-name my-function \
  --layers arn:aws:lambda:us-east-1:123456789012:layer:my-layer:1

# Publish a version
aws lambda publish-version \
  --function-name my-function \
  --description "Production version"

# Create an alias
aws lambda create-alias \
  --function-name my-function \
  --name production \
  --function-version 1

Advanced Lambda Patterns

Lambda Concurrency Types

TypeDescriptionUse Case
Reserved ConcurrencyLimit maximum concurrent invocationsProtect downstream resources
Provisioned ConcurrencyPre-initialize execution environmentsEliminate cold starts

Canary Deployments with Aliases

  1. Create function versions

    • Publish a version representing stable code
    • Update function code and publish new version
  2. Configure alias with routing

    aws lambda create-alias \
      --function-name my-function \
      --name production \
      --function-version 1 \
      --routing-config '{"AdditionalVersionWeights": {"2": 0.1}}'
    
  3. Gradually update weights

    • Increase traffic to new version over time
    • Monitor the performance and errors
    • Complete the deployment by setting alias to new version

Lambda State Machine Patterns

PatternImplementationUse Case
OrchestrationAWS Step Functions coordinate Lambda functionsMulti-step workflows
Fan-outSNS or EventBridge invoke multiple functionsParallel processing
Fan-inMultiple functions write to SQS, one processes resultsAggregation
Circuit breakerStep Functions catch errors and implement fallbackFault tolerance
SagaStep Functions handle compensating transactionsDistributed transactions

AWS Step Functions Integration Example

{
  "Comment": "A simple order processing workflow",
  "StartAt": "ProcessOrder",
  "States": {
    "ProcessOrder": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:us-east-1:123456789012:function:process-order",
      "Next": "CheckInventory"
    },
    "CheckInventory": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:us-east-1:123456789012:function:check-inventory",
      "Next": "InventoryChoice"
    },
    "InventoryChoice": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.inventoryExists",
          "BooleanEquals": true,
          "Next": "ProcessPayment"
        }
      ],
      "Default": "NotifyOutOfStock"
    },
    "ProcessPayment": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:us-east-1:123456789012:function:process-payment",
      "Next": "ShipOrder"
    },
    "ShipOrder": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:us-east-1:123456789012:function:ship-order",
      "End": true
    },
    "NotifyOutOfStock": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:us-east-1:123456789012:function:notify-out-of-stock",
      "End": true
    }
  }
}

Lambda Monitoring and Troubleshooting

CloudWatch Metrics to Monitor

MetricDescriptionAlarm Threshold
InvocationsNumber of times function is invokedDepends on expected traffic
ErrorsNumber of invocations resulting in errors> 0 for critical functions
DurationTime function spends processing an event> 1000ms or specific threshold
ThrottlesNumber of invocation requests throttled> 0 (indicates capacity issues)
ConcurrentExecutionsNumber of function instances executingNear reserved concurrency limit
IteratorAgeAge of last record for stream event sources> 1 hour (processing delay)
DeadLetterErrorsErrors when sending to DLQ> 0 (indicates DLQ issues)

Troubleshooting Common Lambda Issues

IssuePossible CausesSolutions
Function timeoutCode inefficiency, external service latencyIncrease timeout, optimize code, implement async patterns
Memory errorsInsufficient memory allocationIncrease memory setting, optimize memory usage
Permissions errorsIncorrect IAM role permissionsCheck CloudTrail for AccessDenied, add required permissions
Cold start latencyLarge deployment package, runtime initializationUse Provisioned Concurrency, optimize package size
ThrottlingExceeding account concurrency limitsRequest limit increase, implement backoff and retry
Invocation errorsCode bugs, invalid inputCheck logs, implement better error handling, validate input
Code deployment issuesPackage size limit, permissionsUse layers, optimize package, check deployment role

Interpreting Lambda Logs

START RequestId: 6bc28136-xmpl-4365-b021-0ce6b2e64ab0 Version: $LATEST
2020-06-22T15:05:07.409Z 6bc28136-xmpl-4365-b021-0ce6b2e64ab0 INFO  ENVIRONMENT VARIABLES:
{
    "AWS_LAMBDA_FUNCTION_VERSION": "$LATEST",
    "AWS_LAMBDA_LOG_GROUP_NAME": "/aws/lambda/my-function",
    "AWS_LAMBDA_LOG_STREAM_NAME": "2020/06/22/[$LATEST]fc11c7c43833457e9136f9b3cabd85ad",
    ...
}
REPORT RequestId: 6bc28136-xmpl-4365-b021-0ce6b2e64ab0
Duration: 1055.52 ms
Billed Duration: 1056 ms
Memory Size: 128 MB
Max Memory Used: 67 MB
Init Duration: 430.47 ms

Key Log Components:

  • START line: Beginning of invocation with RequestId
  • INFO/ERROR lines: Custom log output from your function
  • REPORT line: Performance metrics including:
    • Duration: Actual execution time
    • Billed Duration: Rounded up to nearest millisecond
    • Memory Size: Configured memory
    • Max Memory Used: Actual memory consumption
    • Init Duration: Time spent initializing (cold start)

Best Practices for Lambda Functions

Performance Best Practices

  • Initialize SDK clients and database connections outside handler function
  • Use global/static variables to cache state between invocations
  • Minimize deployment package size by removing unnecessary dependencies
  • Set function memory based on performance needs
  • Use appropriate asynchronous patterns for long-running tasks
  • Implement retries with exponential backoff for transient failures
  • Use Provisioned Concurrency for latency-sensitive applications

Security Best Practices

  • Follow least privilege principle when configuring IAM roles
  • Never store credentials in function code or environment variables
  • Use AWS Secrets Manager or Parameter Store for sensitive data
  • Validate all input data before processing
  • Implement function timeout appropriate to task
  • Enable AWS X-Ray for request tracing
  • Encrypt environment variables with KMS
  • Regularly update dependencies and runtime versions

Cost Optimization Best Practices

  • Set appropriate memory settings to minimize duration
  • Use Lambda Power Tuning to find optimal memory configuration
  • Combine similar functions to reduce number of invocations
  • Implement request batching when possible
  • Use websockets for real-time applications instead of polling
  • Choose efficient trigger patterns (filtering at the source)
  • Consider container-based alternatives for long-running processes

Development and Testing Best Practices

  • Use AWS SAM CLI for local testing
  • Implement unit and integration tests
  • Use layers for shared dependencies
  • Implement CI/CD pipelines for automated deployment
  • Version functions and use aliases for deployment stages
  • Use environment variables for stage-specific configuration
  • Implement structured logging with contextual information

Resources for Further Learning

Official AWS Resources

Community Resources

Books and Courses

  • “Serverless Applications with Node.js” by Slobodan Stojanović
  • “AWS Lambda in Action” by Danilo Poccia
  • A Cloud Guru’s AWS Lambda courses
Scroll to Top