Skip to content

Commit ac2f4f7

Browse files
committed
update provisioner
1 parent 45fd0a6 commit ac2f4f7

File tree

4 files changed

+70
-53
lines changed

4 files changed

+70
-53
lines changed

Makefile

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,11 @@
1-
run_env = ParameterKey=RunEnvironment,ParameterValue
2-
set_application_prefix = ParameterKey=ApplicationPrefix,ParameterValue
3-
set_application_name = ParameterKey=ApplicationFriendlyName,ParameterValue
4-
51
prod_aws_account = 298118738376
62
dev_aws_account = 427040638965
73
current_aws_account := $(shell aws sts get-caller-identity --query Account --output text)
84

95
src_directory_root = src/
106
dist_ui_directory_root = dist_ui/
117
integration_test_directory_root = tests/live_integration/
12-
13-
# CHANGE ME (as needed)
14-
application_key=infra-core-api
15-
application_name="InfraCoreApi"
16-
17-
region="us-east-1"
18-
19-
# DO NOT CHANGE
20-
common_params = --no-confirm-changeset \
21-
--no-fail-on-empty-changeset \
22-
--capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM CAPABILITY_AUTO_EXPAND \
23-
--region $(region) \
24-
--stack-name $(application_key) \
25-
--tags "project=$(application_key)" "techlead=$(techlead)" \
26-
--s3-prefix $(application_key) \
27-
--resolve-s3
28-
29-
s3_bucket_prefix = "$(current_aws_account)-$(region)-$(application_key)"
30-
ui_s3_bucket = "$(s3_bucket_prefix)-ui"
31-
docs_s3_bucket = "$(s3_bucket_prefix)-docs"
32-
33-
348
GIT_HASH := $(shell git rev-parse --short HEAD)
35-
ORIGIN_SECRET := $(shell openssl rand -hex 32)
369

3710
.PHONY: clean
3811

@@ -81,40 +54,15 @@ build: src/
8154
local:
8255
VITE_BUILD_HASH=$(GIT_HASH) yarn run dev
8356

84-
85-
postdeploy:
86-
@echo "Syncing S3 UI bucket..."
87-
aws s3 sync $(dist_ui_directory_root) s3://$(ui_s3_bucket)/ --delete
88-
aws s3 sync $(dist_ui_directory_root) s3://$(s3_bucket_prefix)/ --delete
89-
make invalidate_cloudfront
90-
9157
deploy_prod: check_account_prod
92-
@echo "Deploying CloudFormation stack..."
93-
@sam deploy $(common_params) --parameter-overrides $(run_env)=prod $(set_application_prefix)=$(application_key) $(set_application_name)="$(application_name)" S3BucketPrefix="$(s3_bucket_prefix)" CloudfrontOriginSecret="$(ORIGIN_SECRET)"
9458
@echo "Deploying Terraform..."
95-
$(eval MAIN_DISTRIBUTION_ID := $(shell aws cloudformation describe-stacks --stack-name $(application_key) --query "Stacks[0].Outputs[?OutputKey=='CloudfrontDistributionId'].OutputValue" --output text))
9659
terraform -chdir=terraform/envs/prod init -lockfile=readonly
97-
terraform -chdir=terraform/envs/prod apply -auto-approve -var main_cloudfront_distribution_id="$(MAIN_DISTRIBUTION_ID)"
98-
make postdeploy
60+
terraform -chdir=terraform/envs/prod apply -auto-approve
9961

10062
deploy_dev: check_account_dev
10163
@echo "Deploying Terraform..."
10264
terraform -chdir=terraform/envs/qa init -lockfile=readonly
10365
terraform -chdir=terraform/envs/qa apply -auto-approve
104-
make postdeploy
105-
106-
invalidate_cloudfront:
107-
@echo "Creating CloudFront invalidation..."
108-
$(eval DISTRIBUTION_ID := $(shell aws cloudformation describe-stacks --stack-name $(application_key) --query "Stacks[0].Outputs[?OutputKey=='CloudfrontDistributionId'].OutputValue" --output text))
109-
$(eval DISTRIBUTION_ID_2 := $(shell aws cloudformation describe-stacks --stack-name $(application_key) --query "Stacks[0].Outputs[?OutputKey=='CloudfrontIcalDistributionId'].OutputValue" --output text))
110-
$(eval INVALIDATION_ID := $(shell aws cloudfront create-invalidation --distribution-id $(DISTRIBUTION_ID) --paths "/*" --query 'Invalidation.Id' --output text --no-cli-page))
111-
$(eval INVALIDATION_ID_2 := $(shell aws cloudfront create-invalidation --distribution-id $(DISTRIBUTION_ID_2) --paths "/*" --query 'Invalidation.Id' --output text --no-cli-page))
112-
@echo "Triggered invalidation jobs $(INVALIDATION_ID) and $(INVALIDATION_ID_2)..."
113-
@echo "Waiting on job $(INVALIDATION_ID)..."
114-
aws cloudfront wait invalidation-completed --distribution-id $(DISTRIBUTION_ID) --id $(INVALIDATION_ID)
115-
@echo "Waiting on job $(INVALIDATION_ID_2)..."
116-
aws cloudfront wait invalidation-completed --distribution-id $(DISTRIBUTION_ID_2) --id $(INVALIDATION_ID_2)
117-
@echo "CloudFront invalidation completed!"
11866

11967
init_terraform:
12068
terraform -chdir=terraform/envs/qa init

terraform/envs/prod/.terraform.lock.hcl

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

terraform/envs/qa/.terraform.lock.hcl

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

terraform/modules/frontend/main.tf

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,37 @@ resource "aws_s3_bucket" "frontend" {
22
bucket = "${var.BucketPrefix}-${var.ProjectId}"
33
}
44

5+
data "archive_file" "ui" {
6+
type = "zip"
7+
source_dir = "${path.module}/../../../dist_ui/"
8+
output_path = "/tmp/ui_archive.zip"
9+
}
10+
resource "null_resource" "upload_frontend" {
11+
triggers = {
12+
ui_bucket_sha = data.archive_file.ui.output_sha
13+
}
14+
15+
provisioner "local-exec" {
16+
command = "aws s3 sync ${data.archive_file.ui.source_dir} s3://${aws_s3_bucket.frontend.id} --delete"
17+
}
18+
}
19+
20+
resource "null_resource" "invalidate_frontend" {
21+
triggers = {
22+
ui_bucket_sha = data.archive_file.ui.output_sha
23+
}
24+
25+
provisioner "local-exec" {
26+
command = <<-EOT
27+
set -e
28+
INVALIDATION_ID=$(aws cloudfront create-invalidation --distribution-id ${aws_cloudfront_distribution.app_cloudfront_distribution.id} --paths "/*" --query 'Invalidation.Id' --output text)
29+
aws cloudfront wait invalidation-completed --distribution-id ${aws_cloudfront_distribution.app_cloudfront_distribution.id} --id "$INVALIDATION_ID"
30+
EOT
31+
interpreter = ["bash", "-c"]
32+
}
33+
}
34+
35+
536
resource "aws_cloudfront_origin_access_control" "frontend_oac" {
637
origin_access_control_origin_type = "s3"
738
signing_behavior = "always"

0 commit comments

Comments
 (0)