Skip to content

Commit 305ef80

Browse files
authored
VED-458 Terraform folder restructure for new INT account
1 parent 3761d09 commit 305ef80

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+512
-440
lines changed

azure/templates/post-deploy.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ steps:
4141
set -e
4242
if ! [[ $APIGEE_ENVIRONMENT =~ .*-*sandbox ]]; then
4343
export AWS_PROFILE=apim-dev
44-
aws_account_no="$(aws sts get-caller-identity --query Account --output text)"
45-
4644
service_name=$(FULLY_QUALIFIED_SERVICE_NAME)
4745
4846
pr_no=$(echo $service_name | { grep -oE '[0-9]+$' || true; })
@@ -58,10 +56,10 @@ steps:
5856
echo Apigee environment: $APIGEE_ENVIRONMENT
5957
echo pr_no: $pr_no
6058
61-
cd terraform
59+
cd terraform
6260
6361
make init
64-
make apply aws_account_no=${aws_account_no} environment=$workspace
62+
make apply environment=${{ parameters.aws_account_type }} sub_environment=$workspace
6563
6664
AWS_DOMAIN_NAME=$(make -s output name=service_domain_name)
6765
IMMS_DELTA_TABLE_NAME=$(make -s output name=imms_delta_table_name)

terraform/.terraform.lock.hcl

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

terraform/Makefile

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
11
-include .env
22

3-
interactionId=$(environment)
3+
environment ?= $(ENVIRONMENT)
4+
sub_environment ?= $(SUB_ENVIRONMENT)
5+
sub_environment_dir := $(if $(findstring pr-,$(sub_environment)),pr,$(sub_environment))
46

5-
aws_profile = apim-dev
6-
tf_cmd = AWS_PROFILE=$(aws_profile) terraform
7+
tf_cmd = AWS_PROFILE=$(AWS_PROFILE) terraform
78

8-
project_name = immunisation
9-
project_short_name = imms
10-
state_bucket = $(project_name)-$(APIGEE_ENVIRONMENT)-terraform-state-files
11-
tf_state= -backend-config="bucket=$(state_bucket)"
9+
bucket_name = $(if $(filter dev,$(environment)),immunisation-$(sub_environment),immunisation-$(environment))-terraform-state-files
1210

13-
tf_vars= -var="project_name=$(project_name)" -var="project_short_name=$(project_short_name)"
11+
tf_state = -backend-config="bucket=$(bucket_name)"
1412

15-
.PHONY : lock-provider workspace init plan apply clean destroy output state-list lambda-zip catch-all-zip
13+
tf_vars = \
14+
-var="sub_environment=$(sub_environment)" \
15+
-var-file="./environments/$(environment)/$(sub_environment_dir)/variables.tfvars"
1616

1717
lock-provider:
1818
# Run this only when you install a new terraform provider. This will generate sha code in lock file for all platform
1919
echo "This may take a while. Be patient!"
2020
$(tf_cmd) providers lock -platform=darwin_arm64 -platform=darwin_amd64 -platform=linux_amd64 -platform=windows_amd64
2121

2222
workspace:
23-
$(tf_cmd) workspace new $(environment) || $(tf_cmd) workspace select $(environment) && echo "Switched to workspace/environment: $(environment)"
23+
$(tf_cmd) workspace new $(sub_environment) || $(tf_cmd) workspace select $(sub_environment) && echo "Switched to workspace/environment: $(sub_environment)"
2424

2525
init:
2626
$(tf_cmd) init $(tf_state) -upgrade $(tf_vars)
2727

28+
init-reconfigure:
29+
$(tf_cmd) init $(tf_state) -upgrade $(tf_vars) -reconfigure
30+
2831
plan: workspace
2932
$(tf_cmd) plan $(tf_vars)
3033

@@ -40,7 +43,7 @@ clean:
4043
destroy: workspace
4144
$(tf_cmd) destroy $(tf_vars) -auto-approve
4245
$(tf_cmd) workspace select default
43-
$(tf_cmd) workspace delete $(environment)
46+
$(tf_cmd) workspace delete $(sub_environment)
4447

4548
output:
4649
$(tf_cmd) output -raw $(name)
@@ -59,3 +62,5 @@ catch-all-zip:
5962

6063
tf-%:
6164
$(tf_cmd) $*
65+
66+
.PHONY : lock-provider workspace init plan apply clean destroy output state-list lambda-zip catch-all-zip

terraform/README.md

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
1-
# immunisation-fhir-api Terraform
1+
# About
2+
The Terraform configuration in this folder is executed in each PR and sets up lambdas associated with the PR. Once the PR is merged, it will be used by the release pipeline to deploy to INT and REF. This is also run by the production release pipeline to deploy the lambdas to the prod blue and green sub environments.
23

3-
## Setup for local dev
4+
## Environments Structure
45

5-
Add your workspace name to the env file. This is usually your shortcode.
6+
Terraform is executed via a `Makefile`.
7+
The environment-specific configuration is structured as follows:
68

