Skip to content

Instantly share code, notes, and snippets.

@jghaines
Last active January 21, 2020 21:17
Show Gist options
  • Save jghaines/5b5b2530bf21b0a1adab3f6683aa7af5 to your computer and use it in GitHub Desktop.
Save jghaines/5b5b2530bf21b0a1adab3f6683aa7af5 to your computer and use it in GitHub Desktop.
Makefile for smart sam build
INPUT_TEMPLATE_FILE := mytemplate.sam.yaml
ifndef PACKAGE_BUCKET
$(error PACKAGE_BUCKET must be defined (as environment variable))
endif
OUTPUT_TEMPLATE_FILE := __generated__.yaml
SHELL = /bin/bash
# Build dependency on the value of PACKAGE_BUCKET environment variable
# https://stackoverflow.com/a/59787919/358224
var_file := .package_bucket.txt
var_name := PACKAGE_BUCKET
is_var_updated = [[ ! -e $(var_file) ]] || [[ "$$(< $(var_file))" != "$($(var_name))" ]]
update_var_file = echo "$($(var_name))" > $(var_file)
$(shell $(is_var_updated) && $(update_var_file))
# targets that aren't really files
.PHONY: help package
help: ## Show this help.
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/:[^#]*##/:\t/'
.aws-sam/build/template.yaml: $(INPUT_TEMPLATE_FILE) code/requirements.txt $(wildcard code/*.py) ## sam-build target and dependencies
sam build \
--template-file $(INPUT_TEMPLATE_FILE) \
--manifest ./code/requirements.txt --debug
# SAM_CLI_TELEMETRY=0 => Turn off telemetry warnings https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-telemetry.html
$(OUTPUT_TEMPLATE_FILE): $(var_file) .aws-sam/build/template.yaml ## create and upload the SAM application-bundle
SAM_CLI_TELEMETRY=0 \
sam package \
--s3-bucket "$(PACKAGE_BUCKET)" \
--output-template-file "$(OUTPUT_TEMPLATE_FILE)" --debug
package: $(OUTPUT_TEMPLATE_FILE) ## standard target to be invoked: package the application
# no-op
@jghaines
Copy link
Author

To workaround aws/aws-sam-cli#805

@pwmcintyre
Copy link

make noob here, how do i use this?

@jghaines
Copy link
Author

Create a directory structure like:

./
+-- Makefile
+-- code
|   +-- __init__.py
|   +-- lambda_function.py
|   +-- requirements.txt
+-- mytemplate.sam.yaml

In the directory run:
PACKAGE_BUCKET=my-s3-package-bucket make package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment