Why does Terraform keep on recreating an instance due to a security group? What I basically have at the moment is a module that provisions the instance and security group in it, but every time I run everything even without any changes, the instance keeps getting replaced.
Sometimes that happens if you add the sg rules in the sg resource. Add the rules outside the group
vpc_security_group_ids
should be used instead of security_groups
Using security_groups
always forces a recreation even though there’s not a single change at all
All good now. Using the vpc_security_group_ids
was the solution.
security_groups
causes your instance to get recreated every time unless you’re on EC2-Classic or on default VPC