Skip to content

Instantly share code, notes, and snippets.

@kbeckman
Last active August 6, 2021 14:22
Show Gist options
  • Save kbeckman/cc9667909a7585277642c25b4748771c to your computer and use it in GitHub Desktop.
Save kbeckman/cc9667909a7585277642c25b4748771c to your computer and use it in GitHub Desktop.
Deploy Lambda via CLI
ROOT_PATH=$(PWD)
BUILD_PATH=$(ROOT_PATH)/build
DIST_PATH=$(ROOT_PATH)/dist
SRC_PATH=$(ROOT_PATH)/lambda/src
PROJECT_NAME=some-lambda-function
STACK_NAME=test-${PROJECT_NAME}
TEST_LAMBDA_ZIP_FILE=test-${PROJECT_NAME}.zip
TEST_LAMBDA_ZIP_PATH=${DIST_PATH}/${TEST_LAMBDA_ZIP_FILE}
CF_STACK_NAME=test-${PROJECT_NAME}
CF_TEMPLATE_BODY="file://test-${PROJECT_NAME}.yml"
.PHONY: clean-build
clean-build:
rm -rf $(BUILD_PATH)
mkdir -p $(BUILD_PATH)
.PHONY: clean-dist
clean-dist:
rm -rf $(DIST_PATH)
mkdir -p $(DIST_PATH)
# Deploys Lambda function for testing purposes...
# Requires authenticated AWS session to push Lambda package and update CF stack.
.PHONY: deploy-test-lambda
deploy-test-lambda: clean-build clean-dist
pip install --upgrade --target ${BUILD_PATH} -r lambda/requirements.txt
cp -R ${SRC_PATH}/* ${BUILD_PATH}
cd ${BUILD_PATH}; zip -r ${TEST_LAMBDA_ZIP_PATH} ./
aws s3 cp ${TEST_LAMBDA_ZIP_PATH} s3://${PROJECT_NAME}-deploy/deploy/${TEST_LAMBDA_ZIP_FILE}
aws cloudformation create-stack --stack-name ${STACK_NAME} --template-body ${CF_TEMPLATE_BODY} \
--capabilities CAPABILITY_NAMED_IAM
.PHONY: teardown-test-lambda
teardown-test-lambda:
aws cloudformation delete-stack --stack-name ${STACK_NAME}
.PHONY: update-test-lambda
update-test-lambda:
cp -R ${SRC_PATH}/* ${BUILD_PATH}
cd ${BUILD_PATH}; zip -r ${TEST_LAMBDA_ZIP_PATH} ./
aws lambda update-function-code --function-name ${PROJECT_NAME} --zip-file fileb://${TEST_LAMBDA_ZIP_PATH}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment