Skip to content

Instantly share code, notes, and snippets.

@speshak
Created May 27, 2020 13:12
Show Gist options
  • Save speshak/66586144d26f1dfde2d68f7ba0fb29f6 to your computer and use it in GitHub Desktop.
Save speshak/66586144d26f1dfde2d68f7ba0fb29f6 to your computer and use it in GitHub Desktop.
AWS SAM Makefile
#!/bin/bash
components=$(cfn-flip -j template.yaml | \
jq -r '.Resources | with_entries(select(.value.Type == "AWS::Serverless::Function" or .value.Type == "AWS::Serverless::LayerVersion" )) | keys | .[]')
echo "COMPONENTS := \$(patsubst %,.aws-sam/build/%,$(echo "${components}" | tr '\n' ' '))"
echo ""
for com in ${components}
do
dir=$(cfn-flip -j template.yaml | \
jq -r ".Resources.$com.Properties | if .ContentUri then .ContentUri else .CodeUri end")
cat <<EOF
${com}_SOURCE := \$(wildcard ${dir}*)
${com}_SCSS := \$(shell find ${dir} -type f -name \*.scss)
${com}_CSS := \$(patsubst %.scss,%.css,\$(${com}_SCSS))
.aws-sam/build/${com}: \$(${com}_SOURCE) \$(${com}_CSS)
sam build ${com}
EOF
done
# vim:noexpandtab
ACCOUNTID=$(shell aws sts get-caller-identity --query Account --output text)
# Parameter loading
ENV=prod
STACK_PARAMETERS := $(shell cat stack-parameters/$(ENV) | tr '\n' ' ')
INPUT_TEMPLATE_FILE := template.yaml
BUILTCSS := $(patsubst %.scss,%.css,$(shell find . -type f -name \*.scss -not -path ./.aws-sam\*))
.PHONY: deploy
deploy: .aws-sam/build/template.yaml
sam deploy \
--s3-bucket sam-us-east-1-$(ACCOUNTID) \
--stack-name beer-engine-$(ENV) \
--region us-east-1 \
--parameter-overrides $(STACK_PARAMETERS) \
--capabilities CAPABILITY_IAM \
--tags Environment=$(ENV) Purpose=BeerEngine
-include deps.mk
%.css: %.scss
sass $< $@
.aws-sam/build/template.yaml: $(INPUT_TEMPLATE_FILE) $(COMPONENTS)
sam build --use-container
.PHONY: build
build: .aws-sam/build/template.yaml
.PHONY: test
test:
pipenv run pytest --capture=no
deps.mk: gen-deps.sh $(INPUT_TEMPLATE_FILE)
./gen-deps.sh > deps.mk
.PHONY: clean
clean:
-rm -rf deps.mk \
.aws-sam \
$(BUILTCSS) \
$(patsubst %,%.map,$(BUILTCSS))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment