Terraform States

What are Terraform states?

Why does Terraform need states?

States in action

cd tf-blog 
terraform init
. . .
. . .
Plan: 3 to add, 0 to change, 0 to destroy.

Changes to Outputs:
+ instance_id = [
+ (known after apply),
+ (known after apply),
+ (known after apply),
]

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

What just happened?

Terraform state file format

{
"version": 4,
"terraform_version": "0.14.3",
"serial": 8,
"lineage": "bfc6125c-e1b2-ab53-8935-380a317f441c",
"outputs": {},
"resources": []
}

Significance of Terraform states

  1. This way state files help Terraform to understand the changes in the configurations and determine which changes it needs to perform in the next terraform apply the operation.
  2. States also help Terraform to understand dependencies.
  3. In case of complex infrastructure deployment, running terraform plan can turn out to be very slow and may also breach API usage limits if Terraform had to check for currently deployed resources directly from the provider. Maintaining states help address this performance issue.

State lock

Importing existing resources

resource "aws_instance" "demo_new" {
provider = aws.aws_west
ami = "ami-03130878b60947df3"
instance_type = "t2.micro"
}
aws_instance.demo_new: Importing from ID "i-05ad4fa08384df633"...
aws_instance.demo_new: Import prepared!
Prepared aws_instance for import
aws_instance.demo_new: Refreshing state... [id=i-05ad4fa08384df633]

Import successful!

Sensitive Data

Workspaces

terraform workspace show# Output
default
terraform workspace new myWs
terraform workspace select <name of the workspace>
terraform show
terraform workspace select myWs 
terraform show

--

--

--

https://letsdotech.dev

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Bandwidth Alliance Partners — Exciting Choices

OCI DBCS patching using Software Images via REST API

Trying out Google’s Firebase

Data Types in JVM

Data Types in JVM

Announcing Plasm Grit Program

J-NPCU - Dynamic image processing with 12x speedup!

On finding our technology stack

I Made a New Captcha System

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Let's Do Tech

Let's Do Tech

https://letsdotech.dev

More from Medium

Remove a local group on Windows-like systems — Ansible module win_group

ACL on S3 bucket objects with Terraform

Provisioning AWS Karpenter Provisioners with Terraform

Generate SSH key using terraform for multiple environments