Skip to content

Commit ac6b407

Browse files
authored
PRMP-1342: Create lambda to handle DynamoDB document reference delete (#222)
* Add delete object lambda and triggers * rebase with policy changes
1 parent 0079c18 commit ac6b407

File tree

2 files changed

+123
-2
lines changed

2 files changed

+123
-2
lines changed

infrastructure/dynamo_db.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module "document_reference_dynamodb_table" {
33
table_name = var.docstore_dynamodb_table_name
44
hash_key = "ID"
55
deletion_protection_enabled = local.is_production
6-
stream_enabled = false
6+
stream_enabled = true
7+
stream_view_type = "OLD_IMAGE"
78
ttl_enabled = true
89
ttl_attribute_name = "TTL"
910
point_in_time_recovery_enabled = !local.is_sandbox
@@ -66,7 +67,8 @@ module "lloyd_george_reference_dynamodb_table" {
6667
table_name = var.lloyd_george_dynamodb_table_name
6768
hash_key = "ID"
6869
deletion_protection_enabled = local.is_production
69-
stream_enabled = false
70+
stream_enabled = true
71+
stream_view_type = "OLD_IMAGE"
7072
ttl_enabled = true
7173
ttl_attribute_name = "TTL"
7274
point_in_time_recovery_enabled = !local.is_sandbox
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
module "delete-document-object-alarm" {
2+
source = "./modules/lambda_alarms"
3+
lambda_function_name = module.delete-document-object-lambda.function_name
4+
lambda_timeout = module.delete-document-object-lambda.timeout
5+
lambda_name = "delete_document_object_handler"
6+
namespace = "AWS/Lambda"
7+
alarm_actions = [module.delete-document-object-alarm-topic.arn]
8+
ok_actions = [module.delete-document-object-alarm-topic.arn]
9+
}
10+
11+
module "delete-document-object-alarm-topic" {
12+
source = "./modules/sns"
13+
sns_encryption_key_id = module.sns_encryption_key.id
14+
current_account_id = data.aws_caller_identity.current.account_id
15+
topic_name = "delete-document-object-topic"
16+
topic_protocol = "lambda"
17+
topic_endpoint = module.delete-document-object-lambda.lambda_arn
18+
delivery_policy = jsonencode({
19+
"Version" : "2012-10-17",
20+
"Statement" : [
21+
{
22+
"Effect" : "Allow",
23+
"Principal" : {
24+
"Service" : "cloudwatch.amazonaws.com"
25+
},
26+
"Action" : [
27+
"SNS:Publish",
28+
],
29+
"Condition" : {
30+
"ArnLike" : {
31+
"aws:SourceArn" : "arn:aws:cloudwatch:eu-west-2:${data.aws_caller_identity.current.account_id}:alarm:*"
32+
}
33+
}
34+
"Resource" : "*"
35+
}
36+
]
37+
})
38+
}
39+
40+
module "delete-document-object-lambda" {
41+
source = "./modules/lambda"
42+
name = "DeleteDocumentObjectS3"
43+
handler = "handlers.delete_document_object_handler.lambda_handler"
44+
lambda_timeout = 900
45+
iam_role_policy_documents = [
46+
module.document_reference_dynamodb_table.dynamodb_read_policy_document,
47+
module.document_reference_dynamodb_table.dynamodb_write_policy_document,
48+
module.ndr-document-store.s3_read_policy_document,
49+
module.ndr-document-store.s3_write_policy_document,
50+
module.lloyd_george_reference_dynamodb_table.dynamodb_read_policy_document,
51+
module.lloyd_george_reference_dynamodb_table.dynamodb_write_policy_document,
52+
module.ndr-lloyd-george-store.s3_read_policy_document,
53+
module.ndr-lloyd-george-store.s3_write_policy_document,
54+
module.ndr-app-config.app_config_policy,
55+
aws_iam_policy.dynamodb_stream_delete_object_policy.policy
56+
]
57+
rest_api_id = null
58+
api_execution_arn = null
59+
lambda_environment_variables = {
60+
APPCONFIG_APPLICATION = module.ndr-app-config.app_config_application_id
61+
APPCONFIG_ENVIRONMENT = module.ndr-app-config.app_config_environment_id
62+
APPCONFIG_CONFIGURATION = module.ndr-app-config.app_config_configuration_profile_id
63+
WORKSPACE = terraform.workspace
64+
}
65+
is_gateway_integration_needed = false
66+
is_invoked_from_gateway = false
67+
}
68+
69+
resource "aws_iam_policy" "dynamodb_stream_delete_object_policy" {
70+
name = "${terraform.workspace}_dynamodb_stream_to_delete_records_policy"
71+
72+
policy = jsonencode({
73+
Version = "2012-10-17"
74+
Statement = [
75+
{
76+
Action = ["dynamodb:GetRecords", "dynamodb:GetShardIterator", "dynamodb:DescribeStream", "dynamodb:ListStreams"]
77+
Effect = "Allow"
78+
Resource = [
79+
module.lloyd_george_reference_dynamodb_table.dynamodb_stream_arn,
80+
module.document_reference_dynamodb_table.dynamodb_stream_arn
81+
]
82+
},
83+
]
84+
})
85+
}
86+
87+
resource "aws_lambda_event_source_mapping" "lloyd_george_dynamodb_stream" {
88+
event_source_arn = module.lloyd_george_reference_dynamodb_table.dynamodb_stream_arn
89+
function_name = module.delete-document-object-lambda.lambda_arn
90+
batch_size = 1
91+
starting_position = "LATEST"
92+
93+
filter_criteria {
94+
filter {
95+
pattern = jsonencode({
96+
"eventName" : [
97+
"REMOVE"
98+
]
99+
})
100+
}
101+
}
102+
}
103+
104+
resource "aws_lambda_event_source_mapping" "document_reference_dynamodb_stream" {
105+
event_source_arn = module.document_reference_dynamodb_table.dynamodb_stream_arn
106+
function_name = module.delete-document-object-lambda.lambda_arn
107+
batch_size = 1
108+
starting_position = "LATEST"
109+
110+
filter_criteria {
111+
filter {
112+
pattern = jsonencode({
113+
"eventName" : [
114+
"REMOVE"
115+
]
116+
})
117+
}
118+
}
119+
}

0 commit comments

Comments
 (0)