Commit 5312d974 authored by Brian Murphey's avatar Brian Murphey Committed by GitHub

feat: Creating SNS/SQS policies should be optional (#54)

parent 3c45c8ca
...@@ -84,14 +84,14 @@ module "s3_bucket" { ...@@ -84,14 +84,14 @@ module "s3_bucket" {
| Name | Version | | Name | Version |
|------|---------| |------|---------|
| terraform | >= 0.12.6, < 0.14 | | terraform | >= 0.12.6 |
| aws | >= 3.0, < 4.0 | | aws | >= 3.0 |
## Providers ## Providers
| Name | Version | | Name | Version |
|------|---------| |------|---------|
| aws | >= 3.0, < 4.0 | | aws | >= 3.0 |
## Inputs ## Inputs
......
...@@ -19,18 +19,18 @@ Note that this example may create resources which cost money. Run `terraform des ...@@ -19,18 +19,18 @@ Note that this example may create resources which cost money. Run `terraform des
| Name | Version | | Name | Version |
|------|---------| |------|---------|
| terraform | >= 0.12.6, < 0.14 | | terraform | >= 0.12.6 |
| aws | >= 3.0, < 4.0 | | aws | >= 3.0 |
| null | ~> 2 | | null | >= 2 |
| random | ~> 2 | | random | >= 2 |
## Providers ## Providers
| Name | Version | | Name | Version |
|------|---------| |------|---------|
| aws | >= 3.0, < 4.0 | | aws | >= 3.0 |
| null | ~> 2 | | null | >= 2 |
| random | ~> 2 | | random | >= 2 |
## Inputs ## Inputs
......
...@@ -76,6 +76,26 @@ resource "aws_sqs_queue" "this" { ...@@ -76,6 +76,26 @@ resource "aws_sqs_queue" "this" {
name = "${random_pet.this.id}-${count.index}" name = "${random_pet.this.id}-${count.index}"
} }
# SQS policy created outside of the module
data "aws_iam_policy_document" "sqs_external" {
statement {
effect = "Allow"
actions = ["sqs:SendMessage"]
principals {
type = "Service"
identifiers = ["s3.amazonaws.com"]
}
resources = [aws_sqs_queue.this[0].arn]
}
}
resource "aws_sqs_queue_policy" "allow_external" {
queue_url = aws_sqs_queue.this[0].id
policy = data.aws_iam_policy_document.sqs_external.json
}
module "all_notifications" { module "all_notifications" {
source = "../../modules/notification" source = "../../modules/notification"
...@@ -129,4 +149,6 @@ module "all_notifications" { ...@@ -129,4 +149,6 @@ module "all_notifications" {
} }
} }
# Creation of policy is handled outside of the module
create_sqs_policy = false
} }
terraform { terraform {
required_version = ">= 0.12.6, < 0.14" required_version = ">= 0.12.6"
required_providers { required_providers {
aws = ">= 3.0, < 4.0" aws = ">= 3.0"
random = "~> 2" random = ">= 2"
null = "~> 2" null = ">= 2"
} }
} }
...@@ -21,17 +21,17 @@ Note that this example may create resources which cost money. Run `terraform des ...@@ -21,17 +21,17 @@ Note that this example may create resources which cost money. Run `terraform des
| Name | Version | | Name | Version |
|------|---------| |------|---------|
| terraform | >= 0.12.6, < 0.14 | | terraform | >= 0.12.6 |
| aws | >= 3.0, < 4.0 | | aws | >= 3.0 |
| random | ~> 2 | | random | >= 2.0 |
## Providers ## Providers
| Name | Version | | Name | Version |
|------|---------| |------|---------|
| aws | >= 3.0, < 4.0 | | aws | >= 3.0 |
| aws.replica | >= 3.0, < 4.0 | | aws.replica | >= 3.0 |
| random | ~> 2 | | random | >= 2.0 |
## Inputs ## Inputs
......
terraform { terraform {
required_version = ">= 0.12.6, < 0.14" required_version = ">= 0.12.6"
required_providers { required_providers {
aws = ">= 3.0, < 4.0" aws = ">= 3.0"
random = "~> 2" random = ">= 2.0"
} }
} }
...@@ -7,15 +7,15 @@ Creates S3 bucket notification resource with all supported types of deliveries: ...@@ -7,15 +7,15 @@ Creates S3 bucket notification resource with all supported types of deliveries:
| Name | Version | | Name | Version |
|------|---------| |------|---------|
| terraform | >= 0.12.6, < 0.14 | | terraform | >= 0.12.6 |
| aws | >= 3.0, < 4.0 | | aws | >= 3.0 |
| random | ~> 2 | | random | >= 2.0 |
## Providers ## Providers
| Name | Version | | Name | Version |
|------|---------| |------|---------|
| aws | >= 3.0, < 4.0 | | aws | >= 3.0 |
## Inputs ## Inputs
...@@ -24,6 +24,8 @@ Creates S3 bucket notification resource with all supported types of deliveries: ...@@ -24,6 +24,8 @@ Creates S3 bucket notification resource with all supported types of deliveries:
| bucket | Name of S3 bucket to use | `string` | `""` | no | | bucket | Name of S3 bucket to use | `string` | `""` | no |
| bucket\_arn | ARN of S3 bucket to use in policies | `string` | `null` | no | | bucket\_arn | ARN of S3 bucket to use in policies | `string` | `null` | no |
| create | Whether to create this resource or not? | `bool` | `true` | no | | create | Whether to create this resource or not? | `bool` | `true` | no |
| create\_sns\_policy | Whether to create a policy for SNS permissions or not? | `bool` | `true` | no |
| create\_sqs\_policy | Whether to create a policy for SQS permissions or not? | `bool` | `true` | no |
| lambda\_notifications | Map of S3 bucket notifications to Lambda function | `any` | `{}` | no | | lambda\_notifications | Map of S3 bucket notifications to Lambda function | `any` | `{}` | no |
| sns\_notifications | Map of S3 bucket notifications to SNS topic | `any` | `{}` | no | | sns\_notifications | Map of S3 bucket notifications to SNS topic | `any` | `{}` | no |
| sqs\_notifications | Map of S3 bucket notifications to SQS queue | `any` | `{}` | no | | sqs\_notifications | Map of S3 bucket notifications to SQS queue | `any` | `{}` | no |
......
...@@ -74,7 +74,7 @@ data "aws_arn" "queue" { ...@@ -74,7 +74,7 @@ data "aws_arn" "queue" {
} }
data "aws_iam_policy_document" "sqs" { data "aws_iam_policy_document" "sqs" {
for_each = var.sqs_notifications for_each = var.create_sqs_policy ? var.sqs_notifications : tomap({})
statement { statement {
sid = "AllowSQSS3BucketNotification" sid = "AllowSQSS3BucketNotification"
...@@ -101,7 +101,7 @@ data "aws_iam_policy_document" "sqs" { ...@@ -101,7 +101,7 @@ data "aws_iam_policy_document" "sqs" {
} }
resource "aws_sqs_queue_policy" "allow" { resource "aws_sqs_queue_policy" "allow" {
for_each = var.sqs_notifications for_each = var.create_sqs_policy ? var.sqs_notifications : tomap({})
queue_url = lookup(each.value, "queue_id", lookup(local.queue_ids, each.key, null)) queue_url = lookup(each.value, "queue_id", lookup(local.queue_ids, each.key, null))
policy = data.aws_iam_policy_document.sqs[each.key].json policy = data.aws_iam_policy_document.sqs[each.key].json
...@@ -109,7 +109,7 @@ resource "aws_sqs_queue_policy" "allow" { ...@@ -109,7 +109,7 @@ resource "aws_sqs_queue_policy" "allow" {
# SNS Topic # SNS Topic
data "aws_iam_policy_document" "sns" { data "aws_iam_policy_document" "sns" {
for_each = var.sns_notifications for_each = var.create_sns_policy ? var.sns_notifications : tomap({})
statement { statement {
sid = "AllowSNSS3BucketNotification" sid = "AllowSNSS3BucketNotification"
...@@ -136,7 +136,7 @@ data "aws_iam_policy_document" "sns" { ...@@ -136,7 +136,7 @@ data "aws_iam_policy_document" "sns" {
} }
resource "aws_sns_topic_policy" "allow" { resource "aws_sns_topic_policy" "allow" {
for_each = var.sns_notifications for_each = var.create_sns_policy ? var.sns_notifications : tomap({})
arn = each.value.topic_arn arn = each.value.topic_arn
policy = data.aws_iam_policy_document.sns[each.key].json policy = data.aws_iam_policy_document.sns[each.key].json
......
...@@ -4,6 +4,18 @@ variable "create" { ...@@ -4,6 +4,18 @@ variable "create" {
default = true default = true
} }
variable "create_sns_policy" {
description = "Whether to create a policy for SNS permissions or not?"
type = bool
default = true
}
variable "create_sqs_policy" {
description = "Whether to create a policy for SQS permissions or not?"
type = bool
default = true
}
variable "bucket" { variable "bucket" {
description = "Name of S3 bucket to use" description = "Name of S3 bucket to use"
type = string type = string
...@@ -18,18 +30,18 @@ variable "bucket_arn" { ...@@ -18,18 +30,18 @@ variable "bucket_arn" {
variable "lambda_notifications" { variable "lambda_notifications" {
description = "Map of S3 bucket notifications to Lambda function" description = "Map of S3 bucket notifications to Lambda function"
type = any # map(map(any)) is better, but Terraform 0.12.25 panics type = any
default = {} default = {}
} }
variable "sqs_notifications" { variable "sqs_notifications" {
description = "Map of S3 bucket notifications to SQS queue" description = "Map of S3 bucket notifications to SQS queue"
type = any # map(map(any)) is better, but Terraform 0.12.25 panics type = any
default = {} default = {}
} }
variable "sns_notifications" { variable "sns_notifications" {
description = "Map of S3 bucket notifications to SNS topic" description = "Map of S3 bucket notifications to SNS topic"
type = any # map(map(any)) is better, but Terraform 0.12.25 panics type = any
default = {} default = {}
} }
terraform { terraform {
required_version = ">= 0.12.6, < 0.14" required_version = ">= 0.12.6"
required_providers { required_providers {
aws = ">= 3.0, < 4.0" aws = ">= 3.0"
random = "~> 2" random = ">= 2.0"
} }
} }
terraform { terraform {
required_version = ">= 0.12.6, < 0.14" required_version = ">= 0.12.6"
required_providers { required_providers {
aws = ">= 3.0, < 4.0" aws = ">= 3.0"
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment