cluster deboistech.in Get started
Cheatsheet

AWS Serverless

Lambda, DynamoDB and the pieces around them from the CLI: create and call functions, read logs, model and query a table.

Practice this path

Lambda: create and call

  • zip function.zip index.js

    Package 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.zip

    Create a function

  • aws lambda invoke --function-name hello --payload '{"name":"Ann"}' --cli-binary-format raw-in-base64-out out.json

    Call it with an event and save the reply

  • aws lambda update-function-code --function-name hello --zip-file fileb://function.zip

    Ship a new version of the code

  • aws lambda update-function-configuration --function-name hello --timeout 15 --memory-size 256

    Change timeout and memory (CPU grows with memory)

  • aws lambda list-functions --query "Functions[].FunctionName"

    List functions

  • aws lambda delete-function --function-name hello

    Remove 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_NAME

    Read 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 --follow

    Watch a function's log as it runs

  • aws logs tail /aws/lambda/hello --since 1h --filter-pattern ERROR

    Only 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_REQUEST

    Create an on-demand table with a partition key

  • aws dynamodb describe-table --table-name Users

    Key schema, status and size

  • aws dynamodb list-tables

    List tables

  • aws dynamodb delete-table --table-name Users

    Delete 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 20

    Read 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 queries

    Choose keys for how you read the data; there are no joins

  • Least privilege for the function role

    Give each Lambda only the actions and tables it needs

  • Idempotent handlers

    Events can be delivered twice: make a repeat harmless

  • Cold start

    The first call after idle is slower; keep packages small

Cheatsheet for the AWS Serverless path · Deboistech · All cheatsheets