Skip to content

Terraform State

Terraform stores information about your infrastructure within a state file.

Terraform uses state to determine which changes to make to your infrastructure, based on the HCL configuration it finds in your code. Changes in state will lead to the resources being modified, added or removed.

Before any changes are applied to your infrastructure, Terraform performs a refresh to sync the current state, in order to check for any resources’ that may have encountered configuration drifts.

You can view and manipulate Terraform state using the terraform state command.

CommandDescription
listList resources in the state
mvMove an item in the state
pullPull current state and output to stdout
pushUpdate remote state from a local state file
replace-providerReplace provider in the state
rmRemove instances from the state
showShow a resource in the state

Example terraform state list output

Terminal window
data.aws_ami.latest_amazon_linux
data.aws_vpc_endpoint_service.s3
aws_instance.web_server
aws_internet_gateway.igw
aws_route.public_route
aws_route_table.private
aws_route_table.public
aws_route_table_association.private["eu-west-2a"]
aws_route_table_association.private["eu-west-2b"]
aws_route_table_association.private["eu-west-2c"]
aws_route_table_association.public["eu-west-2a"]
aws_route_table_association.public["eu-west-2b"]
aws_route_table_association.public["eu-west-2c"]
aws_security_group.web_server
aws_subnet.private["eu-west-2a"]
aws_subnet.private["eu-west-2b"]
aws_subnet.private["eu-west-2c"]
aws_subnet.public["eu-west-2a"]
aws_subnet.public["eu-west-2b"]
aws_subnet.public["eu-west-2c"]
aws_vpc.vpc
aws_vpc_endpoint.s3
aws_vpc_endpoint_route_table_association.s3_az_association

Example terraform state show aws_vpc.vpc output

Terminal window
resource "aws_vpc" "vpc" {
arn = "arn:aws:ec2:eu-west-2:<id>:vpc/vpc-05f087bb5414aab4c"
assign_generated_ipv6_cidr_block = false
cidr_block = "192.168.0.0/16"
default_network_acl_id = "acl-017e916dbee0b5d63"
default_route_table_id = "rtb-06b2809db725c55ef"
default_security_group_id = "sg-0b5a4febc36710404"
dhcp_options_id = "dopt-6c222d04"
enable_dns_hostnames = true
enable_dns_support = true
enable_network_address_usage_metrics = false
id = "vpc-05f087bb5414aab4c"
instance_tenancy = "default"
ipv6_netmask_length = 0
main_route_table_id = "rtb-06b2809db725c55ef"
owner_id = "<id>"
tags = {
"App" = "webapp"
"Name" = "webapp-vpc"
"Prefix" = "webapp"
"Region" = "eu-west-2"
}
tags_all = {
"App" = "webapp"
"Name" = "webapp-vpc"
"Prefix" = "webapp"
"Region" = "eu-west-2"
}
}