Skip to content

Commit 6d993aa

Browse files
committed
refactor: main tf
Signed-off-by: thxCode <[email protected]>
1 parent b215a50 commit 6d993aa

File tree

1 file changed

+48
-33
lines changed

1 file changed

+48
-33
lines changed

main.tf

+48-33
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ locals {
99
namespace = join("-", [local.project_name, local.environment_name])
1010

1111
tags = {
12+
"Name" = join("-", [local.namespace, local.resource_name])
13+
1214
"walrus.seal.io/project-id" = local.project_id
1315
"walrus.seal.io/environment-id" = local.environment_id
1416
"walrus.seal.io/resource-id" = local.resource_id
@@ -95,9 +97,10 @@ resource "random_string" "name_suffix" {
9597
}
9698

9799
locals {
98-
name = join("-", [local.resource_name, random_string.name_suffix.result])
99-
fullname = join("-", [local.namespace, local.name])
100-
password = coalesce(var.password, random_password.password.result)
100+
name = join("-", [local.resource_name, random_string.name_suffix.result])
101+
fullname = join("-", [local.namespace, local.name])
102+
description = "Created by Walrus catalog, and provisioned by Terraform."
103+
password = coalesce(var.password, random_password.password.result)
101104

102105
replication_readonly_replicas = var.replication_readonly_replicas == 0 ? 1 : var.replication_readonly_replicas
103106
}
@@ -117,59 +120,67 @@ locals {
117120
# create security group.
118121

119122
resource "aws_security_group" "target" {
120-
name = local.fullname
121-
tags = local.tags
123+
name = local.fullname
124+
description = local.description
125+
tags = local.tags
122126

123127
vpc_id = data.aws_vpc.selected.id
124128
}
125129

126130
resource "aws_security_group_rule" "target" {
131+
description = local.description
132+
127133
security_group_id = aws_security_group.target.id
128134

129135
type = "ingress"
130136
protocol = "tcp"
131137
cidr_blocks = [data.aws_vpc.selected.cidr_block]
132138
from_port = 6379
133139
to_port = 6379
134-
description = "Access Redis from VPC"
135140
}
136141

137142
resource "aws_elasticache_subnet_group" "target" {
138143
name = local.fullname
139-
description = "Elasticache subnet group for ${local.fullname}"
140-
subnet_ids = data.aws_subnets.selected.ids
144+
description = local.description
141145
tags = local.tags
146+
147+
subnet_ids = data.aws_subnets.selected.ids
148+
}
149+
150+
locals {
151+
parameters = merge(
152+
{
153+
"cluster-enabled" = "no"
154+
},
155+
{
156+
for c in(var.engine_parameters != null ? var.engine_parameters : []) : c.name => c.value
157+
if try(c.value != "", false)
158+
}
159+
)
142160
}
143161

144162
resource "aws_elasticache_parameter_group" "target" {
145163
name = local.fullname
146-
description = "Elasticache parameter group for ${local.fullname}"
147-
family = local.version_family_mapping[local.version]
164+
description = local.description
165+
tags = local.tags
166+
167+
family = local.version_family_mapping[local.version]
148168

149169
dynamic "parameter" {
150-
for_each = concat([
151-
{ name = "cluster-enabled", value = "no" }
152-
], var.engine_parameters == null ? [] : var.engine_parameters)
170+
for_each = local.parameters
153171
content {
154-
name = parameter.value.name
155-
value = tostring(parameter.value.value)
172+
name = parameter.key
173+
value = tostring(parameter.value)
156174
}
157175
}
158-
159-
tags = local.tags
160-
161-
# Ignore changes to the description since it will try to recreate the resource
162-
lifecycle {
163-
ignore_changes = [
164-
description,
165-
]
166-
}
167176
}
168177

169178
resource "aws_elasticache_replication_group" "default" {
170-
replication_group_id = local.fullname
171-
description = "Elasticache replication group for ${local.fullname}"
172-
tags = local.tags
179+
description = local.description
180+
tags = local.tags
181+
182+
replication_group_id = local.fullname
183+
173184
multi_az_enabled = local.architecture == "replication"
174185
automatic_failover_enabled = local.architecture == "replication"
175186
subnet_group_name = aws_elasticache_subnet_group.target.name
@@ -195,10 +206,9 @@ resource "aws_elasticache_replication_group" "default" {
195206
resource "aws_service_discovery_service" "primary" {
196207
count = var.infrastructure.domain_suffix != null ? 1 : 0
197208

198-
name = format("%s.%s", (local.architecture == "replication" ? join("-", [
199-
local.name, "primary"
200-
]) : local.name), local.namespace)
201-
force_destroy = true
209+
name = format("%s.%s", (local.architecture == "replication" ? join("-", [local.name, "primary"]) : local.name), local.namespace)
210+
description = local.description
211+
tags = local.tags
202212

203213
dns_config {
204214
namespace_id = data.aws_service_discovery_dns_namespace.selected[0].id
@@ -208,6 +218,8 @@ resource "aws_service_discovery_service" "primary" {
208218
type = "CNAME"
209219
}
210220
}
221+
222+
force_destroy = true
211223
}
212224

213225
resource "aws_service_discovery_instance" "primary" {
@@ -224,8 +236,9 @@ resource "aws_service_discovery_instance" "primary" {
224236
resource "aws_service_discovery_service" "reader" {
225237
count = var.infrastructure.domain_suffix != null && local.architecture == "replication" ? 1 : 0
226238

227-
name = format("%s.%s", join("-", [local.name, "reader"]), local.namespace)
228-
force_destroy = true
239+
name = format("%s.%s", join("-", [local.name, "reader"]), local.namespace)
240+
description = local.description
241+
tags = local.tags
229242

230243
dns_config {
231244
namespace_id = data.aws_service_discovery_dns_namespace.selected[0].id
@@ -235,6 +248,8 @@ resource "aws_service_discovery_service" "reader" {
235248
type = "CNAME"
236249
}
237250
}
251+
252+
force_destroy = true
238253
}
239254

240255
resource "aws_service_discovery_instance" "reader" {

0 commit comments

Comments
 (0)