-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
sqs_notifications.tf
58 lines (52 loc) · 1.96 KB
/
sqs_notifications.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
locals {
enabled = module.this.enabled
sqs_notifications_enabled = local.enabled && var.bucket_notifications_enabled && var.bucket_notifications_type == "SQS"
sqs_queue_name = module.this.id
partition = join("", data.aws_partition.current[*].partition)
}
data "aws_caller_identity" "current" { count = local.enabled ? 1 : 0 }
data "aws_partition" "current" { count = local.enabled ? 1 : 0 }
resource "aws_sqs_queue" "notifications" {
#bridgecrew:skip=BC_AWS_GENERAL_16:Skipping `AWS SQS server side encryption is not enabled` check because this queue does not have sensitive data. Enabling the encryption for S3 publisher requires the new CMK which is extra here.
count = local.sqs_notifications_enabled ? 1 : 0
name = local.sqs_queue_name
policy = join("", data.aws_iam_policy_document.sqs_policy[*].json)
tags = module.this.tags
}
# https://docs.aws.amazon.com/AmazonS3/latest/userguide/grant-destinations-permissions-to-s3.html
data "aws_iam_policy_document" "sqs_policy" {
count = local.sqs_notifications_enabled ? 1 : 0
statement {
effect = "Allow"
principals {
type = "Service"
identifiers = ["s3.amazonaws.com"]
}
resources = ["arn:${local.partition}:sqs:*:*:${local.sqs_queue_name}"]
actions = [
"sqs:SendMessage"
]
condition {
test = "ArnEquals"
variable = "aws:SourceArn"
values = module.aws_s3_bucket.bucket_arn
}
condition {
test = "StringEquals"
variable = "aws:SourceAccount"
values = [
join("", data.aws_caller_identity.current[*].account_id)]
}
}
}
resource "aws_s3_bucket_notification" "bucket_notification" {
count = local.sqs_notifications_enabled ? 1 : 0
bucket = join("", module.aws_s3_bucket.bucket_id)
queue {
queue_arn = join("", aws_sqs_queue.notifications[*].arn)
events = [
"s3:ObjectCreated:*"
]
filter_prefix = var.bucket_notifications_prefix
}
}