Skip to content

Commit 243be7b

Browse files
authored
Add sns-fifo-topic and sns-standard-topic modules (#9)
1 parent 928c3f4 commit 243be7b

21 files changed

+1096
-1
lines changed

.github/labeler.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
- modules/eventbridge-event-bus/**/*
44
":floppy_disk: msk-cluster":
55
- modules/msk-cluster/**/*
6+
":floppy_disk: sns-fifo-topic":
7+
- modules/sns-fifo-topic/**/*
8+
":floppy_disk: sns-standard-topic":
9+
- modules/sns-standard-topic/**/*

.github/labels.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,9 @@
4646
- color: "fbca04"
4747
description: "This issue or pull request is related to msk-cluster module."
4848
name: ":floppy_disk: msk-cluster"
49+
- color: "fbca04"
50+
description: "This issue or pull request is related to sns-fifo-topic module."
51+
name: ":floppy_disk: sns-fifo-topic"
52+
- color: "fbca04"
53+
description: "This issue or pull request is related to sns-standard-topic module."
54+
name: ":floppy_disk: sns-standard-topic"

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Terraform module which creates messaging related resources on AWS.
88

99
- [eventbridge-event-bus](./modules/eventbridge-event-bus)
1010
- [msk-cluster](./modules/msk-cluster)
11+
- [sns-fifo-topic](./modules/sns-fifo-topic)
12+
- [sns-standard-topic](./modules/sns-standard-topic)
1113

1214

1315
## Target AWS Services
@@ -19,11 +21,19 @@ Terraform Modules from [this package](https://github.com/tedilabs/terraform-aws-
1921
- **AWS MSK (Managed Streaming for Apache Kafka)**
2022
- Cluster
2123
- **AWS SNS (Simple Notification Service)**
22-
- Comming Soon!
24+
- FIFO Topic
25+
- Standard Topic
2326
- **AWS SQS (Simple Queue Service)**
2427
- Comming Soon!
2528

2629

30+
## Examples
31+
32+
### SNS (Simple Notification Service)
33+
34+
- [sns-standard-topic-email-subscription](./examples/sns-standard-topic-email-subscription)
35+
36+
2737
## Self Promotion
2838

2939
Like this project? Follow the repository on [GitHub](https://github.com/tedilabs/terraform-aws-messaging). And if you're feeling especially charitable, follow **[posquit0](https://github.com/posquit0)** on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
provider "aws" {
2+
region = "us-east-1"
3+
}
4+
5+
6+
###################################################
7+
# SNS Topic
8+
###################################################
9+
10+
module "topic" {
11+
source = "../../modules/sns-standard-topic"
12+
# source = "tedilabs/messaging/aws//modules/sns-standard-topic"
13+
# version = "~> 0.2.0"
14+
15+
name = "standard-test-email"
16+
display_name = "Standard Test Email"
17+
18+
subscriptions_by_email = [
19+
{
20+
email = "admin@example.com"
21+
},
22+
{
23+
email = "admin+dlq@example.com"
24+
redrive_policy = {
25+
dead_letter_sqs_queue = "arn:aws:sqs:us-east-1:123456789123:test"
26+
}
27+
},
28+
{
29+
email = "admin+filter@example.com"
30+
filter_policy = {
31+
enabled = true
32+
scope = "ATTRIBUTES"
33+
policy = jsonencode({
34+
"store" = ["example_corp"]
35+
})
36+
}
37+
},
38+
]
39+
subscriptions_by_email_json = [
40+
{
41+
email = "admin@example.com"
42+
},
43+
{
44+
email = "admin+dlq@example.com"
45+
redrive_policy = {
46+
dead_letter_sqs_queue = "arn:aws:sqs:us-east-1:123456789123:test"
47+
}
48+
},
49+
{
50+
email = "admin+filter@example.com"
51+
filter_policy = {
52+
enabled = true
53+
scope = "ATTRIBUTES"
54+
policy = jsonencode({
55+
"store" = ["example_corp"]
56+
})
57+
}
58+
},
59+
]
60+
61+
tags = {
62+
"project" = "terraform-aws-messaging-examples"
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "topic" {
2+
description = "The SNS topic."
3+
value = module.topic
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = "~> 1.5"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = "~> 5.0"
8+
}
9+
}
10+
}

modules/sns-fifo-topic/README.md

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# sns-fifo-topic
2+
3+
This module creates following resources.
4+
5+
- `aws_sns_topic`
6+
- `aws_sns_topic_policy` (optional)
7+
- `aws_sns_topic_subscription` (optional)
8+
9+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
10+
## Requirements
11+
12+
| Name | Version |
13+
|------|---------|
14+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.5 |
15+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.5 |
16+
17+
## Providers
18+
19+
| Name | Version |
20+
|------|---------|
21+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.19.0 |
22+
23+
## Modules
24+
25+
| Name | Source | Version |
26+
|------|--------|---------|
27+
| <a name="module_resource_group"></a> [resource\_group](#module\_resource\_group) | tedilabs/misc/aws//modules/resource-group | ~> 0.10.0 |
28+
29+
## Resources
30+
31+
| Name | Type |
32+
|------|------|
33+
| [aws_sns_topic.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic) | resource |
34+
| [aws_sns_topic_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic_policy) | resource |
35+
| [aws_iam_policy_document.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
36+
37+
## Inputs
38+
39+
| Name | Description | Type | Default | Required |
40+
|------|-------------|------|---------|:--------:|
41+
| <a name="input_name"></a> [name](#input\_name) | (Required) The name of the SNS topic. Topic names must be made up of only uppercase and lowercase ASCII letters, numbers, underscores, and hyphens, and must be between 1 and 256 characters long. For a FIFO (first-in-first-out) topic, the name must end with the `.fifo` suffix. | `string` | n/a | yes |
42+
| <a name="input_content_based_deduplication"></a> [content\_based\_deduplication](#input\_content\_based\_deduplication) | (Optional) Whether to enable default message deduplication based on message content. If set to `false`, a deduplication ID must be provided for every publish request. | `bool` | `false` | no |
43+
| <a name="input_display_name"></a> [display\_name](#input\_display\_name) | (Optional) The display name to use for a topic with SMS subscriptions. | `string` | `""` | no |
44+
| <a name="input_encryption_at_rest"></a> [encryption\_at\_rest](#input\_encryption\_at\_rest) | (Optional) A configuration to encrypt at rest in the SNS topic. Amazon SNS provides in-transit encryption by default. Enabling server-side encryption adds at-rest encryption to your topic. Amazon SNS encrypts your message as soon as it is received. The message is decrypted immediately prior to delivery. `encryption_at_rest` as defined below.<br> (Optional) `enabled` - Whether to enable encryption at rest. Defaults to `false`.<br> (Optional) `kms_key` - The ID of AWS KMS CMK (Customer Master Key) used for the encryption. | <pre>object({<br> enabled = optional(bool, false)<br> kms_key = optional(string)<br> })</pre> | `{}` | no |
45+
| <a name="input_module_tags_enabled"></a> [module\_tags\_enabled](#input\_module\_tags\_enabled) | (Optional) Whether to create AWS Resource Tags for the module informations. | `bool` | `true` | no |
46+
| <a name="input_policy"></a> [policy](#input\_policy) | (Optional) A valid policy JSON document. The resource-based policy defines who can publish or subscribe to the SNS topic. | `string` | `null` | no |
47+
| <a name="input_resource_group_description"></a> [resource\_group\_description](#input\_resource\_group\_description) | (Optional) The description of Resource Group. | `string` | `"Managed by Terraform."` | no |
48+
| <a name="input_resource_group_enabled"></a> [resource\_group\_enabled](#input\_resource\_group\_enabled) | (Optional) Whether to create Resource Group to find and group AWS resources which are created by this module. | `bool` | `true` | no |
49+
| <a name="input_resource_group_name"></a> [resource\_group\_name](#input\_resource\_group\_name) | (Optional) The name of Resource Group. A Resource Group name can have a maximum of 127 characters, including letters, numbers, hyphens, dots, and underscores. The name cannot start with `AWS` or `aws`. | `string` | `""` | no |
50+
| <a name="input_signature_version"></a> [signature\_version](#input\_signature\_version) | (Optional) The signature version corresponds to the hashing algorithm used while creating the signature of the notifications, subscription confirmations, or unsubscribe confirmation messages sent by Amazon SNS. Defaults to `1`. | `number` | `1` | no |
51+
| <a name="input_tags"></a> [tags](#input\_tags) | (Optional) A map of tags to add to all resources. | `map(string)` | `{}` | no |
52+
| <a name="input_xray_tracing_enabled"></a> [xray\_tracing\_enabled](#input\_xray\_tracing\_enabled) | (Optional) Whether to activate AWS X-Ray Active Tracing mode for the SNS topic. If set to Active, Amazon SNS will vend X-Ray segment data to topic owner account if the sampled flag in the tracing header is true. Defaults to `false`, and the topic passes through the tracing header it receives from an Amazon SNS publisher to its subscriptions. | `bool` | `false` | no |
53+
54+
## Outputs
55+
56+
| Name | Description |
57+
|------|-------------|
58+
| <a name="output_arn"></a> [arn](#output\_arn) | The ARN of the SNS topic. |
59+
| <a name="output_content_based_deduplication"></a> [content\_based\_deduplication](#output\_content\_based\_deduplication) | Whether to enable default message deduplication based on message content. |
60+
| <a name="output_display_name"></a> [display\_name](#output\_display\_name) | The display name for a topic with SMS subscriptions. |
61+
| <a name="output_encryption_at_rest"></a> [encryption\_at\_rest](#output\_encryption\_at\_rest) | A configuration to encrypt at rest in the SNS topic. |
62+
| <a name="output_id"></a> [id](#output\_id) | The ID of the SNS topic. |
63+
| <a name="output_name"></a> [name](#output\_name) | The name for the SNS topic. |
64+
| <a name="output_owner"></a> [owner](#output\_owner) | The AWS Account ID of the SNS topic owner. |
65+
| <a name="output_signature_version"></a> [signature\_version](#output\_signature\_version) | The signature version corresponds to the hashing algorithm used while creating the signature of the notifications, subscription confirmations, or unsubscribe confirmation messages sent by Amazon SNS. |
66+
| <a name="output_type"></a> [type](#output\_type) | The type of the SNS topic. |
67+
| <a name="output_xray_tracing_enabled"></a> [xray\_tracing\_enabled](#output\_xray\_tracing\_enabled) | Whether to activate AWS X-Ray Active Tracing mode for the SNS topic. |
68+
| <a name="output_z"></a> [z](#output\_z) | The list of log streams for the log group. |
69+
| <a name="output_zz"></a> [zz](#output\_zz) | The list of log streams for the log group. |
70+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

modules/sns-fifo-topic/main.tf

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
locals {
2+
metadata = {
3+
package = "terraform-aws-messaging"
4+
version = trimspace(file("${path.module}/../../VERSION"))
5+
module = basename(path.module)
6+
name = var.name
7+
}
8+
module_tags = var.module_tags_enabled ? {
9+
"module.terraform.io/package" = local.metadata.package
10+
"module.terraform.io/version" = local.metadata.version
11+
"module.terraform.io/name" = local.metadata.module
12+
"module.terraform.io/full-name" = "${local.metadata.package}/${local.metadata.module}"
13+
"module.terraform.io/instance" = local.metadata.name
14+
} : {}
15+
}
16+
17+
18+
###################################################
19+
# SNS Topic
20+
###################################################
21+
22+
# INFO: Not supported attributes
23+
# - `name_prefix`
24+
# - `delivery_policy`
25+
resource "aws_sns_topic" "this" {
26+
name = var.name
27+
display_name = var.display_name
28+
fifo_topic = true
29+
30+
content_based_deduplication = var.content_based_deduplication
31+
32+
33+
## Observability
34+
tracing_config = (var.xray_tracing_enabled
35+
? "Active"
36+
: "PassThrough"
37+
)
38+
39+
40+
## Encryption
41+
signature_version = var.signature_version
42+
kms_master_key_id = (var.encryption_at_rest.enabled
43+
? var.encryption_at_rest.kms_key
44+
: null
45+
)
46+
47+
# application_success_feedback_role_arn - (Optional) The IAM role permitted to receive success feedback for this topic
48+
# application_success_feedback_sample_rate - (Optional) Percentage of success to sample
49+
# application_failure_feedback_role_arn - (Optional) IAM role for failure feedback
50+
51+
# http_success_feedback_role_arn - (Optional) The IAM role permitted to receive success feedback for this topic
52+
# http_success_feedback_sample_rate - (Optional) Percentage of success to sample
53+
# http_failure_feedback_role_arn - (Optional) IAM role for failure feedback
54+
55+
# lambda_success_feedback_role_arn - (Optional) The IAM role permitted to receive success feedback for this topic
56+
# lambda_success_feedback_sample_rate - (Optional) Percentage of success to sample
57+
# lambda_failure_feedback_role_arn - (Optional) IAM role for failure feedback
58+
59+
# sqs_success_feedback_role_arn - (Optional) The IAM role permitted to receive success feedback for this topic
60+
# sqs_success_feedback_sample_rate - (Optional) Percentage of success to sample
61+
# sqs_failure_feedback_role_arn - (Optional) IAM role for failure feedback
62+
63+
# firehose_success_feedback_role_arn - (Optional) The IAM role permitted to receive success feedback for this topic
64+
# firehose_success_feedback_sample_rate - (Optional) Percentage of success to sample
65+
# firehose_failure_feedback_role_arn
66+
67+
tags = merge(
68+
{
69+
"Name" = local.metadata.name
70+
},
71+
local.module_tags,
72+
var.tags,
73+
)
74+
}

modules/sns-fifo-topic/outputs.tf

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
output "arn" {
2+
description = "The ARN of the SNS topic."
3+
value = aws_sns_topic.this.arn
4+
}
5+
6+
output "id" {
7+
description = "The ID of the SNS topic."
8+
value = aws_sns_topic.this.id
9+
}
10+
11+
output "owner" {
12+
description = "The AWS Account ID of the SNS topic owner."
13+
value = aws_sns_topic.this.owner
14+
}
15+
16+
output "name" {
17+
description = "The name for the SNS topic."
18+
value = aws_sns_topic.this.name
19+
}
20+
21+
output "display_name" {
22+
description = "The display name for a topic with SMS subscriptions."
23+
value = aws_sns_topic.this.display_name
24+
}
25+
26+
output "type" {
27+
description = "The type of the SNS topic."
28+
value = "FIFO"
29+
}
30+
31+
output "content_based_deduplication" {
32+
description = "Whether to enable default message deduplication based on message content."
33+
value = aws_sns_topic.this.content_based_deduplication
34+
}
35+
36+
output "xray_tracing_enabled" {
37+
description = "Whether to activate AWS X-Ray Active Tracing mode for the SNS topic."
38+
value = aws_sns_topic.this.tracing_config == "Active"
39+
}
40+
41+
output "signature_version" {
42+
description = "The signature version corresponds to the hashing algorithm used while creating the signature of the notifications, subscription confirmations, or unsubscribe confirmation messages sent by Amazon SNS."
43+
value = aws_sns_topic.this.signature_version
44+
}
45+
46+
output "encryption_at_rest" {
47+
description = "A configuration to encrypt at rest in the SNS topic."
48+
value = {
49+
enabled = var.encryption_at_rest.enabled
50+
kms_key = aws_sns_topic.this.kms_master_key_id
51+
}
52+
}
53+
54+
output "z" {
55+
description = "The list of log streams for the log group."
56+
value = {
57+
for k, v in aws_sns_topic.this :
58+
k => v
59+
if !contains(["id", "arn", "name", "name_prefix", "display_name", "owner", "tags", "tags_all", "signature_version", "kms_master_key_id", "tracing_config", "content_based_deduplication", "fifo_topic"], k)
60+
}
61+
}
62+
63+
output "zz" {
64+
description = "The list of log streams for the log group."
65+
value = {
66+
policy = aws_sns_topic_policy.this
67+
}
68+
}

modules/sns-fifo-topic/policies.tf

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
###################################################
2+
# Policy for SNS Topic
3+
###################################################
4+
5+
data "aws_iam_policy_document" "this" {
6+
source_policy_documents = concat(
7+
[]
8+
)
9+
override_policy_documents = var.policy != null ? [var.policy] : null
10+
}
11+
12+
resource "aws_sns_topic_policy" "this" {
13+
count = var.policy != null ? 1 : 0
14+
15+
arn = aws_sns_topic.this.arn
16+
policy = data.aws_iam_policy_document.this.json
17+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
locals {
2+
resource_group_name = (var.resource_group_name != ""
3+
? var.resource_group_name
4+
: join(".", [
5+
local.metadata.package,
6+
local.metadata.module,
7+
replace(local.metadata.name, "/[^a-zA-Z0-9_\\.-]/", "-"),
8+
])
9+
)
10+
}
11+
12+
13+
module "resource_group" {
14+
source = "tedilabs/misc/aws//modules/resource-group"
15+
version = "~> 0.10.0"
16+
17+
count = (var.resource_group_enabled && var.module_tags_enabled) ? 1 : 0
18+
19+
name = local.resource_group_name
20+
description = var.resource_group_description
21+
22+
query = {
23+
resource_tags = local.module_tags
24+
}
25+
26+
module_tags_enabled = false
27+
tags = merge(
28+
local.module_tags,
29+
var.tags,
30+
)
31+
}

0 commit comments

Comments
 (0)