AWS Lambda

1/13/2025

AWS Lambda

AWS Lambda lets you run code without provisioning or managing servers.

What is Lambda?

Lambda runs your code in response to events and automatically manages the compute resources.

Creating a Function

javascript
// index.js
exports.handler = async (event) => {
  console.log('Event:', JSON.stringify(event))
  
  return {
    statusCode: 200,
    body: JSON.stringify({
      message: 'Hello from Lambda!',
      input: event
    })
  }
}

Deploy with CLI

bash
# Create deployment package
zip function.zip index.js

# Create function
aws lambda create-function \
  --function-name my-function \
  --runtime nodejs18.x \
  --handler index.handler \
  --zip-file fileb://function.zip \
  --role arn:aws:iam::123456789:role/lambda-role

# Invoke function
aws lambda invoke \
  --function-name my-function \
  --payload '{"key": "value"}' \
  response.json

Event Sources

  • API Gateway: HTTP requests
  • S3: Object events
  • DynamoDB: Stream events
  • SQS: Queue messages
  • EventBridge: Scheduled events