This is a sample template for sam-dynamo - Below is a brief explanation of what we have generated for you:
.
├── Makefile <-- Make to automate build
├── README.md <-- This instructions file
├── nature-remo <-- Source code for a lambda function
│ ├── main.go <-- Lambda function code
│ └── main_test.go <-- Unit tests
└── template.yaml
- AWS CLI already configured with Administrator permission
- Docker installed
- Golang
In this example we use the built-in go get
and the only dependency we need is AWS Lambda Go SDK:
go mod download
Golang is a statically compiled language, meaning that in order to run it you have to build the executable target.
You can issue the following command in a shell to build it:
make build
Invoking function locally through local API Gateway
sam local start-api
aws s3 mb s3://BUCKET_NAME
Next, run the following command to package our Lambda function to S3:
sam package \
--output-template-file packaged.yaml \
--s3-bucket REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAME
Next, the following command will create a Cloudformation Stack and deploy your SAM resources.
sam deploy \
--template-file packaged.yaml \
--stack-name nature-remo-sam-dynamo \
--capabilities CAPABILITY_IAM
See Serverless Application Model (SAM) HOWTO Guide for more details in how to get started.
After deployment is complete you can run the following command to retrieve the API Gateway Endpoint URL:
aws cloudformation describe-stacks \
--stack-name nature-remo-sam-dynamo \
--query 'Stacks[].Outputs'
We use testing
package that is built-in in Golang and you can simply run the following command to run our tests:
go test -v ./nature-remo-sam-dynamo/
Please ensure Go 1.x (where 'x' is the latest version) is installed as per the instructions on the official golang website: https://golang.org/doc/install
A quickstart way would be to use Homebrew, chocolatey or your linux package manager.
Issue the following command from the terminal:
brew install golang
If it's already installed, run the following command to ensure it's the latest version:
brew update
brew upgrade golang
Issue the following command from the powershell:
choco install golang
If it's already installed, run the following command to ensure it's the latest version:
choco upgrade golang
AWS CLI commands to package, deploy and describe outputs defined within the cloudformation stack:
sam package \
--template-file template.yaml \
--output-template-file packaged.yaml \
--s3-bucket REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAME
sam deploy \
--template-file packaged.yaml \
--stack-name sam-dynamo \
--capabilities CAPABILITY_IAM \
--parameter-overrides MyParameterSample=MySampleValue
aws cloudformation describe-stacks \
--stack-name sam-dynamo --query 'Stacks[].Outputs'