Skip to content

AWS Lambda — Serverless Functions

AWS Lambda is a serverless compute service that lets you run code in response to events without provisioning or managing servers. You pay only for the compute time consumed — there is no charge when code is not running.

In Azure terms: AWS Lambda = Azure Functions

  1. You upload your function code (or write inline)
  2. You configure a trigger (event source)
  3. When the trigger fires, Lambda automatically:
    • Allocates compute resources
    • Runs your function
    • Scales automatically (from 1 to 1,000+ concurrent executions)
    • Reports metrics to CloudWatch
RuntimeVersions
Node.js18.x, 20.x
Python3.11, 3.12
Java11, 17, 21
.NET.NET 8
Ruby3.2
Goprovided.al2023 (custom runtime)
Custom RuntimeBring your own via Lambda Layers or container images
Trigger TypeExamples
APIAPI Gateway, Function URL (direct HTTPS)
StorageS3 object created/deleted, DynamoDB Streams
MessagingSQS, SNS, Kinesis, EventBridge
SchedulingEventBridge Scheduler (cron-like)
AuthCognito User Pools (custom auth flows)
OtherCloudFront (Lambda@Edge), ALB, IoT
SettingDescription
Memory128 MB to 10,240 MB (CPU scales proportionally)
Timeout1 second to 15 minutes (900 seconds)
ConcurrencyUp to 1,000 concurrent executions per region (soft limit)
Execution RoleIAM Role that grants the function permissions
Environment VariablesKey-value config passed to function at runtime
LayersShared dependencies/libraries across functions
VPCOptional — attach to a VPC to access private resources
MetricPrice
Requests$0.20 per 1 million requests
Duration$0.0000166667 per GB-second
Free tier1 million requests/month + 400,000 GB-seconds/month forever

Lambda’s free tier never expires — it’s not limited to the first 12 months.

import json
def lambda_handler(event, context):
# event: the trigger payload (dict)
# context: runtime info (function name, remaining time, etc.)
name = event.get('name', 'World')
return {
'statusCode': 200,
'body': json.dumps(f'Hello, {name}!')
}

A common pattern: API Gateway as the trigger makes Lambda an HTTP endpoint:

Client → API Gateway → Lambda → Response
Terminal window
# Create a Lambda function
aws lambda create-function \
--function-name my-function \
--runtime python3.12 \
--role arn:aws:iam::123456789012:role/lambda-role \
--handler lambda_function.lambda_handler \
--zip-file fileb://function.zip
# Invoke a Lambda function
aws lambda invoke \
--function-name my-function \
--payload '{"name": "AWS"}' \
response.json
# Update function code
aws lambda update-function-code \
--function-name my-function \
--zip-file fileb://function.zip
ScenarioLatencyDescription
Cold start100ms – 1s+Lambda spins up a new execution environment
Warm start< 10ms overheadReuses an existing execution environment

Tips to reduce cold starts:

  • Use Provisioned Concurrency (keeps environments warm, extra cost)
  • Keep deployment packages small
  • Use lighter runtimes (Python, Node.js) for latency-sensitive functions
  • Use Lambda SnapStart (Java only)
FeatureAWS LambdaAzure Functions
Max timeout15 minutes10 minutes (Consumption), unlimited (Dedicated/Flex)
Max memory10 GB1.5 GB (Consumption)
TriggersAPI GW, S3, SQS, SNS, EventBridge, DynamoDBHTTP, Blob, Queue, Service Bus, Event Hub, Timer
Hosting plansNo choice — always serverlessConsumption, Premium, Dedicated
Warm instancesProvisioned ConcurrencyPremium (pre-warmed)
LanguagesNode, Python, Java, .NET, Ruby, GoC#, JS, Python, Java, PowerShell, Go
  • Keep functions small and single-purpose
  • Store secrets in AWS Secrets Manager or SSM Parameter Store, not environment variables
  • Set appropriate timeouts — don’t leave at default 3 seconds for I/O-bound functions
  • Enable X-Ray tracing for distributed debugging
  • Use Dead Letter Queues (DLQ) for failed async invocations
  • Tag functions with environment and team for cost tracking