From 607d77a6f11364e441d747c81891ed67817128c3 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Sun, 9 Oct 2022 13:54:45 -0400 Subject: [PATCH] build: unified deployment script (#18) docs: links, script details --- README.md | 15 ++++++++++++--- script/deploy | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 script/deploy diff --git a/README.md b/README.md index d297e21..73b55a9 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,9 @@ AWS Lambdas are fun, but often the amount of boilerplate involved in getting a p ## Local development -The base Lambda handler is at `src/base.py` and all the Terraform configurations are in `infrastructure`. +The base Lambda handler is at `src/base.py` and all the Terraform configuration files are in `infrastructure`. -[Read more about Sceptre](https://sceptre.cloudreach.com/latest/index.html://www.terraform.io/docs/index.html) +[Read more about Terraform](https://www.terraform.io/docs/index.html) [Read more about AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/lambda-python.html) @@ -22,7 +22,16 @@ All](https://github.com/github/scripts-to-rule-them-all) paradigm and can be fou ## Deployment -Deployment is in three steps: on first setup, you will need to make sure that your `bootstrap` environment is ready via `PROJECT=bootstrap . script/apply`. Then, you should prepare your source code package (dependent on the language and framework you're using, you have to supply this bit!) and `ARCHIVE= . script/push`. Finally, you can deploy your application resources with `PROJECT=app . script/apply`. +Provided your runtime source code is prepackages and ready to go, you can simply `ARCHIVE= . script/deploy` to deploy your function in seconds! + +By default, the resources are identified with an environment name `dev-$USER`, but you can change this in staging / +production deployments by specifying the `ENV_NAME` environment variable. + +Individual parts of the application (i.e. the bootstrap resources or the application itself) can be deployed separately +using `PROJECT= script/apply`. + +Prepackaging the source code depends on what language is used as a runtime, more details can be found in [AWS Lambda +documentation](https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-package.html). ## Contributing diff --git a/script/deploy b/script/deploy new file mode 100644 index 0000000..fb23998 --- /dev/null +++ b/script/deploy @@ -0,0 +1,52 @@ +#!/usr/bin/bash -e + +# Deploys the whole project (bootstrap + application resources). This +# also takes care of pushing the lambda function's packed source code +# to S3, provided ARCHIVE is provided. +# +# Example usage: +# ARCHIVE=./lambda_function.zip . script/deploy + +source $(dirname $0)/../.config + +BOOTSTRAP_INFRA_ROOT=$(realpath $BOOTSTRAP_ROOT/infrastructure/bootstrap) +APPLICATION_INFRA_ROOT=$(realpath $BOOTSTRAP_ROOT/infrastructure/app) +DEFAULT_ENVNAME="dev-$USER" + +# Bootstrap resources deployment + +( + cd $BOOTSTRAP_INFRA_ROOT + terraform init + terraform apply --var-file $VARIABLES_PATH + + BUCKET_NAME=$(terraform output --json | jq .artifacts_bucket_name.value -r) + cd - + aws s3 cp $ARCHIVE s3://$BUCKET_NAME +) || exit $? + +# Application resources deployment + +( + cd $APPLICATION_INFRA_ROOT + terraform init + + # Some resources are always marked as tainted + # to force their recreation. + + declare -a ALWAYS_TAINT_RESOURCES=( + "aws_lambda_function.apgnd_lambda_func" + "aws_lambda_permission.apigw" + ) + + for RESOURCE in $ALWAYS_TAINT_RESOURCES + do + terraform taint --allow-missing $RESOURCE + done + + terraform apply \ + --var-file $VARIABLES_PATH \ + -var="env_name=${ENV_NAME:-$DEFAULT_ENVNAME}" \ + -var="commit_sha=$(git log --pretty=format:'%H' -n 1)" \ + -var="lambda_archive_name=$(basename $ARCHIVE)" +) || exit $?