-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SNS topic, this is preparatory work to support automated emails. --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
5feeb7e
commit bec56c0
Showing
13 changed files
with
256 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
data "aws_caller_identity" "current" {} | ||
|
||
data "aws_region" "current" {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
locals { | ||
sns_topic_display_name = coalesce(var.custom_sns_topic_name, title(replace("${var.project}-${var.function}-${var.env}", "-", " "))) | ||
sns_topic_name = coalesce(var.custom_sns_topic_name, "${var.project}-${var.function}-${var.env}") | ||
|
||
tags = merge(var.tags, { | ||
Terraform = "true" | ||
Name = local.sns_topic_name, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
resource "aws_sns_topic" "sns_topic" { | ||
name = local.sns_topic_name | ||
display_name = local.sns_topic_display_name | ||
policy = coalesce(var.sns_policy, templatefile("${path.module}/templates/${var.sns_policy_template}.json", { region = data.aws_region.current.id, account_id = data.aws_caller_identity.current.account_id, sns_topic_name = local.sns_topic_name })) | ||
|
||
tags = merge( | ||
var.tags, | ||
tomap( | ||
{ "Name" = local.sns_topic_name } | ||
) | ||
) | ||
kms_master_key_id = var.kms_key_arn | ||
} | ||
|
||
resource "aws_sns_topic_subscription" "email_subscriptions" { | ||
for_each = toset(var.email_subscriptions) | ||
endpoint = each.key | ||
protocol = "email" | ||
topic_arn = aws_sns_topic.sns_topic.arn | ||
raw_message_delivery = false | ||
} | ||
|
||
resource "aws_sns_topic_subscription" "lambda_subscriptions" { | ||
for_each = var.lambda_subscriptions | ||
endpoint = each.value | ||
protocol = "lambda" | ||
topic_arn = aws_sns_topic.sns_topic.arn | ||
raw_message_delivery = false | ||
} | ||
|
||
resource "aws_sns_topic_subscription" "sqs_subscriptions" { | ||
for_each = var.sqs_subscriptions | ||
endpoint = each.value | ||
protocol = "sqs" | ||
topic_arn = aws_sns_topic.sns_topic.arn | ||
raw_message_delivery = true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
output "sns_topic_arn" { | ||
value = aws_sns_topic.sns_topic.arn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"Version": "2012-10-17", | ||
"Id": "default_policy", | ||
"Statement": [ | ||
{ | ||
"Sid": "default_statement", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"AWS": "*" | ||
}, | ||
"Action": [ | ||
"sns:GetTopicAttributes", | ||
"sns:SetTopicAttributes", | ||
"sns:AddPermission", | ||
"sns:RemovePermission", | ||
"sns:DeleteTopic", | ||
"sns:Subscribe", | ||
"sns:ListSubscriptionsByTopic", | ||
"sns:Publish", | ||
"sns:Receive" | ||
], | ||
"Resource": "arn:aws:sns:${region}:${account_id}:${sns_topic_name}", | ||
"Condition": { | ||
"StringEquals": { | ||
"AWS:SourceOwner": "${account_id}" | ||
} | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ | ||
"Version": "2012-10-17", | ||
"Id": "allow_account_access_to_topic_policy", | ||
"Statement": [ | ||
{ | ||
"Sid": "allow_account_access_to_topic", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"AWS": "*" | ||
}, | ||
"Action": [ | ||
"sns:GetTopicAttributes", | ||
"sns:SetTopicAttributes", | ||
"sns:AddPermission", | ||
"sns:RemovePermission", | ||
"sns:DeleteTopic", | ||
"sns:Subscribe", | ||
"sns:ListSubscriptionsByTopic", | ||
"sns:Publish", | ||
"sns:Receive" | ||
], | ||
"Resource": "arn:aws:sns:${region}:${account_id}:${sns_topic_name}", | ||
"Condition": { | ||
"StringEquals": { | ||
"AWS:SourceOwner": "${account_id}" | ||
} | ||
} | ||
}, | ||
{ | ||
"Sid": "allow_eventbridge_access_to_topic", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "events.amazonaws.com" | ||
}, | ||
"Action": "sns:Publish", | ||
"Resource": "arn:aws:sns:${region}:${account_id}:${sns_topic_name}" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
variable "project" { | ||
description = "abbreviation for the project, forms the first part of the resource name" | ||
default = "" | ||
} | ||
|
||
variable "function" { | ||
description = "forms the second part of the resource name" | ||
default = "" | ||
} | ||
|
||
variable "env" { | ||
description = "suffix for environment, e.g. dev" | ||
default = "" | ||
} | ||
|
||
variable "custom_sns_topic_display_name" { | ||
description = "Customised SNS topic display name, leave empty to use standard naming convention" | ||
default = "" | ||
} | ||
|
||
|
||
variable "custom_sns_topic_name" { | ||
description = "Customised SNS topic name, leave empty to use standard naming convention" | ||
default = "" | ||
} | ||
|
||
variable "sns_policy" { | ||
description = "A string containing the SNS policy, if used" | ||
default = "" | ||
} | ||
|
||
variable "sns_policy_template" { | ||
description = "Name of SNS policy template file, if used" | ||
default = "default" | ||
} | ||
|
||
variable "kms_key_arn" { | ||
description = "A KMS key arn to be used to encrypt the queue contents at rest" | ||
default = null | ||
} | ||
|
||
variable "email_subscriptions" { | ||
type = list(string) | ||
description = "List of email addresses to subscribe to this topic" | ||
default = [] | ||
} | ||
|
||
variable "lambda_subscriptions" { | ||
type = map(string) | ||
description = "A map of lambda names to arns to subscribe to this topic" | ||
default = {} | ||
} | ||
|
||
variable "sqs_subscriptions" { | ||
type = map(string) | ||
description = "A map of SQS names to arns to subscribe to this topic" | ||
default = {} | ||
} | ||
|
||
variable "tags" { | ||
type = map(string) | ||
default = {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters