Skip to content

Instantly share code, notes, and snippets.

@maddiesch
Last active November 19, 2020 06:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maddiesch/29d22ac28fa04bc8aac61b37a2a384e5 to your computer and use it in GitHub Desktop.
Save maddiesch/29d22ac28fa04bc8aac61b37a2a384e5 to your computer and use it in GitHub Desktop.
Basic Golang & SAM Makefile
# Configuration
SAM_ARTIFACT_BUCKET := <artifact-bucket-name>
SAM_STACK_NAME := <sam cloudformation stack name>
SAM_DEPLOY_CAPABILITIES := CAPABILITY_IAM
SAM_TEMPLATE_PARAMETERS :=
export AWS_PROFILE := default
ROOT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
.PHONY: build
build:
@echo "Place Build dependency & config here"
.PHONY: clean
clean: go-clean sam-clean
.PHONY: test
test: go-test
##
##
## Golang
##
##
# All the go files in the ROOT_DIR
GO_FILES := $(shell find ${ROOT_DIR} -type f -name '*.go' ! -name '*_test.go')
# All the go files
GOLANG ?= go
GO_LAMBDA_ENV ?= GOOS=linux GOARCH=amd64
GO_LAMBDA_BUILD_FLAGS ?= -ldflags="-s -w" -tags "lambda.norpc"
GO_LAMBDA_BUILD ?= ${GO_LAMBDA_ENV} ${GOLANG} build ${GO_LAMBDA_BUILD_FLAGS}
bin/%/bootstrap: ${GO_FILES}
cd ${ROOT_DIR}/cmd/$(patsubst bin/%/bootstrap,%,$@) && ${GO_LAMBDA_BUILD} -o ${ROOT_DIR}/$@ .
.PHONY: go-clean
go-clean:
rm -rf ./bin
${GOLANG} clean -testcache
${GOLANG} mod tidy
.PHONY: go-test
go-test:
${GOLANG} test -v ./...
##
##
## AWS-SAM
##
##
SAM_CLI ?= $(shell which sam)
AWS_CLI ?= $(shell which aws)
SAM_TEMPLATE_FILE ?= ${ROOT_DIR}/template.yml
SAM_PACKAGE_FILE ?= ${ROOT_DIR}/packaged.yml
.PHONY: sam-package
sam-package: build
${SAM_CLI} package \
--template ${SAM_TEMPLATE_FILE} \
--output-template-file ${SAM_PACKAGE_FILE} \
--s3-bucket ${SAM_ARTIFACT_BUCKET}
.PHONY: sam-deploy
sam-deploy: sam-package
${AWS_CLI} cloudformation deploy \
--template-file ${SAM_PACKAGE_FILE} \
--stack-name ${SAM_STACK_NAME} \
--capabilities ${SAM_DEPLOY_CAPABILITIES} \
--parameter-overrides ${SAM_TEMPLATE_PARAMETERS}
.PHONY: sam-clean
sam-clean:
rm -rf ${SAM_PACKAGE_FILE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment