AWS Serverless
Lambda, DynamoDB and the pieces around them from the CLI: create and call functions, read logs, model and query a table.
Lambda: create and call
zip function.zip index.jsPackage the code (Lambda takes a zip)
aws lambda create-function --function-name hello --runtime nodejs20.x --handler index.handler --role ROLE_ARN --zip-file fileb://function.zipCreate a function
aws lambda invoke --function-name hello --payload '{"name":"Ann"}' --cli-binary-format raw-in-base64-out out.jsonCall it with an event and save the reply
aws lambda update-function-code --function-name hello --zip-file fileb://function.zipShip a new version of the code
aws lambda update-function-configuration --function-name hello --timeout 15 --memory-size 256Change timeout and memory (CPU grows with memory)
aws lambda list-functions --query "Functions[].FunctionName"List functions
aws lambda delete-function --function-name helloRemove a function
Lambda: handler shape
exports.handler = async (event) => ({ statusCode: 200, body: JSON.stringify({ ok: true }) });A minimal Node.js handler that returns an HTTP-style reply
def handler(event, context): return {"statusCode": 200, "body": "ok"}The same in Python
process.env.TABLE_NAMERead configuration from an environment variable (set with update-function-configuration --environment)
context.getRemainingTimeInMillis()How long is left before the timeout (Node.js)
Logs
aws logs tail /aws/lambda/hello --followWatch a function's log as it runs
aws logs tail /aws/lambda/hello --since 1h --filter-pattern ERROROnly the errors from the last hour
aws logs describe-log-groups --log-group-name-prefix /aws/lambda/List the log groups of your functions
console.log(JSON.stringify({ level: "info", msg: "saved", id }))Log JSON so it can be filtered and counted
DynamoDB: tables
aws dynamodb create-table --table-name Users --attribute-definitions AttributeName=id,AttributeType=S --key-schema AttributeName=id,KeyType=HASH --billing-mode PAY_PER_REQUESTCreate an on-demand table with a partition key
aws dynamodb describe-table --table-name UsersKey schema, status and size
aws dynamodb list-tablesList tables
aws dynamodb delete-table --table-name UsersDelete a table
DynamoDB: items
Values carry a type: S string, N number, BOOL, L list, M map.
aws dynamodb put-item --table-name Users --item '{"id":{"S":"u1"},"name":{"S":"Ann"}}'Write (or replace) an item
aws dynamodb get-item --table-name Users --key '{"id":{"S":"u1"}}'Read one item by key (fast and cheap)
aws dynamodb update-item --table-name Users --key '{"id":{"S":"u1"}}' --update-expression "SET age = :a" --expression-attribute-values '{":a":{"N":"31"}}'Change attributes without replacing the item
aws dynamodb query --table-name Orders --key-condition-expression "pk = :p" --expression-attribute-values '{":p":{"S":"u1"}}'Read many items that share a partition key
aws dynamodb scan --table-name Users --max-items 20Read the whole table (slow and costly: avoid on big tables)
aws dynamodb delete-item --table-name Users --key '{"id":{"S":"u1"}}'Delete an item
--condition-expression "attribute_not_exists(id)"Add to put-item: only create if it does not exist yet
Design reminders
Design tables around your queriesChoose keys for how you read the data; there are no joins
Least privilege for the function roleGive each Lambda only the actions and tables it needs
Idempotent handlersEvents can be delivered twice: make a repeat harmless
Cold startThe first call after idle is slower; keep packages small
Nothing matches that search.
Cheatsheet for the AWS Serverless path · Deboistech · All cheatsheets