This template can be used as a boilerplate for RESTful APIs built with Go, AWS Lambda, and AWS SAM.
To be able to use this repo successfully you will need:
- Go 1.23+
- AWS SAM CLI
- Docker (for
sam buildandsam local) - An AWS account where the resources will be created.
- AWS IAM roles for CD via GitHub Actions. This template uses the strategy for using IAM roles during deployment that is outlined in this article. Make sure that you have ARNs for an "OIDC" role handy, since you'll need to set it as a repo secret during the set up process.
- A DataDog API Key.
- Start a new repo with this repo as the template.
- Clone this repo locally and switch to a new branch that will be used for repo set up.
- Update the
DescriptionandAppCodedefault intemplate.yamlto match your new service name. - Update the
stack_nameands3_prefixinsamconfig.tomlto match your new service name. - Update the
modulepath ingo.modto match your new repo (e.g.github.com/xplor/your-service-name).
You can use SAM CLI to run the API locally. You can get things up and running with these steps:
- Build the project
make build- Start the local API
make local- Check that the healthcheck endpoint is working correctly
curl -i http://localhost:3000/v1/healthcheckThis application comes with a number of GitHub Actions workflows that we recommend you use for CI/CD.
- Set the required repo secrets in GitHub for the IAM roles that are needed for deployment. The OIDC role should be set as
OIDC_ROLE_ARN. - Set the DataDog API Key as the repo secret
DD_API_KEY. - Move the GitHub actions to
.githubso that they will work as expected the next time you push up your code.
mv github-actions .github- Commit your changes and push your branch up.
- Open a PR with the set up changes.
- Verify that GitHub actions were run for testing and linting. We strongly recommend enforcing that these jobs pass before PRs can be merged to main.
- Once your tests are green, merge your branch into
main. - Verify that the Deploy action was kicked off, and that the application deploys successfully.
- Celebrate!!!! Your new service is ready for development! Go build cool things!
# run golangci-lint
make lint
# fix golangci-lint errors
make lint-fixLinting is done using golangci-lint. The configuration is in .golangci.yml.
To install golangci-lint locally:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latestYou can run tests with the following command:
make testTo generate a coverage report:
make test-coverageThis produces coverage.out and coverage.html in the project root.
- Create a new directory under
functions/with amain.go:
mkdir functions/my-function-
Add a new
AWS::Serverless::Functionresource intemplate.yamlpointing to the new directory. -
Run
make buildto verify the build succeeds.
template-go-sam-api/
├── functions/ # Lambda function source code
│ └── healthcheck/ # Each function in its own directory
│ ├── main.go # Handler entry point
│ └── main_test.go # Handler tests
├── github-actions/ # GitHub Actions (move to .github to activate)
│ ├── dependabot.yml
│ └── workflows/
│ ├── deploy-sam.yml # Deploy on merge to main
│ ├── lint.yml # Lint on PRs
│ └── test.yml # Test on PRs
├── .editorconfig # Editor settings
├── .gitignore
├── .golangci.yml # Linter configuration
├── go.mod # Go module definition
├── go.sum # Go dependency checksums
├── Makefile # Build, test, lint commands
├── README.md
├── samconfig.toml # SAM CLI configuration
└── template.yaml # SAM/CloudFormation template