From 9377bf9247c96318b99273eb2978d1afce8cf0eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Doriann=20Corlou=C3=ABr?= Date: Thu, 20 Jun 2024 19:50:49 +0900 Subject: [PATCH] Name tag takes precedence over Name variable (#26) --- asg.tf | 12 ++++++++---- ec2.tf | 4 +--- main.tf | 8 ++------ 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/asg.tf b/asg.tf index 641dea1..1cb53b9 100644 --- a/asg.tf +++ b/asg.tf @@ -13,10 +13,14 @@ resource "aws_autoscaling_group" "main" { version = "$Latest" } - tag { - key = "Name" - value = var.name - propagate_at_launch = true + dynamic "tag" { + for_each = lookup(var.tags, "Name", null) == null ? ["Name"] : [] + + content { + key = "Name" + value = var.name + propagate_at_launch = true + } } dynamic "tag" { diff --git a/ec2.tf b/ec2.tf index e4039a4..66b42ab 100644 --- a/ec2.tf +++ b/ec2.tf @@ -74,9 +74,7 @@ resource "aws_launch_template" "main" { content { resource_type = tag_specifications.value - tags = merge(var.tags, { - Name = var.name - }) + tags = merge({ Name = var.name }, var.tags) } } diff --git a/main.tf b/main.tf index c294986..50628fb 100644 --- a/main.tf +++ b/main.tf @@ -49,9 +49,7 @@ resource "aws_security_group" "main" { ipv6_cidr_blocks = ["::/0"] } - tags = merge(var.tags, { - Name = var.name - }) + tags = merge({ Name = var.name }, var.tags) } resource "aws_network_interface" "main" { @@ -60,9 +58,7 @@ resource "aws_network_interface" "main" { security_groups = [aws_security_group.main.id] source_dest_check = false - tags = merge(var.tags, { - Name = var.name - }) + tags = merge({ Name = var.name }, var.tags) } resource "aws_route" "main" {