AWS SAM (serverless application model)
build package:
aws cloudformation package --template-file template.yaml --output-template-file output.yaml --s3-bucket my-bucket-name
deploy package:
aws cloudformation deploy --template-file output.yaml --stack-name StackName --capabilities CAPABILITY_IAM
Use aws sam local to test and debug locally.
- install docker
- install aws sam local:
npm install -g aws-sam-local
- install Go (v1.8+):
brew install go
- set GO Env variables (see below)
- run
go get github.com/awslabs/aws-sam-local
export GOROOT=/usr/local/opt/go/libexec
export GOPATH=$HOME/.go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Follow instructions: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html
To use the AWS CLI with the local dynamoDB, simply add --endpoint-url http://localhost:8000
to
each command
Example to create the table reviewTable:
aws dynamodb create-table --table-name reviewTable
--attribute-definitions AttributeName=id,AttributeType=S
--key-schema AttributeName=id,KeyType=HASH
--provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1
--endpoint-url http://localhost:8000
- validate the template yaml:
sam validate
- run API Gateway locally:
sam local start-api
- invoke a lambda function:
sam local invoke "FunctionName" -e event.json
Include environment variables:
- via external file:
sam local start-api --env-vars file_with_env_vars.json
- via shell:
TABLE_NAME=reviewTable sam local start-api
To run:
java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb
The DB must be running while testing functions that need to access it.
Acces the DynamoDB Javascript Shell on: http://localhost:8000/shell/
To correctly use defined environment variables use --env-vars argument on invoke or start-api to provide a JSON file that contains values for environment variables defined in your function.
Example set review table name:
{
"GetAllFunction": {
"TABLE_NAME": "reviewTable",
},
"GetFunction": {
"TABLE_NAME": "reviewTable",
},
"PutFunction": {
"TABLE_NAME": "reviewTable",
},
"DeleteFunction": {
"TABLE_NAME": "reviewTable",
},
}
Simple define the variable while calling sam
.
Example: TABLE_NAME=reviewTable sam local start-api
This way is simpler when the variable is used multiple times.