AWS — Lambda
What is Lambda?
Serverless compute service that runs code without provisioning servers.
Create function
aws lambda create-function \
--function-name my-function \
--runtime python3.9 \
--handler lambda_function.lambda_handler \
--role arn:aws:iam::123456789:role/lambda-role \
--zip-file fileb://function.zip
Python handler
import json
def lambda_handler(event, context):
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
Invoke
aws lambda invoke \
--function-name my-function \
--payload '{"key": "value"}' \
response.json
Triggers
- API Gateway: HTTP requests
- S3: Object events
- DynamoDB: Stream events
- SQS: Message processing
- EventBridge: Scheduled events
Environment variables
aws lambda update-function-configuration \
--function-name my-function \
--environment "Variables={DB_HOST=mydb.xxx,API_KEY=abc123}"
Layers
aws lambda publish-layer-version \
--layer-name my-layer \
--zip-file fileb://layer.zip
aws lambda update-function-configuration \
--function-name my-function \
--layers arn:aws:lambda:us-east-1:123456789:layer:my-layer:1
Concurrency
# Reserved concurrency
aws lambda put-function-concurrency \
--function-name my-function \
--reserved-concurrent-executions 100
Best practices
- Keep functions small
- Use environment variables
- Handle errors gracefully
- Monitor with CloudWatch
Mini Practice
- Create a Lambda function
- Test with sample event
- Add an S3 trigger
- Monitor with CloudWatch
Up Next
Continue with VPC - Virtual Private Cloud.
Related Topics
Frequently Asked Questions about Lambda
What is Lambda in AWS?
Lambda is a fundamental concept in AWS. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Lambda?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Lambda.
Why is Lambda important in AWS?
Lambda is essential for AWS development. Understanding this concept will help you write better code and solve real-world problems more effectively.