Skip to content

Commit db0b088

Browse files
committed
refactor: skip vpc dns validation if no domain suffix
Signed-off-by: thxCode <thxcode0824@gmail.com>
1 parent 8d564be commit db0b088

File tree

1 file changed

+43
-36
lines changed

1 file changed

+43
-36
lines changed

main.tf

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ data "aws_vpc" "selected" {
3434

3535
lifecycle {
3636
postcondition {
37-
condition = self.enable_dns_support
38-
error_message = "VPC needs to enable DNS support"
37+
condition = try(var.infrastructure.domain_suffix == null, false) || (self.enable_dns_support && self.enable_dns_hostnames)
38+
error_message = "VPC needs to enable DNS support and DNS hostnames resolution"
3939
}
4040
}
4141
}
@@ -68,7 +68,7 @@ data "aws_kms_key" "selected" {
6868
}
6969

7070
data "aws_service_discovery_dns_namespace" "selected" {
71-
count = var.infrastructure.domain_suffix != null ? 1 : 0
71+
count = try(var.infrastructure.domain_suffix != null, false) ? 1 : 0
7272

7373
name = var.infrastructure.domain_suffix
7474
type = "DNS_PRIVATE"
@@ -110,45 +110,14 @@ locals {
110110
# Deployment
111111
#
112112

113+
# create parameters group.
114+
113115
locals {
114116
version = coalesce(var.engine_version == "6.0" ? "6.x" : var.engine_version, "7.0")
115117
version_family_map = {
116118
"6.x" = "redis6.x",
117119
"7.0" = "redis7",
118120
}
119-
publicly_accessible = try(var.infrastructure.publicly_accessible, false)
120-
}
121-
122-
# create security group.
123-
124-
resource "aws_security_group" "target" {
125-
name = local.fullname
126-
description = local.description
127-
tags = local.tags
128-
129-
vpc_id = data.aws_vpc.selected.id
130-
}
131-
132-
resource "aws_security_group_rule" "target" {
133-
description = local.description
134-
135-
security_group_id = aws_security_group.target.id
136-
type = "ingress"
137-
protocol = "tcp"
138-
cidr_blocks = local.publicly_accessible ? ["0.0.0.0/0", data.aws_vpc.selected.cidr_block] : [data.aws_vpc.selected.cidr_block]
139-
from_port = 6379
140-
to_port = 6379
141-
}
142-
143-
resource "aws_elasticache_subnet_group" "target" {
144-
name = local.fullname
145-
description = local.description
146-
tags = local.tags
147-
148-
subnet_ids = data.aws_subnets.selected.ids
149-
}
150-
151-
locals {
152121
parameters = merge(
153122
{
154123
"cluster-enabled" = "no"
@@ -158,6 +127,7 @@ locals {
158127
if try(c.value != "", false)
159128
}
160129
)
130+
publicly_accessible = try(var.infrastructure.publicly_accessible, false)
161131
}
162132

163133
resource "aws_elasticache_parameter_group" "target" {
@@ -176,6 +146,39 @@ resource "aws_elasticache_parameter_group" "target" {
176146
}
177147
}
178148

149+
# create subnet group
150+
151+
resource "aws_elasticache_subnet_group" "target" {
152+
name = local.fullname
153+
description = local.description
154+
tags = local.tags
155+
156+
subnet_ids = data.aws_subnets.selected.ids
157+
}
158+
159+
# create security group.
160+
161+
resource "aws_security_group" "target" {
162+
name = local.fullname
163+
description = local.description
164+
tags = local.tags
165+
166+
vpc_id = data.aws_vpc.selected.id
167+
}
168+
169+
resource "aws_security_group_rule" "target" {
170+
description = local.description
171+
172+
security_group_id = aws_security_group.target.id
173+
type = "ingress"
174+
protocol = "tcp"
175+
cidr_blocks = local.publicly_accessible ? ["0.0.0.0/0", data.aws_vpc.selected.cidr_block] : [data.aws_vpc.selected.cidr_block]
176+
from_port = 6379
177+
to_port = 6379
178+
}
179+
180+
# create group.
181+
179182
resource "aws_elasticache_replication_group" "default" {
180183
description = local.description
181184
tags = local.tags
@@ -205,6 +208,10 @@ resource "aws_elasticache_replication_group" "default" {
205208
snapshot_retention_limit = 5
206209
}
207210

211+
#
212+
# Exposing
213+
#
214+
208215
resource "aws_service_discovery_service" "primary" {
209216
count = var.infrastructure.domain_suffix != null ? 1 : 0
210217

0 commit comments

Comments
 (0)