SAM CLI

sam build, sam deploy and sam local invoke

sam build copies each function's code into .aws-sam/build/, running npm 2,036 install for Node.js 2,131 , and writes a template that points at the build output. sam deploy zips and uploads the artifacts to a bucket in the aws-sam-cli-managed-default stack (with resolve_s3), creates a change set and executes it; its settings live in sam/samconfig.toml (stack booknest-sam, CAPABILITY_IAM, no confirmation prompt). The SAM CLI 6,733 uses boto3, which honors the AWS_ENDPOINT_URL exported in awslocal, so both go to LocalStack 63,725 unchanged. sam local invoke then runs the built function in a Lambda 24 runtime container on your own Docker 514 , with a saved event:

Validating, building and deploying, seeding the table, then invoking locallyShell
cd ~/v5-ch7/booknest/sam
export SAM_CLI_TELEMETRY=0
sam validate --lint
sam build 2>&1 | grep -E 'Build Succeeded|Built Template'
sam deploy 2>/dev/null | grep '^Successfully'
jq -c '.books[] | {id: {N: (.id | tostring)}, title: {S: .title}, author: {S: .author},
  genre: {S: .genre}, price: {N: (.price | tostring)}}' ../db/seed.json |
  while read -r item; do aws dynamodb put-item --table-name booknest-sam-books --item "$item"; done
docker network create l1-sam >/dev/null && docker network connect l1-sam l1-localstack
sam local invoke BooksFunction --event events/get-book.json --env-vars env.json \
  --docker-network l1-sam 2>/dev/null | jq -r '.statusCode, .body'
Output
/home/dev/v5-ch7/booknest/sam/template.yaml is a valid SAM Template
Build Succeeded
Built Template   : .aws-sam/build/template.yaml
Successfully created/updated stack - booknest-sam in us-east-1
200
{"genre":"Cooking","id":3,"title":"Salt and Saffron","author":"Priya Nair","price":24}

Local runs need two overrides, in sam/env.json. SAM 24 resolves !Ref BooksTable to the logical ID BooksTable, not the table's name, so BOOKS_TABLE is set to booknest-sam-books; and the function's container needs a route to DynamoDB 24 , so DYNAMODB_ENDPOINT is http://l1-localstack:4566 on the l1-sam network that both containers join. Without the first override the function answered ResourceNotFoundException; without the network, ECONNREFUSED. The first local invoke also builds a public.ecr.aws/lambda/nodejs:24-rapid-x86_64 image (649 MB), which later runs reuse.