Skip to content

Commit 13a3922

Browse files
committed
Merge branch 'feature/PI-357-automatic_snapshots_of_etl' into release/2024-07-22
2 parents da61bda + b2a30c4 commit 13a3922

File tree

9 files changed

+114
-6
lines changed

9 files changed

+114
-6
lines changed

infrastructure/terraform/per_account/int/main.tf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,37 @@ module "snapshot_bucket" {
107107
version = "3.15.2"
108108
bucket = "${local.project}--${replace(terraform.workspace, "_", "-")}--snapshot"
109109
attach_deny_insecure_transport_policy = true
110+
attach_access_log_delivery_policy = true
110111
versioning = {
111112
enabled = true
112113
}
113114
tags = {
114115
Name = "${local.project}--${replace(terraform.workspace, "_", "-")}--snapshot"
115116
}
116117
}
118+
119+
data "aws_s3_bucket_policy" "existing_policy" {
120+
bucket = module.snapshot_bucket.s3_bucket_id
121+
}
122+
123+
resource "aws_s3_bucket_policy" "snapshot_bucket_policy" {
124+
bucket = module.snapshot_bucket.s3_bucket_id
125+
126+
policy = jsonencode({
127+
Version = "2012-10-17",
128+
Statement = concat(
129+
jsondecode(data.aws_s3_bucket_policy.existing_policy.policy)["Statement"], [
130+
{
131+
Sid = "AllowDynamoDBExport",
132+
Effect = "Allow",
133+
Principal = { Service = "dynamodb.amazonaws.com" },
134+
Action = [
135+
"s3:PutObject",
136+
"s3:AbortMultipartUpload",
137+
"s3:ListMultipartUploadParts"
138+
],
139+
Resource = "${module.snapshot_bucket.s3_bucket_arn}/*"
140+
}
141+
])
142+
})
143+
}

infrastructure/terraform/per_account/prod/main.tf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,37 @@ module "snapshot_bucket" {
105105
version = "3.15.2"
106106
bucket = "${local.project}--${replace(terraform.workspace, "_", "-")}--snapshot"
107107
attach_deny_insecure_transport_policy = true
108+
attach_access_log_delivery_policy = true
108109
versioning = {
109110
enabled = true
110111
}
111112
tags = {
112113
Name = "${local.project}--${replace(terraform.workspace, "_", "-")}--snapshot"
113114
}
114115
}
116+
117+
data "aws_s3_bucket_policy" "existing_policy" {
118+
bucket = module.snapshot_bucket.s3_bucket_id
119+
}
120+
121+
resource "aws_s3_bucket_policy" "snapshot_bucket_policy" {
122+
bucket = module.snapshot_bucket.s3_bucket_id
123+
124+
policy = jsonencode({
125+
Version = "2012-10-17",
126+
Statement = concat(
127+
jsondecode(data.aws_s3_bucket_policy.existing_policy.policy)["Statement"], [
128+
{
129+
Sid = "AllowDynamoDBExport",
130+
Effect = "Allow",
131+
Principal = { Service = "dynamodb.amazonaws.com" },
132+
Action = [
133+
"s3:PutObject",
134+
"s3:AbortMultipartUpload",
135+
"s3:ListMultipartUploadParts"
136+
],
137+
Resource = "${module.snapshot_bucket.s3_bucket_arn}/*"
138+
}
139+
])
140+
})
141+
}

infrastructure/terraform/per_workspace/locals.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ locals {
1010
# e.g. api.cpm.dev.national.nhs.uk
1111
zone = var.domain
1212

13-
domain = "${terraform.workspace}.${var.domain}"
13+
domain = "${terraform.workspace}.${var.domain}"
14+
etl_snapshot_bucket = contains(["int", "prod"], var.environment) ? "${local.project}--${replace(var.environment, "_", "-")}--snapshot" : "snapshot_not_required"
1415
}

infrastructure/terraform/per_workspace/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,5 +192,6 @@ module "sds_etl" {
192192
table_arn = module.table.dynamodb_table_arn
193193
is_persistent = var.workspace_type == "PERSISTENT"
194194
truststore_bucket = data.aws_s3_bucket.truststore_bucket
195+
etl_snapshot_bucket = local.etl_snapshot_bucket
195196
environment = var.environment
196197
}

infrastructure/terraform/per_workspace/modules/etl/sds/etl-diagram.asl.json

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,38 @@
262262
"Next": "fail"
263263
}
264264
],
265-
"Default": "delete-state-lock"
265+
"Default": "set-snapshot-bucket"
266+
},
267+
"set-snapshot-bucket": {
268+
"Type": "Pass",
269+
"Next": "check-snapshot-bucket",
270+
"Result": {
271+
"snapshot_bucket": "${etl_snapshot_bucket}"
272+
}
273+
},
274+
"check-snapshot-bucket": {
275+
"Type": "Choice",
276+
"Choices": [
277+
{
278+
"Variable": "$.snapshot_bucket",
279+
"StringEquals": "snapshot_not_required",
280+
"Next": "skip-export"
281+
}
282+
],
283+
"Default": "export-dynamodb-to-s3"
284+
},
285+
"export-dynamodb-to-s3": {
286+
"Type": "Task",
287+
"Parameters": {
288+
"S3Bucket": "${etl_snapshot_bucket}",
289+
"TableArn": "${table_arn}"
290+
},
291+
"Resource": "arn:aws:states:::aws-sdk:dynamodb:exportTableToPointInTime",
292+
"Next": "delete-state-lock"
293+
},
294+
"skip-export": {
295+
"Type": "Pass",
296+
"Next": "delete-state-lock"
266297
},
267298
"delete-state-lock": {
268299
"Type": "Task",

infrastructure/terraform/per_workspace/modules/etl/sds/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ resource "aws_sfn_state_machine" "state_machine" {
303303
etl_update_state_machine_arn = module.update_transform_and_load_step_function.state_machine_arn
304304
etl_bulk_state_machine_arn = module.bulk_transform_and_load_step_function.state_machine_arn
305305
etl_state_lock_key = var.etl_state_lock_key
306+
etl_snapshot_bucket = var.etl_snapshot_bucket
307+
table_arn = var.table_arn
306308
}
307309
)
308310
logging_configuration {

infrastructure/terraform/per_workspace/modules/etl/sds/step_function_role.tf

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,14 @@ locals {
4848
"s3:ListBucket",
4949
"s3:ListBucketMultipartUploads",
5050
"s3:PutObjectVersionTagging",
51-
"s3:DeleteObject"
51+
"s3:DeleteObject",
52+
"s3:PutBucketPolicy"
5253
]
5354
resources = [
5455
"${module.bucket.s3_bucket_arn}",
55-
"${module.bucket.s3_bucket_arn}/*"
56+
"${module.bucket.s3_bucket_arn}/*",
57+
"arn:aws:s3:::${var.etl_snapshot_bucket}/*",
58+
"arn:aws:s3:::${var.etl_snapshot_bucket}"
5659
]
5760
}
5861

@@ -69,6 +72,19 @@ locals {
6972
]
7073
resources = ["*"]
7174
}
75+
76+
dynamodb = {
77+
actions = ["dynamodb:ExportTableToPointInTime"]
78+
resources = ["arn:aws:dynamodb:eu-west-2:${var.assume_account}:table/${var.table_name}"]
79+
}
80+
81+
kms = {
82+
actions = [
83+
"kms:Decrypt",
84+
"kms:DescribeKey"
85+
]
86+
resources = [data.aws_kms_key.dynamodb_kms_key.arn]
87+
}
7288
}
7389

7490
depends_on = [module.bucket, module.update_transform_and_load_step_function, module.bulk_transform_and_load_step_function]
@@ -106,6 +122,10 @@ data "aws_iam_policy_document" "service" {
106122
}
107123
}
108124

125+
data "aws_kms_key" "dynamodb_kms_key" {
126+
key_id = "alias/${var.table_name}"
127+
}
128+
109129
resource "aws_iam_policy" "service" {
110130
for_each = { for k, v in local.service_integrations : k => v }
111131
name = "${local.name}--${each.key}"

infrastructure/terraform/per_workspace/modules/etl/sds/vars.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ variable "table_name" {}
2323
variable "table_arn" {}
2424
variable "is_persistent" {}
2525
variable "truststore_bucket" {}
26+
variable "etl_snapshot_bucket" {}
2627
variable "environment" {
2728

2829
}

scripts/infrastructure/policies/deployment2-policy.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,6 @@
146146
"s3:PutBucketOwnershipControls",
147147
"s3:PutBucketPublicAccessBlock",
148148
"s3:PutBucketAcl",
149-
"s3:PutBucketPolicy",
150-
"s3:PutBucketLogging",
151149
"s3:DeleteBucketPolicy",
152150
"s3:DeleteBucket",
153151
"s3:DeleteObject",

0 commit comments

Comments
 (0)