Skip to content

Commit 5cfe71d

Browse files
Merge pull request #1033 from NHSDigital/feature/made14-NRL-1591-prod-infra-fixes
[NRL-1591] Add prod backup infrastructure code
2 parents 715c0c6 + 831eeb1 commit 5cfe71d

File tree

11 files changed

+129
-21
lines changed

11 files changed

+129
-21
lines changed

terraform/account-wide-infrastructure/modules/athena/sql/rep_producer.sql

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ WITH
3333
, event_function_request_id
3434
, event_correlation_id
3535
, event_xray_trace_id
36-
, event_pointer_types
36+
--, event_pointer_types
3737
, COALESCE("event_headers_nhsd-end-user-organisation-ods", event_metadata_ods_code) user_ods
3838
FROM
3939
producer_deletedocumentreference
4040
)
41-
, pr AS (
41+
/*, pr AS (
4242
SELECT
4343
time
4444
, event_timestamp
@@ -56,8 +56,8 @@ WITH
5656
, COALESCE("event_headers_nhsd-end-user-organisation-ods", event_metadata_ods_code) user_ods
5757
FROM
5858
producer_readdocumentreference
59-
)
60-
, ps AS (
59+
)*/
60+
/*, ps AS (
6161
SELECT
6262
time
6363
, event_timestamp
@@ -75,7 +75,7 @@ WITH
7575
, COALESCE("event_headers_nhsd-end-user-organisation-ods", event_metadata_ods_code) user_ods
7676
FROM
7777
producer_searchdocumentreference
78-
)
78+
)*/
7979
, psp AS (
8080
SELECT
8181
time
@@ -90,12 +90,12 @@ WITH
9090
, event_function_request_id
9191
, event_correlation_id
9292
, event_xray_trace_id
93-
, event_pointer_types
93+
--, event_pointer_types
9494
, COALESCE("event_headers_nhsd-end-user-organisation-ods", event_metadata_ods_code) user_ods
9595
FROM
9696
producer_searchpostdocumentreference
9797
)
98-
, pu AS (
98+
/*, pu AS (
9999
SELECT
100100
time
101101
, event_timestamp
@@ -113,7 +113,7 @@ WITH
113113
, COALESCE("event_headers_nhsd-end-user-organisation-ods", event_metadata_ods_code) user_ods
114114
FROM
115115
producer_updatedocumentreference
116-
)
116+
)*/
117117
, pus AS (
118118
SELECT
119119
time
@@ -128,7 +128,7 @@ WITH
128128
, event_function_request_id
129129
, event_correlation_id
130130
, event_xray_trace_id
131-
, event_pointer_types
131+
--, event_pointer_types
132132
, COALESCE("event_headers_nhsd-end-user-organisation-ods", event_metadata_ods_code) user_ods
133133
FROM
134134
producer_upsertdocumentreference
@@ -140,18 +140,18 @@ WITH
140140
UNION SELECT *
141141
FROM
142142
pd
143-
UNION SELECT *
143+
/*UNION SELECT *
144144
FROM
145145
pr
146146
UNION SELECT *
147147
FROM
148-
ps
148+
ps*/
149149
UNION SELECT *
150150
FROM
151151
psp
152-
UNION SELECT *
152+
/*UNION SELECT *
153153
FROM
154-
pu
154+
pu*/
155155
UNION SELECT *
156156
FROM
157157
pus
@@ -177,7 +177,7 @@ SELECT
177177
, event_function_request_id
178178
, b.event_correlation_id
179179
, b.event_xray_trace_id
180-
, event_pointer_types
180+
--, event_pointer_types
181181
, oc.user_ods
182182
FROM
183183
(base b

terraform/account-wide-infrastructure/modules/glue/glue.tf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ resource "aws_glue_job" "glue_job" {
9090
description = "Transfer logs from source to bucket"
9191
glue_version = "5.0"
9292
worker_type = "G.1X"
93-
timeout = 2880
93+
execution_class = "STANDARD"
94+
timeout = 60 # minutes
9495
max_retries = 0
9596
number_of_workers = 4
9697
command {
@@ -110,5 +111,7 @@ resource "aws_glue_job" "glue_job" {
110111
"--enable-continuous-log-filter" = "true"
111112
"--enable-metrics" = "true"
112113
"--extra-py-files" = "s3://${aws_s3_bucket.code-bucket.id}/src.zip"
114+
"--enable-job-insights" = "true"
115+
"--job-language" = "python"
113116
}
114117
}

terraform/account-wide-infrastructure/prod/aws-backup.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ module "source" {
174174
"schedule" : "cron(30 0 ? * 4)" # every Thursday from 00:30 to precede releases
175175
"copy_action" : [{
176176
"cold_storage_after" : 14 # ensures 2 warm including one from previous release
177-
"delete_after" : 105
177+
"delete_after" : 15 * 7
178178
}],
179179
"lifecycle" : {
180180
"delete_after" : 2

terraform/account-wide-infrastructure/test/main.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ provider "aws" {
1212
}
1313

1414
}
15+
provider "awscc" {
16+
region = local.region
17+
18+
assume_role = {
19+
role_arn = "arn:aws:iam::${var.assume_account}:role/${var.assume_role}"
20+
}
21+
}
1522

1623
provider "awscc" {
1724
region = local.region

terraform/backup-infrastructure/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ Then, initialise the terraform backup workspace. For the test account:
2828
```shell
2929
$ cd test
3030
$ terraform init && ( \
31-
terraform workspace new backup-infra-test || \
32-
terraform workspace select backup-infra-test )
31+
terraform workspace new test || \
32+
terraform workspace select test )
3333
```
3434
3535
If you want to apply changes to prod, use the `prod` directory and the `backup-infra-prod` terraform workspace.
@@ -69,8 +69,8 @@ Then, initialise your terraform workspace. For the test account:
6969
```shell
7070
$ cd test
7171
$ terraform init && ( \
72-
terraform workspace new backup-infra-test || \
73-
terraform workspace select backup-infra-test )
72+
terraform workspace new test || \
73+
terraform workspace select test )
7474
```
7575
7676
If you want to destroy resources in prod, use the `prod` directory and the `backup-infra-prod` terraform workspace.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
# We need a key for the backup vaults. This key will be used to encrypt the backups themselves.
3+
# We need one per vault (on the assumption that each vault will be in a different account).
4+
resource "aws_kms_key" "destination_backup_key" {
5+
description = "KMS key for AWS Backup vaults"
6+
deletion_window_in_days = 7
7+
enable_key_rotation = true
8+
policy = jsonencode({
9+
Version = "2012-10-17"
10+
Statement = [
11+
{
12+
Effect = "Allow"
13+
Sid = "Enable IAM User Permissions"
14+
Principal = {
15+
AWS = "arn:aws:iam::${local.destination_account_id}:root"
16+
}
17+
Action = "kms:*"
18+
Resource = "*"
19+
}
20+
]
21+
})
22+
}
23+
24+
module "destination" {
25+
source = "../modules/aws-backup-destination"
26+
27+
source_account_name = "prod" # please note that the assigned value would be the prefix in aws_backup_vault.vault.name
28+
account_id = local.destination_account_id
29+
source_account_id = local.source_account_id
30+
kms_key = aws_kms_key.destination_backup_key.arn
31+
enable_vault_protection = false
32+
}
33+
34+
###
35+
# Destination vault ARN output
36+
###
37+
38+
output "destination_vault_arn" {
39+
# The ARN of the backup vault in the destination account is needed by
40+
# the source account to copy backups into it.
41+
value = module.destination.vault_arn
42+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data "aws_caller_identity" "current" {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
locals {
2+
# Adjust these as required
3+
project_name = "nrlf-prod-backup"
4+
environment_name = "prod"
5+
6+
source_account_id = var.source_account_id
7+
destination_account_id = var.assume_account
8+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
provider "aws" {
2+
region = "eu-west-2"
3+
4+
assume_role {
5+
role_arn = "arn:aws:iam::${var.assume_account}:role/${var.assume_role}"
6+
}
7+
8+
default_tags {
9+
tags = {
10+
project_name = local.project_name
11+
workspace = terraform.workspace
12+
}
13+
}
14+
}
15+
16+
terraform {
17+
required_providers {
18+
aws = {
19+
source = "hashicorp/aws"
20+
version = "~> 5.76.0"
21+
}
22+
}
23+
24+
backend "s3" {
25+
region = "eu-west-2"
26+
bucket = "nhsd-nrlf--terraform-state"
27+
dynamodb_table = "nhsd-nrlf--terraform-state-lock"
28+
key = "terraform-state-backup-infrastructure"
29+
workspace_key_prefix = "nhsd-nrlf"
30+
encrypt = false
31+
}
32+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
variable "assume_account" {
2+
description = "The account id to deploy the infrastructure to"
3+
sensitive = true
4+
}
5+
6+
variable "assume_role" {
7+
description = "Name of the role to assume to deploy the infrastructure"
8+
type = string
9+
}
10+
11+
variable "source_account_id" {
12+
description = "The account id of the backup source account"
13+
type = string
14+
sensitive = true
15+
}

0 commit comments

Comments
 (0)