build: unified deployment script (#18)

docs: links, script details
This commit is contained in:
Marc 2022-10-09 13:54:45 -04:00 committed by GitHub
parent b46a200226
commit 607d77a6f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 3 deletions

View file

@ -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=<path-to-zip> . 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=<path-to-function-zip> . 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=<bootstrap|app> 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

52
script/deploy Normal file
View file

@ -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 $?