Skip to content

Commit 8cc2877

Browse files
authored
Merge pull request #7 from AndrewFarley/allow-existing-sns
Feature: Allow existing SNS via a variable flag
2 parents 8083aa4 + 6ff8cb3 commit 8cc2877

5 files changed

Lines changed: 79 additions & 6 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@
77

88
# .tfvars files
99
*.tfvars
10+
11+
# Build harness
12+
.build-harness
13+
build-harness/

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ module "es_alarms" {
5656
}
5757
```
5858

59+
You can alternatively have this module not create an SNS incase you have existing ones created elsewhere.
60+
61+
```hcl
62+
module "es_alarms" {
63+
source = "github::https://github.com/dubiety/terraform-aws-elasticsearch-cloudwatch-sns-alarms.git?ref=master"
64+
domain_name = "example"
65+
sns_topic = "arn:aws:sns:us-east-1:123456123456:sns-to-slack" # < Put your full SNS ARN here, if necessary read from var or a resource
66+
create_sns_topic = false
67+
}
68+
```
69+
5970

6071
## Inputs
6172

@@ -80,7 +91,8 @@ module "es_alarms" {
8091
| `monitor_jvm_memory_pressure_too_high` | Enable monitoring of JVM memory pressure is too high | string | `true` | no |
8192
| `monitor_master_cpu_utilization_too_high` | Enable monitoring of CPU utilization of master nodes are too high. Only enable this when dedicated master is enabled | string | `false` | no |
8293
| `monitor_master_jvm_memory_pressure_too_high` | Enable monitoring of JVM memory pressure of master nodes are too high. Only enable this wwhen dedicated master is enabled | string | `false` | no |
83-
| `sns_topic` | SNS topic you want to specify. If leave empty, it will use a prefix and a timestampe appended | string | `""` | no |
94+
| `create_sns_topic` | Will create an SNS topic, if you set this to false you MUST set `sns_topic` to a FULL ARN | string | `true` | no |
95+
| `sns_topic` | SNS topic you want to specify. If leave empty, it will use a prefix and a timestamp appended. If `create_sns_topic` is set to false, this MUST be a FULL ARN | string | `""` | no |
8496
| `sns_topic_postfix` | SNS topic postfix | string | `""` | no |
8597
| `sns_topic_prefix` | SNS topic prefix | string | `""` | no |
8698

examples/use-existing-sns/main.tf

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
### For connecting and provisioning
2+
variable "region" {
3+
default = "us-west-2"
4+
}
5+
6+
provider "aws" {
7+
region = var.region
8+
9+
# Make it faster by skipping something
10+
skip_get_ec2_platforms = true
11+
skip_metadata_api_check = true
12+
skip_region_validation = true
13+
skip_credentials_validation = true
14+
skip_requesting_account_id = true
15+
}
16+
17+
# Create an existing SNS topic (aka, do not create in module)
18+
resource "aws_sns_topic" "this" {
19+
name = "using-existing-sns-topic-test"
20+
}
21+
22+
module "es_alarms" {
23+
source = "../../"
24+
domain_name = "example"
25+
# To use an existing SNS topic, your sns_topic MUST be a full ARN
26+
sns_topic = var.aws_sns_topic.arn
27+
# And you must set this to false
28+
create_sns_topic = false
29+
}
30+
31+
output "es_alarms_sns_topic_arn" {
32+
value = module.es_alarms.sns_topic_arn
33+
}

main.tf

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,44 @@ data "aws_caller_identity" "default" {}
66

77
# Make a topic
88
resource "aws_sns_topic" "default_prefix" {
9-
count = var.sns_topic == "" ? 1 : 0
9+
count = var.sns_topic == "" && var.create_sns_topic == true ? 1 : 0
1010
name_prefix = "${var.sns_topic_prefix}elasticsearch-threshold-alerts${var.sns_topic_postfix}"
1111
}
1212

1313
resource "aws_sns_topic" "default" {
14-
count = var.sns_topic != "" ? 1 : 0
14+
count = var.sns_topic != "" && var.create_sns_topic == true ? 1 : 0
1515
name = "${var.sns_topic_prefix}${var.sns_topic}${var.sns_topic_postfix}"
1616
}
1717

1818
locals {
19-
aws_sns_topic_arn = var.sns_topic == "" ? element(concat(aws_sns_topic.default_prefix.*.arn, list("")), 0) : element(concat(aws_sns_topic.default.*.arn, list("")), 0)
20-
aws_sns_topic_name = var.sns_topic == "" ? element(concat(aws_sns_topic.default_prefix.*.name, list("")), 0) : var.sns_topic
19+
aws_sns_topic_arn = coalesce(
20+
element(
21+
concat(
22+
aws_sns_topic.default_prefix.*.arn,
23+
[""],
24+
),
25+
0,
26+
),
27+
element(
28+
concat(
29+
aws_sns_topic.default.*.arn,
30+
[""],
31+
),
32+
0,
33+
),
34+
var.sns_topic
35+
)
36+
aws_sns_topic_name = element(split(":", local.aws_sns_topic_arn), 5)
2137
}
2238

2339
resource "aws_sns_topic_policy" "default" {
40+
count = var.create_sns_topic == true ? 1 : 0
2441
arn = local.aws_sns_topic_arn
25-
policy = data.aws_iam_policy_document.sns_topic_policy.json
42+
policy = data.aws_iam_policy_document.sns_topic_policy[0].json
2643
}
2744

2845
data "aws_iam_policy_document" "sns_topic_policy" {
46+
count = var.create_sns_topic == true ? 1 : 0
2947
policy_id = "__default_policy_ID"
3048

3149
statement {

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ variable "domain_name" {
33
type = string
44
}
55

6+
variable "create_sns_topic" {
7+
description = "If you don't want to create the SNS topic, set this to false. It will use the sns_topic value directly"
8+
type = bool
9+
default = true
10+
}
11+
612
variable "sns_topic" {
713
description = "SNS topic you want to specify. If leave empty, it will use a prefix and a timestampe appended"
814
type = string

0 commit comments

Comments
 (0)