AWS Serverless Cheat Sheet
AWS Lambda Basic
exports.handler = async (event) => {
return {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!')
};
};
API Gateway + Lambda
1. Create Lambda function
2. Create REST API using API Gateway
3. Add method to trigger Lambda
DynamoDB Access
const AWS = require('aws-sdk');
const dynamo = new AWS.DynamoDB.DocumentClient();
const params = {
TableName: 'Users',
Key: { id: '123' }
};
dynamo.get(params).promise().then(data => console.log(data));
Serverless Framework CLI
sls create --template aws-nodejs
sls deploy
sls invoke -f hello
Lambda + API Gateway
# Lambda handler in Python
def lambda_handler(event, context):
return {
"statusCode": 200,
"body": "Hello from Lambda"
}
EventBridge Rule
aws events put-rule --schedule-expression "rate(5 minutes)" --name MyRule
Step Functions Example
{
"Comment": "Simple Step Function",
"StartAt": "FirstState",
"States": {
"FirstState": {
"Type": "Pass",
"Result": "Hello",
"End": true
}
}
}