Skip to content

Commit 2863105

Browse files
feat: Add SNS topic delivery status IAM role (#178)
Co-authored-by: Anton Babenko <[email protected]>
1 parent 95f37ca commit 2863105

File tree

6 files changed

+127
-1
lines changed

6 files changed

+127
-1
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,12 @@ See the [functions](https://github.com/terraform-aws-modules/terraform-aws-notif
8484
| Name | Type |
8585
|------|------|
8686
| [aws_cloudwatch_log_group.lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
87+
| [aws_iam_role.sns_feedback_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
8788
| [aws_sns_topic.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic) | resource |
8889
| [aws_sns_topic_subscription.sns_notify_slack](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic_subscription) | resource |
8990
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
9091
| [aws_iam_policy_document.lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
92+
| [aws_iam_policy_document.sns_feedback](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
9193
| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source |
9294
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
9395

@@ -100,6 +102,7 @@ See the [functions](https://github.com/terraform-aws-modules/terraform-aws-notif
100102
| <a name="input_cloudwatch_log_group_tags"></a> [cloudwatch\_log\_group\_tags](#input\_cloudwatch\_log\_group\_tags) | Additional tags for the Cloudwatch log group | `map(string)` | `{}` | no |
101103
| <a name="input_create"></a> [create](#input\_create) | Whether to create all resources | `bool` | `true` | no |
102104
| <a name="input_create_sns_topic"></a> [create\_sns\_topic](#input\_create\_sns\_topic) | Whether to create new SNS topic | `bool` | `true` | no |
105+
| <a name="input_enable_sns_topic_delivery_status_logs"></a> [enable\_sns\_topic\_delivery\_status\_logs](#input\_enable\_sns\_topic\_delivery\_status\_logs) | Whether to enable SNS topic delivery status logs | `bool` | `false` | no |
103106
| <a name="input_iam_policy_path"></a> [iam\_policy\_path](#input\_iam\_policy\_path) | Path of policies to that should be added to IAM role for Lambda Function | `string` | `null` | no |
104107
| <a name="input_iam_role_boundary_policy_arn"></a> [iam\_role\_boundary\_policy\_arn](#input\_iam\_role\_boundary\_policy\_arn) | The ARN of the policy that is used to set the permissions boundary for the role | `string` | `null` | no |
105108
| <a name="input_iam_role_name_prefix"></a> [iam\_role\_name\_prefix](#input\_iam\_role\_name\_prefix) | A unique role name beginning with the specified prefix | `string` | `"lambda"` | no |
@@ -119,13 +122,22 @@ See the [functions](https://github.com/terraform-aws-modules/terraform-aws-notif
119122
| <a name="input_lambda_role"></a> [lambda\_role](#input\_lambda\_role) | IAM role attached to the Lambda Function. If this is set then a role will not be created for you. | `string` | `""` | no |
120123
| <a name="input_lambda_source_path"></a> [lambda\_source\_path](#input\_lambda\_source\_path) | The source path of the custom Lambda function | `string` | `null` | no |
121124
| <a name="input_log_events"></a> [log\_events](#input\_log\_events) | Boolean flag to enabled/disable logging of incoming events | `bool` | `false` | no |
125+
| <a name="input_putin_khuylo"></a> [putin\_khuylo](#input\_putin\_khuylo) | Do you agree that Putin doesn't respect Ukrainian sovereignty and territorial integrity? More info: https://en.wikipedia.org/wiki/Putin_khuylo! | `bool` | `true` | no |
122126
| <a name="input_recreate_missing_package"></a> [recreate\_missing\_package](#input\_recreate\_missing\_package) | Whether to recreate missing Lambda package if it is missing locally or not | `bool` | `true` | no |
123127
| <a name="input_reserved_concurrent_executions"></a> [reserved\_concurrent\_executions](#input\_reserved\_concurrent\_executions) | The amount of reserved concurrent executions for this lambda function. A value of 0 disables lambda from being triggered and -1 removes any concurrency limitations | `number` | `-1` | no |
124128
| <a name="input_slack_channel"></a> [slack\_channel](#input\_slack\_channel) | The name of the channel in Slack for notifications | `string` | n/a | yes |
125129
| <a name="input_slack_emoji"></a> [slack\_emoji](#input\_slack\_emoji) | A custom emoji that will appear on Slack messages | `string` | `":aws:"` | no |
126130
| <a name="input_slack_username"></a> [slack\_username](#input\_slack\_username) | The username that will appear on Slack messages | `string` | n/a | yes |
127131
| <a name="input_slack_webhook_url"></a> [slack\_webhook\_url](#input\_slack\_webhook\_url) | The URL of Slack webhook | `string` | n/a | yes |
132+
| <a name="input_sns_topic_feedback_role_description"></a> [sns\_topic\_feedback\_role\_description](#input\_sns\_topic\_feedback\_role\_description) | Description of IAM role to use for SNS topic delivery status logging | `string` | `null` | no |
133+
| <a name="input_sns_topic_feedback_role_force_detach_policies"></a> [sns\_topic\_feedback\_role\_force\_detach\_policies](#input\_sns\_topic\_feedback\_role\_force\_detach\_policies) | Specifies to force detaching any policies the IAM role has before destroying it. | `bool` | `true` | no |
134+
| <a name="input_sns_topic_feedback_role_name"></a> [sns\_topic\_feedback\_role\_name](#input\_sns\_topic\_feedback\_role\_name) | Name of the IAM role to use for SNS topic delivery status logging | `string` | `null` | no |
135+
| <a name="input_sns_topic_feedback_role_path"></a> [sns\_topic\_feedback\_role\_path](#input\_sns\_topic\_feedback\_role\_path) | Path of IAM role to use for SNS topic delivery status logging | `string` | `null` | no |
136+
| <a name="input_sns_topic_feedback_role_permissions_boundary"></a> [sns\_topic\_feedback\_role\_permissions\_boundary](#input\_sns\_topic\_feedback\_role\_permissions\_boundary) | The ARN of the policy that is used to set the permissions boundary for the IAM role used by SNS topic delivery status logging | `string` | `null` | no |
137+
| <a name="input_sns_topic_feedback_role_tags"></a> [sns\_topic\_feedback\_role\_tags](#input\_sns\_topic\_feedback\_role\_tags) | A map of tags to assign to IAM the SNS topic feedback role | `map(string)` | `{}` | no |
128138
| <a name="input_sns_topic_kms_key_id"></a> [sns\_topic\_kms\_key\_id](#input\_sns\_topic\_kms\_key\_id) | ARN of the KMS key used for enabling SSE on the topic | `string` | `""` | no |
139+
| <a name="input_sns_topic_lambda_feedback_role_arn"></a> [sns\_topic\_lambda\_feedback\_role\_arn](#input\_sns\_topic\_lambda\_feedback\_role\_arn) | IAM role for SNS topic delivery status logs. If this is set then a role will not be created for you. | `string` | `""` | no |
140+
| <a name="input_sns_topic_lambda_feedback_sample_rate"></a> [sns\_topic\_lambda\_feedback\_sample\_rate](#input\_sns\_topic\_lambda\_feedback\_sample\_rate) | The percentage of successful deliveries to log | `number` | `100` | no |
129141
| <a name="input_sns_topic_name"></a> [sns\_topic\_name](#input\_sns\_topic\_name) | The name of the SNS topic to create | `string` | n/a | yes |
130142
| <a name="input_sns_topic_tags"></a> [sns\_topic\_tags](#input\_sns\_topic\_tags) | Additional tags for the SNS topic | `map(string)` | `{}` | no |
131143
| <a name="input_subscription_filter_policy"></a> [subscription\_filter\_policy](#input\_subscription\_filter\_policy) | (Optional) A valid filter policy that will be used in the subscription to filter messages seen by the target resource. | `string` | `null` | no |
@@ -144,6 +156,7 @@ See the [functions](https://github.com/terraform-aws-modules/terraform-aws-notif
144156
| <a name="output_notify_slack_lambda_function_name"></a> [notify\_slack\_lambda\_function\_name](#output\_notify\_slack\_lambda\_function\_name) | The name of the Lambda function |
145157
| <a name="output_notify_slack_lambda_function_version"></a> [notify\_slack\_lambda\_function\_version](#output\_notify\_slack\_lambda\_function\_version) | Latest published version of your Lambda function |
146158
| <a name="output_slack_topic_arn"></a> [slack\_topic\_arn](#output\_slack\_topic\_arn) | The ARN of the SNS topic from which messages will be sent to Slack |
159+
| <a name="output_sns_topic_feedback_role_arn"></a> [sns\_topic\_feedback\_role\_arn](#output\_sns\_topic\_feedback\_role\_arn) | The Amazon Resource Name (ARN) of the IAM role used for SNS delivery status logging |
147160
| <a name="output_this_slack_topic_arn"></a> [this\_slack\_topic\_arn](#output\_this\_slack\_topic\_arn) | The ARN of the SNS topic from which messages will be sent to Slack (backward compatibility for version 4.x) |
148161
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
149162

examples/cloudwatch-alerts-to-slack/main.tf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ module "notify_slack" {
2222
"test",
2323
])
2424

25-
sns_topic_name = "slack-topic"
25+
sns_topic_name = "slack-topic"
26+
enable_sns_topic_delivery_status_logs = true
27+
28+
# Specify the ARN of the pre-defined feedback role or leave blank to have the module create it
29+
#sns_topic_lambda_feedback_role_arn = "arn:aws:iam::111122223333:role/sns-delivery-status"
2630

2731
lambda_function_name = "notify_slack_${each.value}"
2832

iam.tf

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
locals {
2+
create_sns_feedback_role = local.create && var.create_sns_topic && var.enable_sns_topic_delivery_status_logs && var.sns_topic_lambda_feedback_role_arn == ""
3+
}
4+
5+
data "aws_iam_policy_document" "sns_feedback" {
6+
count = local.create_sns_feedback_role ? 1 : 0
7+
8+
statement {
9+
sid = "PermitDeliveryStatusMessagesToCloudWatchLogs"
10+
effect = "Allow"
11+
12+
actions = [
13+
"logs:CreateLogGroup",
14+
"logs:CreateLogStream",
15+
"logs:PutLogEvents",
16+
"logs:PutMetricFilter",
17+
"logs:PutRetentionPolicy"
18+
]
19+
20+
resources = [
21+
"*"
22+
]
23+
}
24+
}
25+
26+
resource "aws_iam_role" "sns_feedback_role" {
27+
count = local.create_sns_feedback_role ? 1 : 0
28+
29+
name = var.sns_topic_feedback_role_name
30+
description = var.sns_topic_feedback_role_description
31+
path = var.sns_topic_feedback_role_path
32+
force_detach_policies = var.sns_topic_feedback_role_force_detach_policies
33+
permissions_boundary = var.sns_topic_feedback_role_permissions_boundary
34+
assume_role_policy = data.aws_iam_policy_document.sns_feedback[0].json
35+
36+
tags = merge(var.tags, var.sns_topic_feedback_role_tags)
37+
}

main.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ data "aws_partition" "current" {}
33
data "aws_region" "current" {}
44

55
locals {
6+
create = var.create && var.putin_khuylo
7+
68
sns_topic_arn = try(
79
aws_sns_topic.this[0].arn,
810
"arn:${data.aws_partition.current.id}:sns:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:${var.sns_topic_name}",
911
""
1012
)
1113

14+
sns_feedback_role = local.create_sns_feedback_role ? aws_iam_role.sns_feedback_role[0].arn : var.sns_topic_lambda_feedback_role_arn
1215
lambda_policy_document = {
1316
sid = "AllowWriteToCloudwatchLogs"
1417
effect = "Allow"
@@ -57,6 +60,10 @@ resource "aws_sns_topic" "this" {
5760

5861
kms_master_key_id = var.sns_topic_kms_key_id
5962

63+
lambda_failure_feedback_role_arn = var.enable_sns_topic_delivery_status_logs ? local.sns_feedback_role : null
64+
lambda_success_feedback_role_arn = var.enable_sns_topic_delivery_status_logs ? local.sns_feedback_role : null
65+
lambda_success_feedback_sample_rate = var.enable_sns_topic_delivery_status_logs ? var.sns_topic_lambda_feedback_sample_rate : null
66+
6067
tags = merge(var.tags, var.sns_topic_tags)
6168
}
6269

outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,8 @@ output "lambda_cloudwatch_log_group_arn" {
4848
description = "The Amazon Resource Name (ARN) specifying the log group"
4949
value = try(aws_cloudwatch_log_group.lambda[0].arn, "")
5050
}
51+
52+
output "sns_topic_feedback_role_arn" {
53+
description = "The Amazon Resource Name (ARN) of the IAM role used for SNS delivery status logging"
54+
value = local.sns_feedback_role
55+
}

variables.tf

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
variable "putin_khuylo" {
2+
description = "Do you agree that Putin doesn't respect Ukrainian sovereignty and territorial integrity? More info: https://en.wikipedia.org/wiki/Putin_khuylo!"
3+
type = bool
4+
default = true
5+
}
6+
17
variable "create" {
28
description = "Whether to create all resources"
39
type = bool
@@ -57,6 +63,60 @@ variable "sns_topic_kms_key_id" {
5763
default = ""
5864
}
5965

66+
variable "enable_sns_topic_delivery_status_logs" {
67+
description = "Whether to enable SNS topic delivery status logs"
68+
type = bool
69+
default = false
70+
}
71+
72+
variable "sns_topic_lambda_feedback_role_arn" {
73+
description = "IAM role for SNS topic delivery status logs. If this is set then a role will not be created for you."
74+
type = string
75+
default = ""
76+
}
77+
78+
variable "sns_topic_feedback_role_name" {
79+
description = "Name of the IAM role to use for SNS topic delivery status logging"
80+
type = string
81+
default = null
82+
}
83+
84+
variable "sns_topic_feedback_role_description" {
85+
description = "Description of IAM role to use for SNS topic delivery status logging"
86+
type = string
87+
default = null
88+
}
89+
90+
variable "sns_topic_feedback_role_path" {
91+
description = "Path of IAM role to use for SNS topic delivery status logging"
92+
type = string
93+
default = null
94+
}
95+
96+
variable "sns_topic_feedback_role_force_detach_policies" {
97+
description = "Specifies to force detaching any policies the IAM role has before destroying it."
98+
type = bool
99+
default = true
100+
}
101+
102+
variable "sns_topic_feedback_role_permissions_boundary" {
103+
description = "The ARN of the policy that is used to set the permissions boundary for the IAM role used by SNS topic delivery status logging"
104+
type = string
105+
default = null
106+
}
107+
108+
variable "sns_topic_feedback_role_tags" {
109+
description = "A map of tags to assign to IAM the SNS topic feedback role"
110+
type = map(string)
111+
default = {}
112+
}
113+
114+
variable "sns_topic_lambda_feedback_sample_rate" {
115+
description = "The percentage of successful deliveries to log"
116+
type = number
117+
default = 100
118+
}
119+
60120
variable "slack_webhook_url" {
61121
description = "The URL of Slack webhook"
62122
type = string

0 commit comments

Comments
 (0)