Skip to content

Commit

Permalink
Use Chalice Terraform integration
Browse files Browse the repository at this point in the history
  • Loading branch information
kislyuk committed Sep 12, 2019
1 parent 9f8a1e5 commit 8f7b75a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 48 deletions.
45 changes: 21 additions & 24 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
SHELL=/bin/bash
SAM_TX="import sys, json, boto3, samtranslator.translator.transform as t, samtranslator.public.translator as pt; \
print(json.dumps(t.transform(json.load(sys.stdin), {}, pt.ManagedPolicyLoader(boto3.client('iam')))))"
GET_CREDS="import json, boto3.session as s; \
print(json.dumps(s.Session().get_credentials().get_frozen_credentials()._asdict()))"

export AWS_REGION=$(shell aws configure get region)
export AWS_ACCOUNT_ID=$(shell aws sts get-caller-identity | jq -r .Account)
export TF_S3_BUCKET=tfstate-$(AWS_ACCOUNT_ID)
SHELL=/bin/bash -o pipefail
export APP_NAME=$(shell jq -r .app_name .chalice/config.json)
export STAGE=dev
export TF_DATA_DIR=.terraform.$(STAGE)
export TFSTATE_FILE=$(TF_DATA_DIR)/remote.tfstate
export TF_CLI_ARGS_output=--state $(TFSTATE_FILE)
export TF_CLI_ARGS_init=--backend-config $(APP_HOME)/$(TF_DATA_DIR)/aws_config.json

deploy: init package
$(eval LAMBDA_MD5 = $(shell md5sum dist/deployment.zip | cut -f 1 -d ' '))
aws s3 cp dist/deployment.zip s3://$(TF_S3_BUCKET)/$(LAMBDA_MD5).zip
cat dist/sam.json | \
jq '.Resources.APIHandler.Properties.CodeUri="s3://$(TF_S3_BUCKET)/$(LAMBDA_MD5).zip"' | \
python -c $(SAM_TX) > dist/cloudformation.json
terraform apply
terraform state show aws_cloudformation_stack.lambda
# See https://github.com/terraform-providers/terraform-provider-aws/issues/1184
AWS_SDK_LOAD_CONFIG=1

package:
chalice package dist
deploy: init
echo "$$(jq .resource.aws_api_gateway_deployment.rest_api.lifecycle.create_before_destroy=true chalice.tf.json)" > chalice.tf.json
terraform apply

get-config:
@python -c $(GET_CREDS) | jq ".region=env.AWS_REGION | .bucket=env.TF_S3_BUCKET | .key=env.APP_NAME"
init: package
$(eval AWS_REGION = $(shell aws configure get region))
$(eval AWS_ACCOUNT_ID = $(shell aws sts get-caller-identity | jq -r .Account))
$(eval TF_S3_BUCKET = tfstate-$(AWS_ACCOUNT_ID))
-rm -f $(TF_DATA_DIR)/*.tfstate
mkdir -p $(TF_DATA_DIR)
jq -n ".region=env.AWS_REGION | .bucket=env.TF_S3_BUCKET | .key=env.APP_NAME+env.STAGE" > $(TF_DATA_DIR)/aws_config.json
terraform init

init:
-rm -f .terraform/terraform.tfstate
terraform init --backend-config <($(MAKE) get-config)
package:
chalice package --pkg-format terraform .

destroy: init
terraform destroy
Expand Down
2 changes: 2 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

app = Chalice(app_name='chalice-app-template')
app.debug = True if log_level == logging.DEBUG else False
for l in "botocore.vendored.requests.packages.urllib3.connectionpool", "requests.packages.urllib3.connectionpool":
logging.getLogger(l).setLevel(max(log_level, logging.WARNING))

logger = logging.getLogger(__name__)

Expand Down
24 changes: 0 additions & 24 deletions terraform-deploy.tf

This file was deleted.

0 comments on commit 8f7b75a

Please sign in to comment.