Skip to content

Latest commit

 

History

History

SAM-and-ServerlessApplicationRepository

Resources for building serverless applications and pipelines

Description Repo/Link
AWS Serverless Application Model (SAM) awslabs/serverless-application-model
AWS Serverless Application Model (SAM) CLI awslabs/aws-sam-cli
AWS Serverless Application Model (SAM) Examples awslabs/serverless-application-model/examples/
AWS Serverless Application Repository AWS Serverless Application Repository
Cookiecutter SAM for Python Lambda functions aws-samples/cookiecutter-aws-sam-python

Notes on known sam-beta-cdk issues

  1. KeyError when running sam-beta-cdk ...
    KeyError: '/home/.../lambda'
    Failed to execute script __main__
    
    • Known bug: aws/aws-sam-cli#2849
    • Workaround:
      • Add "@aws-cdk/core:newStyleStackSynthesis": false into cdk.json
      • Add an empty requirements.txt to lambda/ (folder containing the Lambda functions).

SAM

Note: If docker volume mounting is required, need to use Windows cmd instead of WSL, otherwise the mounting will not work probably (without error).

Setup

# 1. Install Docker
# 2. Install Python 3.8
# 3. Install aws-sam-cli

pip install aws-sam-cli

sam init --runtime python3.8 --dependency-manager pip --name sam-app --output-dir . --app-template hello-world

cd sam-app/

Build and test locally

# To build on your workstation, run this command in folder containing
# SAM template. Built artifacts will be written to .aws-sam/build folder
sam build

sam local invoke --event events\event.json

sam local start-api
curl http://localhost:3000/

sam local start-lambda

# Run unit tests
pip install pytest pytest-mock --user
python -m pytest tests/ -v

Deploy

aws s3 mb s3://BUCKET_NAME

sam package --output-template-file packaged.yaml --s3-bucket BUCKET_NAME

sam deploy \
    --template-file packaged.yaml \
    --stack-name sam-app \
    --capabilities CAPABILITY_IAM

sam-app$ aws cloudformation describe-stacks \
    --stack-name sam-app \
    --query 'Stacks[].Outputs[?OutputKey==`HelloWorldApi`]' \
    --output table

sam logs -n HelloWorldFunction --stack-name sam-app --tail

Fetch, tail, and filter Lambda function logs

sam logs -n HelloWorldFunction --stack-name sam-app --tail

Cleanup

aws cloudformation delete-stack --stack-name sam-app
aws s3 rb s3://BUCKET_NAME