Skip to content
This repository was archived by the owner on Nov 9, 2022. It is now read-only.

Commit 5312d97

Browse files
authored
feat: Creating SNS/SQS policies should be optional (terraform-aws-modules#54)
1 parent 3c45c8c commit 5312d97

File tree

11 files changed

+75
-39
lines changed

11 files changed

+75
-39
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ module "s3_bucket" {
8484

8585
| Name | Version |
8686
|------|---------|
87-
| terraform | >= 0.12.6, < 0.14 |
88-
| aws | >= 3.0, < 4.0 |
87+
| terraform | >= 0.12.6 |
88+
| aws | >= 3.0 |
8989

9090
## Providers
9191

9292
| Name | Version |
9393
|------|---------|
94-
| aws | >= 3.0, < 4.0 |
94+
| aws | >= 3.0 |
9595

9696
## Inputs
9797

examples/notification/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ Note that this example may create resources which cost money. Run `terraform des
1919

2020
| Name | Version |
2121
|------|---------|
22-
| terraform | >= 0.12.6, < 0.14 |
23-
| aws | >= 3.0, < 4.0 |
24-
| null | ~> 2 |
25-
| random | ~> 2 |
22+
| terraform | >= 0.12.6 |
23+
| aws | >= 3.0 |
24+
| null | >= 2 |
25+
| random | >= 2 |
2626

2727
## Providers
2828

2929
| Name | Version |
3030
|------|---------|
31-
| aws | >= 3.0, < 4.0 |
32-
| null | ~> 2 |
33-
| random | ~> 2 |
31+
| aws | >= 3.0 |
32+
| null | >= 2 |
33+
| random | >= 2 |
3434

3535
## Inputs
3636

examples/notification/main.tf

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@ resource "aws_sqs_queue" "this" {
7676
name = "${random_pet.this.id}-${count.index}"
7777
}
7878

79+
# SQS policy created outside of the module
80+
data "aws_iam_policy_document" "sqs_external" {
81+
statement {
82+
effect = "Allow"
83+
actions = ["sqs:SendMessage"]
84+
85+
principals {
86+
type = "Service"
87+
identifiers = ["s3.amazonaws.com"]
88+
}
89+
90+
resources = [aws_sqs_queue.this[0].arn]
91+
}
92+
}
93+
94+
resource "aws_sqs_queue_policy" "allow_external" {
95+
queue_url = aws_sqs_queue.this[0].id
96+
policy = data.aws_iam_policy_document.sqs_external.json
97+
}
98+
7999
module "all_notifications" {
80100
source = "../../modules/notification"
81101

@@ -129,4 +149,6 @@ module "all_notifications" {
129149
}
130150
}
131151

152+
# Creation of policy is handled outside of the module
153+
create_sqs_policy = false
132154
}

examples/notification/versions.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
terraform {
2-
required_version = ">= 0.12.6, < 0.14"
2+
required_version = ">= 0.12.6"
33

44
required_providers {
5-
aws = ">= 3.0, < 4.0"
6-
random = "~> 2"
7-
null = "~> 2"
5+
aws = ">= 3.0"
6+
random = ">= 2"
7+
null = ">= 2"
88
}
99
}

examples/s3-replication/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ Note that this example may create resources which cost money. Run `terraform des
2121

2222
| Name | Version |
2323
|------|---------|
24-
| terraform | >= 0.12.6, < 0.14 |
25-
| aws | >= 3.0, < 4.0 |
26-
| random | ~> 2 |
24+
| terraform | >= 0.12.6 |
25+
| aws | >= 3.0 |
26+
| random | >= 2.0 |
2727

2828
## Providers
2929

3030
| Name | Version |
3131
|------|---------|
32-
| aws | >= 3.0, < 4.0 |
33-
| aws.replica | >= 3.0, < 4.0 |
34-
| random | ~> 2 |
32+
| aws | >= 3.0 |
33+
| aws.replica | >= 3.0 |
34+
| random | >= 2.0 |
3535

3636
## Inputs
3737

examples/s3-replication/versions.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
terraform {
2-
required_version = ">= 0.12.6, < 0.14"
2+
required_version = ">= 0.12.6"
33

44
required_providers {
5-
aws = ">= 3.0, < 4.0"
6-
random = "~> 2"
5+
aws = ">= 3.0"
6+
random = ">= 2.0"
77
}
88
}

modules/notification/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ Creates S3 bucket notification resource with all supported types of deliveries:
77

88
| Name | Version |
99
|------|---------|
10-
| terraform | >= 0.12.6, < 0.14 |
11-
| aws | >= 3.0, < 4.0 |
12-
| random | ~> 2 |
10+
| terraform | >= 0.12.6 |
11+
| aws | >= 3.0 |
12+
| random | >= 2.0 |
1313

1414
## Providers
1515

1616
| Name | Version |
1717
|------|---------|
18-
| aws | >= 3.0, < 4.0 |
18+
| aws | >= 3.0 |
1919

2020
## Inputs
2121

@@ -24,6 +24,8 @@ Creates S3 bucket notification resource with all supported types of deliveries:
2424
| bucket | Name of S3 bucket to use | `string` | `""` | no |
2525
| bucket\_arn | ARN of S3 bucket to use in policies | `string` | `null` | no |
2626
| create | Whether to create this resource or not? | `bool` | `true` | no |
27+
| create\_sns\_policy | Whether to create a policy for SNS permissions or not? | `bool` | `true` | no |
28+
| create\_sqs\_policy | Whether to create a policy for SQS permissions or not? | `bool` | `true` | no |
2729
| lambda\_notifications | Map of S3 bucket notifications to Lambda function | `any` | `{}` | no |
2830
| sns\_notifications | Map of S3 bucket notifications to SNS topic | `any` | `{}` | no |
2931
| sqs\_notifications | Map of S3 bucket notifications to SQS queue | `any` | `{}` | no |

modules/notification/main.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ data "aws_arn" "queue" {
7474
}
7575

7676
data "aws_iam_policy_document" "sqs" {
77-
for_each = var.sqs_notifications
77+
for_each = var.create_sqs_policy ? var.sqs_notifications : tomap({})
7878

7979
statement {
8080
sid = "AllowSQSS3BucketNotification"
@@ -101,15 +101,15 @@ data "aws_iam_policy_document" "sqs" {
101101
}
102102

103103
resource "aws_sqs_queue_policy" "allow" {
104-
for_each = var.sqs_notifications
104+
for_each = var.create_sqs_policy ? var.sqs_notifications : tomap({})
105105

106106
queue_url = lookup(each.value, "queue_id", lookup(local.queue_ids, each.key, null))
107107
policy = data.aws_iam_policy_document.sqs[each.key].json
108108
}
109109

110110
# SNS Topic
111111
data "aws_iam_policy_document" "sns" {
112-
for_each = var.sns_notifications
112+
for_each = var.create_sns_policy ? var.sns_notifications : tomap({})
113113

114114
statement {
115115
sid = "AllowSNSS3BucketNotification"
@@ -136,7 +136,7 @@ data "aws_iam_policy_document" "sns" {
136136
}
137137

138138
resource "aws_sns_topic_policy" "allow" {
139-
for_each = var.sns_notifications
139+
for_each = var.create_sns_policy ? var.sns_notifications : tomap({})
140140

141141
arn = each.value.topic_arn
142142
policy = data.aws_iam_policy_document.sns[each.key].json

modules/notification/variables.tf

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ variable "create" {
44
default = true
55
}
66

7+
variable "create_sns_policy" {
8+
description = "Whether to create a policy for SNS permissions or not?"
9+
type = bool
10+
default = true
11+
}
12+
13+
variable "create_sqs_policy" {
14+
description = "Whether to create a policy for SQS permissions or not?"
15+
type = bool
16+
default = true
17+
}
18+
719
variable "bucket" {
820
description = "Name of S3 bucket to use"
921
type = string
@@ -18,18 +30,18 @@ variable "bucket_arn" {
1830

1931
variable "lambda_notifications" {
2032
description = "Map of S3 bucket notifications to Lambda function"
21-
type = any # map(map(any)) is better, but Terraform 0.12.25 panics
33+
type = any
2234
default = {}
2335
}
2436

2537
variable "sqs_notifications" {
2638
description = "Map of S3 bucket notifications to SQS queue"
27-
type = any # map(map(any)) is better, but Terraform 0.12.25 panics
39+
type = any
2840
default = {}
2941
}
3042

3143
variable "sns_notifications" {
3244
description = "Map of S3 bucket notifications to SNS topic"
33-
type = any # map(map(any)) is better, but Terraform 0.12.25 panics
45+
type = any
3446
default = {}
3547
}

modules/notification/versions.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
terraform {
2-
required_version = ">= 0.12.6, < 0.14"
2+
required_version = ">= 0.12.6"
33

44
required_providers {
5-
aws = ">= 3.0, < 4.0"
6-
random = "~> 2"
5+
aws = ">= 3.0"
6+
random = ">= 2.0"
77
}
88
}

versions.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
terraform {
2-
required_version = ">= 0.12.6, < 0.14"
2+
required_version = ">= 0.12.6"
33

44
required_providers {
5-
aws = ">= 3.0, < 4.0"
5+
aws = ">= 3.0"
66
}
77
}

0 commit comments

Comments
 (0)