Skip to content

Commit 34a1a0b

Browse files
committed
Updated readme, pipeline and makefile
1 parent 93dcb30 commit 34a1a0b

File tree

4 files changed

+40
-21
lines changed

4 files changed

+40
-21
lines changed

azure/templates/post-deploy.yml

Lines changed: 3 additions & 5 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,11 +56,11 @@ 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 plan aws_account_no=${aws_account_no} environment=$workspace
65-
# make apply aws_account_no=${aws_account_no} environment=$workspace
62+
make plan environment=${{ parameters.aws_account_type }} sub_environment=$workspace
63+
# make apply environment=${{ parameters.aws_account_type }} sub_environment=$workspace
6664
6765
AWS_DOMAIN_NAME=$(make -s output name=service_domain_name)
6866
IMMS_DELTA_TABLE_NAME=$(make -s output name=imms_delta_table_name)

terraform/Makefile

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
-include .env
22

3+
environment ?= $(ENVIRONMENT)
4+
sub_environment ?= $(SUB_ENVIRONMENT)
5+
sub_environment_dir := $(if $(findstring pr-,$(sub_environment)),pr,$(sub_environment))
6+
37
tf_cmd = AWS_PROFILE=$(AWS_PROFILE) terraform
4-
tf_state= -backend-config="bucket=$(STATE_BUCKET_NAME)"
58

6-
sub_env_folder := $(if $(findstring pr-,$(SUB_ENVIRONMENT)),pr,$(SUB_ENVIRONMENT))
7-
tf_vars= -var="sub_environment=$(SUB_ENVIRONMENT)" -var-file="./environments/$(ENVIRONMENT)/$(sub_env_folder)/variables.tfvars"
9+
bucket_name = $(if $(filter dev,$(environment)),immunisation-$(sub_environment),immunisation-$(environment))-terraform-state-files
10+
11+
tf_state = -backend-config="bucket=$(bucket_name)"
12+
13+
tf_vars = \
14+
-var="sub_environment=$(sub_environment)" \
15+
-var-file="./environments/$(environment)/$(sub_environment_dir)/variables.tfvars"
816

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

1422
workspace:
15-
$(tf_cmd) workspace new $(SUB_ENVIRONMENT) || $(tf_cmd) workspace select $(SUB_ENVIRONMENT) && echo "Switched to workspace/environment: $(SUB_ENVIRONMENT)"
23+
$(tf_cmd) workspace new $(sub_environment) || $(tf_cmd) workspace select $(sub_environment) && echo "Switched to workspace/environment: $(sub_environment)"
1624

1725
init:
1826
$(tf_cmd) init $(tf_state) -upgrade $(tf_vars)
@@ -35,7 +43,7 @@ clean:
3543
destroy: workspace
3644
$(tf_cmd) destroy $(tf_vars) -auto-approve
3745
$(tf_cmd) workspace select default
38-
$(tf_cmd) workspace delete $(SUB_ENVIRONMENT)
46+
$(tf_cmd) workspace delete $(sub_environment)
3947

4048
output:
4149
$(tf_cmd) output -raw $(name)

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 from this folder runs 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 prod 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 provisioners 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/main.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ terraform {
1818

1919
provider "aws" {
2020
region = var.aws_region
21-
#profile = "apim-dev"
2221
default_tags {
2322
tags = {
2423
Project = var.project_name

0 commit comments

Comments
 (0)