7-
```shell
8-
echo environment=your-shortcode >> .env
9-
make init
10-
make workspace
11-
make apply
9+
environments/
10+
└── <ENVIRONMENT>/ # e.g. dev, int, prod (AWS account name)
11+
└── <SUB_ENVIRONMENT_DIR> / # e.g. pr, internal-dev
12+
└── variables.tfvars
13+
14+
The `Makefile` automatically reads the `.env` file to determine the correct `variables.tfvars` file to use, allowing customization of infrastructure for each sub-environment.
15+
16+
## Run locally
17+
1. Create a `.env` file with the following values:
18+
```dotenv
19+
ENVIRONMENT=dev # Target AWS account (e.g., dev, int, prod)
20+
SUB_ENVIRONMENT=pr-123 # Sub-environment (e.g., pr-57, internal-dev)
21+
AWS_REGION=eu-west-2
22+
AWS_PROFILE=your-aws-profile
1223
```
24+
2. Run `make init` to download providers and dependencies
25+
3. Run `make plan` to output plan with the changes that terraform will perform
26+
4. **WARNING**: Run `make apply` only after thoroughly reviewing the plan as this might destroy or modify existing infrastructure
1327

14-
See the Makefile for other commands.
28+
Note: If you switch environment configuration in .env ensure that you run `make init-reconfigure` to reconfigure the backend to prevent migrating the existing state to the new backend.
1529

16-
If you want to apply Terraform to a workspace created by a PR you can set the above environment to the PR number.
30+
If you want to apply Terraform to a workspace created by a PR you can set the above SUB_ENVIRONMENT to the `PR-number` and ENVIRONMENT set to `dev`.
1731
E.g. `pr-57`. You can use this to test out changes when tests fail in CI.

terraform/ack_lambda.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ resource "aws_ecr_repository_policy" "ack_lambda_ECRImageRetreival_policy" {
6868
],
6969
"Condition" : {
7070
"StringLike" : {
71-
"aws:sourceArn" : "arn:aws:lambda:eu-west-2:${local.immunisation_account_id}:function:${local.short_prefix}-ack-lambda"
71+
"aws:sourceArn" : "arn:aws:lambda:eu-west-2:${var.immunisation_account_id}:function:${local.short_prefix}-ack-lambda"
7272
}
7373
}
7474
}
@@ -105,7 +105,7 @@ resource "aws_iam_policy" "ack_lambda_exec_policy" {
105105
"logs:CreateLogStream",
106106
"logs:PutLogEvents"
107107
]
108-
Resource = "arn:aws:logs:eu-west-2:${local.immunisation_account_id}:log-group:/aws/lambda/${local.short_prefix}-ack-lambda:*"
108+
Resource = "arn:aws:logs:eu-west-2:${var.immunisation_account_id}:log-group:/aws/lambda/${local.short_prefix}-ack-lambda:*"
109109
},
110110
{
111111
Effect = "Allow"
@@ -148,7 +148,7 @@ resource "aws_iam_policy" "ack_lambda_exec_policy" {
148148
"sqs:DeleteMessage",
149149
"sqs:GetQueueAttributes"
150150
],
151-
Resource = "arn:aws:sqs:eu-west-2:${local.immunisation_account_id}:${local.short_prefix}-ack-metadata-queue.fifo" },
151+
Resource = "arn:aws:sqs:eu-west-2:${var.immunisation_account_id}:${local.short_prefix}-ack-metadata-queue.fifo" },
152152
{
153153
"Effect" : "Allow",
154154
"Action" : [
@@ -216,7 +216,7 @@ resource "aws_lambda_function" "ack_processor_lambda" {
216216
variables = {
217217
ACK_BUCKET_NAME = aws_s3_bucket.batch_data_destination_bucket.bucket
218218
SPLUNK_FIREHOSE_NAME = module.splunk.firehose_stream_name
219-
ENVIRONMENT = terraform.workspace
219+
ENVIRONMENT = var.sub_environment
220220
AUDIT_TABLE_NAME = aws_dynamodb_table.audit-table.name
221221
FILE_NAME_PROC_LAMBDA_NAME = aws_lambda_function.file_processor_lambda.function_name
222222
}

terraform/api_gateway/acm_cert.tf

Lines changed: 0 additions & 31 deletions
This file was deleted.

terraform/api_gateway/api.tf

Lines changed: 0 additions & 66 deletions
This file was deleted.

terraform/api_gateway/variables.tf

Lines changed: 0 additions & 11 deletions
This file was deleted.

terraform/configs.tf

Lines changed: 0 additions & 6 deletions
This file was deleted.

terraform/delta.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ resource "aws_ecr_repository_policy" "delta_lambda_ECRImageRetreival_policy" {
6969
],
7070
"Condition" : {
7171
"StringLike" : {
72-
"aws:sourceArn" : "arn:aws:lambda:eu-west-2:${local.immunisation_account_id}:function:${local.short_prefix}-${local.function_name}"
72+
"aws:sourceArn" : "arn:aws:lambda:eu-west-2:${var.immunisation_account_id}:function:${local.short_prefix}-${local.function_name}"
7373
}
7474
}
7575
}

0 commit comments

Comments
 (0)