This directory contains terraform to build the main NRLF api infrastructure.
NRLF project uses terraform workspaces to handle deploying multiple NRLF stacks to each of our environments. NRFL stacks are identified by their TF workspace id. Resources in each stack will contain the workspace id in its name. (e.g. nhsd-nrlf--dev-pointers-table or nhsd-nrlf--469d5da6-pointers-table).
Each developer/QA can create their own ephemeral instance of the NRLF infrastructure. These are deployed as isolated NRLF stacks into the dev AWS account and use variables in etc/dev.tfvars.
This project has a number of "persistent environments", similar to traditional dev, ref and prod environments. Each of these environments will typically contain multiple NRLF stacks, allowing for blue/green style deployment, and have shared storage infrastructure like DynamoDB tables and S3 buckets. The persistent environments are deployed as follows:
| Environment | TF Workspace | TF Config | AWS Account | Internal Domain | Public Domain |
|---|---|---|---|---|---|
| internal-dev | dev-N | etc/dev.tfvars |
dev | record-locator.dev.national.nhs.uk |
internal-dev.api.service.nhs.uk |
| dev-sandbox | dev-sandbox-N | etc/dev.tfvars |
dev | record-locator.dev.national.nhs.uk |
internal-dev-sandbox.api.service.nhs.uk |
| internal-qa | qa-N | etc/qa.tfvars |
test | qa.record-locator.national.nhs.uk |
internal-qa.api.service.nhs.uk |
| qa-sandbox | qa-sandbox-N | etc/qa.tfvars |
test | qa.record-locator.national.nhs.uk |
internal-qa-sandbox.api.service.nhs.uk |
| int | int-N | etc/int.tfvars |
test | record-locator.int.national.nhs.uk |
int.api.service.nhs.uk |
| sandbox | int-sandbox-N | etc/int.tfvars |
test | record-locator.int.national.nhs.uk |
sandbox.api.service.nhs.uk |
| perftest | perftest-N | etc/perftest.tfvars |
test | perftest.record-locator.national.nhs.uk |
perftest.api.service.nhs.uk |
| ref | ref-N | etc/ref.tfvars |
test | record-locator.ref.national.nhs.uk |
ref.api.service.nhs.uk |
| prod | prod-N | etc/prod.tfvars |
prod | record-locator.national.nhs.uk |
api.service.nhs.uk |
The N in the TF workspace name repesents the stack id in that environment. So, for example, the internal-dev environment might have two stacks, dev-1 and dev-2 with TF workspace names matching their stack names. All resources for the dev-1 stack will be contained within the dev-1 TF workspace.
CI pipeline creates infrastructure in the dev AWS account. These will have workspace id of nrl<jira-id>-<first six char of commit hash> and use variables in etc/dev.tfvars
Before you begin deploying NRLF infrastructure, you will need:
- An NRLF-enabled AWS account, ideally
dev. See bootstrap for details on setting up a new account. - The required packages to build NRLF, see the Setup section in README.md.
- To be logged into the AWS mgmt account on the CLI that you are deploying from.
If infrastructure changes require account wide AWS resources. Please deploy the corresponding NRLF account wide infrastructure first.
Run the Persistent Environment Deploy github workflow to deploy infrastructure changes to your chosen environment. Else follow the steps below to deploy manually.
To deploy the infrastructure, you need to build the NRLF artifacts and then deploy them with Terraform.
The steps are as follows:
First, build the NRLF artifacts that will be deployed by Terraform:
$ make build-artifactsOn the first deployment, you will need to initialise and create your workspace. To create a new ephemeral dev workspace, run:
$ make initIf you want to use an existing workspace, or if you want to use the workspace of a persistent environment, do the following:
$ make ENV={ENV_NAME} TF_WORKSPACE_NAME={WORKSPACE_NAME} initreplacing {ENV_NAME} with the environment name (e.g. dev, qa, qa-sandbox etc) and {WORKSPACE_NAME} with the name of the workspace/stack you want to use.
So, for example, if you want to use the qa environment and deploy to the qa-1 stack, you'd do the following:
$ make ENV=qa TF_WORKSPACE_NAME=qa-1 initIf your Terraform provider config changes, you may need to reinitialise your workspace.
To create a Terraform plan for a dev workspace:
$ make planTo create a Terraform plan for a workspace in another environment:
$ make ENV={ENV_NAME} planreplacing {ENV_NAME} with the environment name (e.g. dev, qa, qa-sandbox etc).
To apply your changes to a dev workspace:
$ make applyTo apply your changes to a workspace in another environment:
$ make ENV={ENV_NAME} applyreplacing {ENV_NAME} with the environment name (e.g. dev, qa, qa-sandbox etc).
To tear down the infrastructure, you need to use Terraform to destroy the resources in your Terraform workspace.
First make build-artifacts. Then assume management and run make get-s3-perms ENV={ENV_NAME} in the project root.
To teardown the infrastructure, do the following:
$ make ENV={ENV_NAME} TF_WORKSPACE_NAME={WORKSPACE_NAME} init destroy
replacing {ENV_NAME} with the environment name (e.g. dev, qa, qa-sandbox etc) and {WORKSPACE_NAME} with the name of the workspace/stack you want to destroy.