Skip to content

Commit

Permalink
feat: Add attach_sns_policy (#89)
Browse files Browse the repository at this point in the history
Co-authored-by: Anton Babenko <anton@antonbabenko.com>
  • Loading branch information
kaykhan and antonbabenko authored Jun 15, 2023
1 parent 9b26b0e commit 6e09aa1
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: v1.77.0
rev: v1.81.0
hooks:
- id: terraform_fmt
- id: terraform_validate
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ No modules.
| [aws_iam_policy.kinesis_firehose](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_iam_policy.lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_iam_policy.sfn](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_iam_policy.sns](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_iam_policy.sqs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_iam_policy.tracing](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_iam_policy_attachment.additional_inline](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy_attachment) | resource |
Expand All @@ -402,6 +403,7 @@ No modules.
| [aws_iam_policy_attachment.kinesis_firehose](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy_attachment) | resource |
| [aws_iam_policy_attachment.lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy_attachment) | resource |
| [aws_iam_policy_attachment.sfn](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy_attachment) | resource |
| [aws_iam_policy_attachment.sns](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy_attachment) | resource |
| [aws_iam_policy_attachment.sqs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy_attachment) | resource |
| [aws_iam_policy_attachment.tracing](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy_attachment) | resource |
| [aws_iam_role.eventbridge](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
Expand All @@ -419,6 +421,7 @@ No modules.
| [aws_iam_policy_document.kinesis_firehose](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.sfn](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.sns](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.sqs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |

## Inputs
Expand All @@ -442,6 +445,7 @@ No modules.
| <a name="input_attach_policy_jsons"></a> [attach\_policy\_jsons](#input\_attach\_policy\_jsons) | Controls whether policy\_jsons should be added to IAM role | `bool` | `false` | no |
| <a name="input_attach_policy_statements"></a> [attach\_policy\_statements](#input\_attach\_policy\_statements) | Controls whether policy\_statements should be added to IAM role | `bool` | `false` | no |
| <a name="input_attach_sfn_policy"></a> [attach\_sfn\_policy](#input\_attach\_sfn\_policy) | Controls whether the StepFunction policy should be added to IAM role for EventBridge Target | `bool` | `false` | no |
| <a name="input_attach_sns_policy"></a> [attach\_sns\_policy](#input\_attach\_sns\_policy) | Controls whether the SNS policy should be added to IAM role for EventBridge Target | `bool` | `false` | no |
| <a name="input_attach_sqs_policy"></a> [attach\_sqs\_policy](#input\_attach\_sqs\_policy) | Controls whether the SQS policy should be added to IAM role for EventBridge Target | `bool` | `false` | no |
| <a name="input_attach_tracing_policy"></a> [attach\_tracing\_policy](#input\_attach\_tracing\_policy) | Controls whether X-Ray tracing policy should be added to IAM role for EventBridge | `bool` | `false` | no |
| <a name="input_bus_name"></a> [bus\_name](#input\_bus\_name) | A unique name for your EventBridge Bus | `string` | `"default"` | no |
Expand Down Expand Up @@ -479,6 +483,7 @@ No modules.
| <a name="input_rules"></a> [rules](#input\_rules) | A map of objects with EventBridge Rule definitions. | `map(any)` | `{}` | no |
| <a name="input_schemas_discoverer_description"></a> [schemas\_discoverer\_description](#input\_schemas\_discoverer\_description) | Default schemas discoverer description | `string` | `"Auto schemas discoverer event"` | no |
| <a name="input_sfn_target_arns"></a> [sfn\_target\_arns](#input\_sfn\_target\_arns) | The Amazon Resource Name (ARN) of the StepFunctions you want to use as EventBridge targets | `list(string)` | `[]` | no |
| <a name="input_sns_target_arns"></a> [sns\_target\_arns](#input\_sns\_target\_arns) | The Amazon Resource Name (ARN) of the AWS SNS's you want to use as EventBridge targets | `list(string)` | `[]` | no |
| <a name="input_sqs_target_arns"></a> [sqs\_target\_arns](#input\_sqs\_target\_arns) | The Amazon Resource Name (ARN) of the AWS SQS Queues you want to use as EventBridge targets | `list(string)` | `[]` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | A map of tags to assign to resources. | `map(string)` | `{}` | no |
| <a name="input_targets"></a> [targets](#input\_targets) | A map of objects with EventBridge Target definitions. | `any` | `{}` | no |
Expand Down
1 change: 0 additions & 1 deletion examples/api-gateway-event-source/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ provider "aws" {
region = "ap-southeast-1"

# Make it faster by skipping something
skip_get_ec2_platforms = true
skip_metadata_api_check = true
skip_region_validation = true
skip_credentials_validation = true
Expand Down
1 change: 0 additions & 1 deletion examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ provider "aws" {
region = "ap-southeast-1"

# Make it faster by skipping something
skip_get_ec2_platforms = true
skip_metadata_api_check = true
skip_region_validation = true
skip_credentials_validation = true
Expand Down
1 change: 0 additions & 1 deletion examples/default-bus/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ provider "aws" {
region = "ap-southeast-1"

# Make it faster by skipping something
skip_get_ec2_platforms = true
skip_metadata_api_check = true
skip_region_validation = true
skip_credentials_validation = true
Expand Down
1 change: 0 additions & 1 deletion examples/with-api-destination/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ provider "aws" {
region = "ap-southeast-1"

# Make it faster by skipping something
skip_get_ec2_platforms = true
skip_metadata_api_check = true
skip_region_validation = true
skip_credentials_validation = true
Expand Down
1 change: 0 additions & 1 deletion examples/with-archive/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ provider "aws" {
region = "ap-southeast-1"

# Make it faster by skipping something
skip_get_ec2_platforms = true
skip_metadata_api_check = true
skip_region_validation = true
skip_credentials_validation = true
Expand Down
1 change: 0 additions & 1 deletion examples/with-ecs-scheduling/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ provider "aws" {
region = "ap-southeast-1"

# Make it faster by skipping something
skip_get_ec2_platforms = true
skip_metadata_api_check = true
skip_region_validation = true
skip_credentials_validation = true
Expand Down
1 change: 0 additions & 1 deletion examples/with-lambda-scheduling/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ provider "aws" {
region = "ap-southeast-1"

# Make it faster by skipping something
skip_get_ec2_platforms = true
skip_metadata_api_check = true
skip_region_validation = true
skip_credentials_validation = true
Expand Down
1 change: 0 additions & 1 deletion examples/with-permissions/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ provider "aws" {
region = "ap-southeast-1"

# Make it faster by skipping something
skip_get_ec2_platforms = true
skip_metadata_api_check = true
skip_region_validation = true
skip_credentials_validation = true
Expand Down
45 changes: 45 additions & 0 deletions iam.tf
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,51 @@ resource "aws_iam_policy_attachment" "sqs" {
policy_arn = aws_iam_policy.sqs[0].arn
}

#############
# SNS Config
#############

data "aws_iam_policy_document" "sns" {
count = local.create_role && var.attach_sns_policy ? 1 : 0

statement {
sid = "SNSAccess"
effect = "Allow"
actions = [
"sns:Publish",
]
resources = var.sns_target_arns
}

statement {
sid = "SNSKMSAccess"
effect = "Allow"
actions = [
"kms:Decrypt",
"kms:GenerateDataKey"
]
resources = ["*"]
}

}

resource "aws_iam_policy" "sns" {
count = local.create_role && var.attach_sns_policy ? 1 : 0

name = "${local.role_name}-sns"
policy = data.aws_iam_policy_document.sns[0].json

tags = merge({ Name = "${local.role_name}-sns" }, var.tags)
}

resource "aws_iam_policy_attachment" "sns" {
count = local.create_role && var.attach_sns_policy ? 1 : 0

name = "${local.role_name}-sns"
roles = [aws_iam_role.eventbridge[0].name]
policy_arn = aws_iam_policy.sns[0].arn
}

#############
# ECS Config
#############
Expand Down
12 changes: 12 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ variable "attach_sqs_policy" {
default = false
}

variable "attach_sns_policy" {
description = "Controls whether the SNS policy should be added to IAM role for EventBridge Target"
type = bool
default = false
}

variable "attach_ecs_policy" {
description = "Controls whether the ECS policy should be added to IAM role for EventBridge Target"
type = bool
Expand Down Expand Up @@ -254,6 +260,12 @@ variable "sqs_target_arns" {
default = []
}

variable "sns_target_arns" {
description = "The Amazon Resource Name (ARN) of the AWS SNS's you want to use as EventBridge targets"
type = list(string)
default = []
}

variable "ecs_target_arns" {
description = "The Amazon Resource Name (ARN) of the AWS ECS Tasks you want to use as EventBridge targets"
type = list(string)
Expand Down

0 comments on commit 6e09aa1

Please sign in to comment.