Hi,
I’m learning Terraform and was wondering are there best practices/recommendations for refreshing state after applying some changes?
For example, I have an example configuration that spins up a small cluster of EC2 instances and there are all the usual parts: VPC, sec. group, default routing table, Internet gateway, instances and EIPs associated with them, keypair to SSH into instances, etc.
Executing terraform apply
for the first time works fine, everything is up and configured, all is well. However, if I run terraform plan
without making any changes to the config, there will be configuration drift detected.
For example, I use aws_instance
and aws_eip
resources to spin up instances and attach EIPs to them. Running terraform plan
right after the initial terraform apply
, I’ll get something like:
~ resource "aws_instance" "node" {
~ associate_public_ip_address = false -> true
id = "i-XXXXXXXXXXXXXX"
+ public_ip = "X.Y.Z.W"
tags = {
"Name" = "example"
}
# (26 unchanged attributes hidden)
# (5 unchanged blocks hidden)
}
Some resources will report some optional attributes to have changed (with default values). For example, aws_key_pair
and aws_default_route_table
:
~ resource "aws_key_pair" "deployer" {
id = "deployer key"
+ tags = {}
# (6 unchanged attributes hidden)
}
# module.discovery.module.network.aws_default_route_table.discovery has changed
~ resource "aws_default_route_table" "example" {
id = "rtb-XXXXXXXXXXXXXXXXXX"
+ propagating_vgws = []
tags = {
"Name" = "test route table"
}
# (6 unchanged attributes hidden)
}
Other examples are icmp_code
and icmp_type
attributes on aws_default_network_acl.ingress/egress
. I can work around this by specifying the defaults in my configuration, but it feels a bit silly having to specify some optional attributes with their defaults just to achieve state idempotency.
I understand that some resources’ states change after applying other bits of configuration (e.g. aws_instance
and aws_eip
), but would it make sense for terraform
to automatically refresh state after running apply
in order for the state file to reflect actual post-apply state? Or are there reasons for not doing this?
Thank you!