I have a module to create my network infrastructure in AWS, and it is returning a list of security group IDs. After I deploy an aws_instance using that output for the subnet_ids parameter, the next plan says it must replace the instance because of the security group IDs even though the plan isn’t replacing the security groups. Can anyone explain why
security group:
resource "aws_security_group" "dns" {
name = "SG-DNS"
description = "Allow DNS traffic within security group."
vpc_id = aws_vpc.main.id
tags = {
Name = "SG-DNS"
product = var.product
}
lifecycle {
ignore_changes = [
tags
]
}
}
outputs:
output "security_groups" {
value = {
dns = aws_security_group.dns.id
dns = aws_security_group.ssh.id
ssl = aws_security_group.ssl.id
}
}
instance:
resource "aws_instance" "this" {
ami = data.aws_ami.this.id
iam_instance_profile = "base_profile"
instance_type = "t3.large"
root_block_device {
encrypted = true
volume_type = "gp3"
volume_size = "100"
delete_on_termination = true
}
security_groups = [
module.Network.security_groups["dns"],
module.Network.security_groups["ssh"],
module.Network.security_groups["ssl"]
]
...
}
then in the plan
~ security_groups = [ # forces replacement
+ "sg-123456",
+ "sg-234567",
+ "sg-345678",
]