Refreshing infrastructure

It could be that someone updated the resource managed by Terraform manually, via AWS API or AWS Management Console. If changed attribute is specified inside the template, then you can fix it by running the terraform apply command--given that Terraform templates are the source of truth when it comes to the state of the infrastructure. But if this attribute is not managed by Terraform template, then you might still want it to be reflected in your state file. That's when the terraform refresh command comes to the rescue.

Apply the template and then go to AWS Console. Open the VPC section and find the VPC Terraform created. Right-click on this VPC and choose Edit DNS Hostnames. Then change DNS Hostnames to Yes:

Refreshing infrastructure

Now head back to your console and execute the terraform refresh command. Open the terraform.tfstate file and verify that the change in the state file happened as expected:

 "aws_vpc.my_vpc": { 
                    "type": "aws_vpc", 
                    "depends_on": [], 
                    "primary": { 
                        "id": "vpc-1d04fc75", 
                        "attributes": { 
                            ... 
                            "enable_dns_hostnames": "true", 

Periodically running the terraform refresh command might be a good idea to ensure that state file and actual infrastructure are in sync. But what if the resource was never part of the Terraform template?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.122.162