Skip to content

Commit a815510

Browse files
committed
feat(mqtt): add mapache user + drop user_data from ignore_changes
Two coupled changes: 1. New mqtt_user_mapache (default "mapache") with a 32-char random_password, written as a third line in /etc/nanomq_pwd.conf via user-data. Intended for the mapache services fleet beyond gr26 — query, foreman, any future MQTT publishers. Keeps the credential distinct from gr26 so it can rotate independently of CAN ingest. 2. Drop user_data from lifecycle.ignore_changes (keep ami). The old guard meant adding users, ACL tweaks, or any user-data edit required an explicit `terraform taint` / `-replace` to actually deploy — easy to forget, and we've already hit the trap twice. nanomq carries no persistent state, so a ~90s replacement on legitimate config changes is acceptable; paho clients auto-reconnect. Postgres + ClickHouse modules keep user_data ignored because they own data volumes. Side effect: the apply that lands this PR will replace the running gr-mqtt instance (new user-data → instance recreate). EIP, SG, and all three random_password values survive in state.
1 parent 67798bb commit a815510

5 files changed

Lines changed: 46 additions & 9 deletions

File tree

infra/environments/prod/outputs.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ output "mqtt_password_tcm26" {
6565
sensitive = true
6666
}
6767

68+
output "mqtt_password_mapache" {
69+
description = "Generated MQTT password for the mapache services fleet user. Read with `terraform output -raw mqtt_password_mapache` → mapache-secrets/MQTT_MAPACHE_PASSWORD or wherever the consumer reads it."
70+
value = module.mqtt.mqtt_password_mapache
71+
sensitive = true
72+
}
73+
6874
output "clickhouse_private_ip" {
6975
description = "Private IP of the ClickHouse EC2. In-cluster pods connect to this on 8123 (HTTP) / 9000 (native)."
7076
value = module.clickhouse.private_ip

infra/modules/mqtt-ec2/main.tf

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ resource "random_password" "mqtt_tcm26" {
4949
special = false
5050
}
5151

52+
# Credential for mapache services (the in-cluster Go services beyond gr26
53+
# — e.g. query, foreman, future publishers). Distinct from gr26's own
54+
# user so the service-fleet credential can rotate independently of the
55+
# CAN-ingest pipeline.
56+
resource "random_password" "mqtt_mapache" {
57+
length = 32
58+
special = false
59+
}
60+
5261
resource "aws_security_group" "this" {
5362
name = var.name
5463
description = "NanoMQ for ${var.name}"
@@ -107,18 +116,22 @@ resource "aws_instance" "this" {
107116
}
108117

109118
user_data = templatefile("${path.module}/user-data.sh.tftpl", {
110-
nanomq_version = var.nanomq_version
111-
mqtt_user = var.mqtt_user
112-
mqtt_password = random_password.mqtt.result
113-
mqtt_user_tcm26 = var.mqtt_user_tcm26
114-
mqtt_password_tcm26 = random_password.mqtt_tcm26.result
119+
nanomq_version = var.nanomq_version
120+
mqtt_user = var.mqtt_user
121+
mqtt_password = random_password.mqtt.result
122+
mqtt_user_tcm26 = var.mqtt_user_tcm26
123+
mqtt_password_tcm26 = random_password.mqtt_tcm26.result
124+
mqtt_user_mapache = var.mqtt_user_mapache
125+
mqtt_password_mapache = random_password.mqtt_mapache.result
115126
})
116127

117-
# Don't recycle the instance on user-data churn. nanomq carries no
118-
# persistent state we care about across replacements, so AMI bumps
119-
# are also benign — `terraform taint` to intentionally replace.
128+
# user_data is intentionally NOT in ignore_changes: nanomq carries no
129+
# persistent state, so legitimate config edits (new user, ACL change)
130+
# should flow through a normal `terraform apply` and trigger the ~90s
131+
# broker downtime willingly. Keeping `ami` ignored so unrelated AL2023
132+
# AMI churn doesn't silently roll the instance.
120133
lifecycle {
121-
ignore_changes = [user_data, ami]
134+
ignore_changes = [ami]
122135
}
123136

124137
tags = {

infra/modules/mqtt-ec2/outputs.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,14 @@ output "mqtt_password_tcm26" {
4444
value = random_password.mqtt_tcm26.result
4545
sensitive = true
4646
}
47+
48+
output "mqtt_user_mapache" {
49+
description = "MQTT username for mapache services beyond gr26. Pair with mqtt_password_mapache."
50+
value = var.mqtt_user_mapache
51+
}
52+
53+
output "mqtt_password_mapache" {
54+
description = "Generated MQTT password for mapache services. Read via `terraform output -raw mqtt_password_mapache` and put into the relevant k8s Secret key."
55+
value = random_password.mqtt_mapache.result
56+
sensitive = true
57+
}

infra/modules/mqtt-ec2/user-data.sh.tftpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ EOF
2525
cat >/etc/nanomq_pwd.conf <<EOF
2626
"${mqtt_user}": "${mqtt_password}"
2727
"${mqtt_user_tcm26}": "${mqtt_password_tcm26}"
28+
"${mqtt_user_mapache}": "${mqtt_password_mapache}"
2829
EOF
2930
chmod 600 /etc/nanomq_pwd.conf
3031

infra/modules/mqtt-ec2/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ variable "mqtt_user_tcm26" {
4242
default = "tcm26"
4343
}
4444

45+
variable "mqtt_user_mapache" {
46+
description = "MQTT username for mapache services beyond gr26 (query, foreman, future publishers). Paired with the module-generated mqtt_password_mapache. Distinct from mqtt_user so the service-fleet credential can rotate independently of the CAN-ingest pipeline."
47+
type = string
48+
default = "mapache"
49+
}
50+
4551
variable "allowed_security_group_ids" {
4652
description = "Security group IDs allowed to connect on port 1883. Typically the EKS node SG."
4753
type = list(string)

0 commit comments

Comments
 (0)