Skip to content

Commit f15a1c3

Browse files
authored
VED-Add-Lambda-s3 Policy (#894)
* add kms encryption for s3 * VED-863: Attach Policy for kms and s3 to ecs task policy
1 parent 68c8a28 commit f15a1c3

File tree

5 files changed

+73
-1
lines changed

5 files changed

+73
-1
lines changed

terraform/ecs_batch_processor_config.tf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,14 @@ resource "aws_iam_policy" "ecs_task_exec_policy" {
157157
"firehose:PutRecordBatch"
158158
],
159159
"Resource" : "arn:aws:firehose:*:*:deliverystream/${module.splunk.firehose_stream_name}"
160-
}
160+
},
161+
{
162+
Effect = "Allow",
163+
Action = [
164+
"s3:PutObject",
165+
],
166+
Resource = "${aws_s3_bucket.data_quality_reports_bucket.arn}/*"
167+
},
161168
]
162169
})
163170
}

terraform/endpoints.tf

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ locals {
44
policy_path = "${path.root}/policies"
55
}
66

7+
# Select the Policy folder
78
data "aws_iam_policy_document" "logs_policy_document" {
89
source_policy_documents = [templatefile("${local.policy_path}/log.json", {})]
910
}
@@ -57,6 +58,20 @@ data "aws_iam_policy_document" "imms_policy_document" {
5758
]
5859
}
5960

61+
data "aws_iam_policy_document" "imms_data_quality_s3_doc" {
62+
source_policy_documents = [
63+
templatefile("${local.policy_path}/s3_data_quality_access.json", {
64+
s3_bucket_arn = aws_s3_bucket.data_quality_reports_bucket.arn
65+
kms_key_arn = data.aws_kms_key.existing_s3_encryption_key.arn
66+
})
67+
]
68+
}
69+
70+
resource "aws_iam_policy" "imms_s3_kms_policy" {
71+
name = "${local.short_prefix}-s3-kms-policy"
72+
policy = data.aws_iam_policy_document.imms_data_quality_s3_doc.json
73+
}
74+
6075
module "imms_event_endpoint_lambdas" {
6176
source = "./modules/lambda"
6277
count = length(local.imms_endpoints)
@@ -71,6 +86,19 @@ module "imms_event_endpoint_lambdas" {
7186
vpc_security_group_ids = [data.aws_security_group.existing_securitygroup.id]
7287
}
7388

89+
90+
# Attach data quality report S3 bucket and KMS policy only to "create_imms" and "update_imms" endpoints
91+
resource "aws_iam_role_policy_attachment" "attach_data_quality_s3_to_specific_lambdas" {
92+
for_each = {
93+
for i, mod in module.imms_event_endpoint_lambdas :
94+
local.imms_endpoints[i] => mod
95+
if local.imms_endpoints[i] == "create_imms" || local.imms_endpoints[i] == "update_imms"
96+
}
97+
98+
role = each.value.lambda_role_name
99+
policy_arn = aws_iam_policy.imms_s3_kms_policy.arn
100+
}
101+
74102
locals {
75103
# Mapping outputs with each called lambda
76104
imms_lambdas = {

terraform/modules/lambda/outputs.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ output "lambda_arn" {
77
output "invoke_arn" {
88
value = module.lambda_function_container_image.lambda_function_invoke_arn
99
}
10+
output "lambda_role_name" {
11+
value = aws_iam_role.lambda_role.name
12+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Sid": "S3Access",
6+
"Effect": "Allow",
7+
"Action": ["s3:PutObject"],
8+
"Resource": ["${s3_bucket_arn}/*"]
9+
},
10+
{
11+
"Sid": "KMSAccessForS3Encryption",
12+
"Effect": "Allow",
13+
"Action": [
14+
"kms:Encrypt",
15+
"kms:Decrypt",
16+
"kms:ReEncrypt*",
17+
"kms:GenerateDataKey*",
18+
"kms:DescribeKey"
19+
],
20+
"Resource": "${kms_key_arn}"
21+
}
22+
]
23+
}

terraform/s3_dq_reports.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,15 @@ resource "aws_s3_bucket_policy" "data_quality_bucket_policy" {
8585
},
8686
]
8787
})
88+
}
89+
90+
resource "aws_s3_bucket_server_side_encryption_configuration" "s3_data_quality_encryption" {
91+
bucket = aws_s3_bucket.data_quality_reports_bucket.id
92+
93+
rule {
94+
apply_server_side_encryption_by_default {
95+
kms_master_key_id = data.aws_kms_key.existing_s3_encryption_key.arn
96+
sse_algorithm = "aws:kms"
97+
}
98+
}
8899
}

0 commit comments

Comments
 (0)