Here’s a cheat sheet for AWS Lambda:
AWS Lambda Basics
- AWS Lambda is a serverless compute service that runs code in response to events.
- It automatically scales, and you only pay for the compute time consumed.
Lambda Functions
- A Lambda function is the unit of execution.
- Supports multiple languages: Node.js, Python, Java, Go, Ruby, .NET Core, etc.
Lambda Triggers
- Events that invoke Lambda functions.
- Examples: S3 events, API Gateway, CloudWatch Events, etc.
Lambda Execution Role
- Lambda functions assume an execution role to access other AWS services.
- Roles define permissions and policies.
Handler Function
- Entry point for Lambda function execution.
- Defined in the format:
filename.handler
.
Environment Variables
- Store configuration values for Lambda functions.
- Accessed using
process.env
in Node.js oros.environ
in Python.
Cold Starts
- The first time a Lambda function is invoked or after a long idle period.
- Can impact response time.
Lambda Layers
- Share code and dependencies between functions.
- Reduce the size of deployment packages.
Logging
- Use
console.log()
to log output. - View logs in AWS CloudWatch Logs.
Timeout
- Set the maximum execution time for a Lambda function (up to 15 minutes).
Memory Allocation
- Allocate memory to Lambda functions.
- Affects CPU and network performance.
Lambda@Edge
- Runs code in response to CloudFront events.
- Used for content delivery and global deployments.
Lambda CLI Commands
Create a new Lambda function:
aws lambda create-function --function-name MyFunction --runtime nodejs14.x --handler index.handler --role arn:aws:iam::account-id:role/execution_role --zip-file fileb://function.zip
Invoke a Lambda function:
aws lambda invoke --function-name MyFunction --payload '{"key1":"value1", "key2":"value2"}' output.txt
List Lambda functions:
aws lambda list-functions
Update Lambda function code:
aws lambda update-function-code --function-name MyFunction --zip-file fileb://function.zip
Set environment variables:
aws lambda update-function-configuration --function-name MyFunction --environment Variables={Key1=Value1,Key2=Value2}
Remember to replace placeholders like MyFunction
, index.handler
, account-id
, and function.zip
with your specific values.
This cheat sheet covers Lambda concepts and commands. For more detailed information, refer to the AWS Lambda Documentation.