If you've read the output of the terraform apply
command carefully, you might be really curious about what this part means:
The state of your infrastructure has been saved to the path
below. This state is required to modify and destroy your
infrastructure, so keep it safe. To inspect the complete state
use the `terraform show` command.
State path: terraform.tfstate
What it means is that Terraform didn't simply create an instance and forget about it. It actually saved everything it knows about this instance to a special file, named the state
file. In this file, Terraform stores the state
of all the resources it created. This file is saved to the same directory where the Terraform template is, with the .tfstate
extension. The format of the state
file is simple json
. Let's take a look at it piece by piece.
{ "version": 3, "terraform_version": "0.8.2", "serial": 1, "lineage": "65a6dc1b-3f42-4f23-8df1-8b2275602aff",
version
of the state
file. It's not the version of this particular state; it's a version of a format of state
files in general. This allows Terraform to move the format of the state
file forward without breaking compatibility with older versions.terraform_version
key is self-explanatory: it's the version of Terraform that the state
file was created with. If you try to use Terraform 0.7 with a state
file that specifies version 0.8.2, Terraform will not allow you to do so.serial
key is increased every time you update your state
, even with smallest modifications. It is used by Terraform to detect potentially conflicting updates.lineage
key is set only when you create a new state file. After this, the value of lineage
is never updated and is not currently used by Terraform. It is planned to be used in order to reduce mistakes when working with remote state
files, which we will discuss in later chapters.What goes next in state
file are actually resources you've created. Terraform obtains all the information possible about the resource and writes it to state
file:
"modules": [ { "path": [ "root" ], "outputs": {}, "resources": { "aws_instance.hello-instance": { "type": "aws_instance", "depends_on": [], "primary": { "id": "i-06f88fe6a2b4307b8", "attributes": { "ami": "ami-9bf712f4", "availability_zone": "eu-central-1a", "disable_api_termination": "false", "ebs_block_device.#": "0", "ebs_optimized": "false", "ephemeral_block_device.#": "0", "iam_instance_profile": "", "id": "i-06f88fe6a2b4307b8",
You've never specified parameters such as availability zone or disable_api_termination
, and yet Terraform has them in the state
file.
The state
file is what makes Terraform capable of not only creating, but also updating and destroying infrastructure. Terraform knows if the actual state of resources has changed and if parameters in template changed and then it intelligently figures out what the final state should look like and gets your infrastructure to this state.
This makes the state
file so important that you never want to lose it after you create your environment. Losing the state
file means losing control of your environment through Terraform. It can be very frustrating to create a huge test environment, delete the state
file by accident and then delete all resources manually through AWS Management Console.
The state
file was not made to be read by humans. But Terraform has multiple commands that allow you to view and modify the state
file conveniently. The terraform state list
command will list all resources in the state
file:
$> terraform state list aws_instance.hello-instance
The terraform show
command will print a complete state in a human-readable format:
$> terraform show aws_instance.hello-instance: id = i-06f88fe6a2b4307b8 ami = ami-9bf712f4 availability_zone = eu-central-1a disable_api_termination = false ebs_block_device.# = 0 ebs_optimized = false ephemeral_block_device.# = 0 iam_instance_profile = instance_state = running instance_type = t2.micro key_name = monitoring = false ...
If you want to view the details of only one resource, you can use terraform state show path_to_resource
. Running terraform state show aws_instance.hello-instance
will give you all the details about the created instance.
After talking so much about how useful the state
file is, let's finally use Terraform to update the instance.
18.220.11.34