kosa3 / nature-remo-sam-dynamo

Home Page:https://medium.com/@kosa3/sam%E3%81%A7nature-remo%E3%83%87%E3%83%BC%E3%82%BF%E3%82%92dynamodb%E3%81%B8put-get%E3%81%99%E3%82%8B-18d3d16b6d30

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

nature-remo-sam-dynamo

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

Requirements

Setup process

Installing dependencies

In this example we use the built-in go get and the only dependency we need is AWS Lambda Go SDK:

go mod download

Building

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

Local development

Invoking function locally through local API Gateway

sam local start-api

Packaging and deployment

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'

Testing

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/

Appendix

Golang installation

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.

Homebrew (Mac)

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

Chocolatey (Windows)

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

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'