This is a sample template for service-type-api-backend - Below is a brief explanation of what we have generated for you:
.
├── Makefile
├── README.md
├── cmd
│ └── api
│ └── main.go
├── docs
│ └── Service Template - API Backend.postman_collection.json
├── go.mod
├── go.sum
├── internal
│ ├── constants
│ │ └── constants.go
│ ├── log
│ │ └── log.go
│ ├── ping
│ │ └── handler.go
│ ├── routes
│ │ └── routes.go
│ ├── session
│ │ └── aws_session.go
│ ├── todos
│ │ ├── handler.go
│ │ ├── model.go
│ │ ├── routes.go
│ │ └── service.go
│ └── utils
│ └── utils.go
├── local_env.json
├── samconfig.toml
└── template.yaml- AWS CLI already configured with Administrator permission
- Docker installed
- Golang
- SAM CLI - Install the SAM CLI
In this example we use the built-in sam build to automatically download all the dependencies and package our build target.
Read more about SAM Build here
The sam build command is wrapped inside of the Makefile. To execute this simply run
makeInvoking function locally through local API Gateway
make startIf the previous command ran successfully you should now be able to hit the following local endpoint to invoke your function http://localhost:8080/api/v1/ping
SAM CLI is used to emulate both Lambda and API Gateway locally and uses our template.yaml to understand how to bootstrap this environment (runtime, where the source code is, etc.) - The following excerpt is what the CLI will read in order to initialize an API and its routes:
...
Events:
CatchAll:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /{proxy+}
Method: anyAWS Lambda Golang runtime requires a flat folder with the executable generated on build step. SAM will use CodeUri property to know where to look up for the application:
...
ServiceTemplateFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: cmd/api/
...To deploy your application for the first time, run the following in your shell:
sam deploy --guidedThe command will package and deploy your application to AWS, with a series of prompts:
- Stack Name: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching your project name.
- AWS Region: The AWS region you want to deploy your app to.
- Confirm changes before deploy: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes.
- Allow SAM CLI IAM role creation: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modifies IAM roles, the
CAPABILITY_IAMvalue forcapabilitiesmust be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass--capabilities CAPABILITY_IAMto thesam deploycommand. - Save arguments to samconfig.toml: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run
sam deploywithout parameters to deploy changes to your application.
You can find your API Gateway Endpoint URL in the output values displayed after deployment.
We use testing package that is built-in in Golang and you can simply run the following command to run our tests:
make testPlease 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 golangIf it's already installed, run the following command to ensure it's the latest version:
brew update
brew upgrade golangIssue the following command from the powershell:
choco install golangIf it's already installed, run the following command to ensure it's the latest version:
choco upgrade golang# setup local dn
make db
# initialize table
aws dynamodb create-table --endpoint-url http://127.0.0.1:8000/ \
--table-name api-backend-template-dev \
--attribute-definitions \
AttributeName=pk,AttributeType=S \
AttributeName=sk,AttributeType=S \
AttributeName=date_time,AttributeType=S \
--key-schema \
AttributeName=pk,KeyType=HASH \
AttributeName=sk,KeyType=RANGE \
--local-secondary-indexes '
{
"IndexName": "DateTimeIndex",
"KeySchema": [
{"AttributeName": "pk", "KeyType": "HASH"},
{"AttributeName": "date_time", "KeyType": "RANGE"}
],
"Projection": {
"ProjectionType": "ALL"
}
}' \
--provisioned-throughput \
ReadCapacityUnits=10,WriteCapacityUnits=5