Terraform State
What is Terraform State?
Section titled “What is 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.
State Commands
Section titled “State Commands”You can view and manipulate Terraform state using the terraform state
command.
Command | Description |
---|---|
list | List resources in the state |
mv | Move an item in the state |
pull | Pull current state and output to stdout |
push | Update remote state from a local state file |
replace-provider | Replace provider in the state |
rm | Remove instances from the state |
show | Show a resource in the state |
Examples
Section titled “Examples”Listing all resources in the state
Section titled “Listing all resources in the state”Example terraform state list
output
data.aws_ami.latest_amazon_linuxdata.aws_vpc_endpoint_service.s3aws_instance.web_serveraws_internet_gateway.igwaws_route.public_routeaws_route_table.privateaws_route_table.publicaws_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_serveraws_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.vpcaws_vpc_endpoint.s3aws_vpc_endpoint_route_table_association.s3_az_association
Getting details of a specific resource
Section titled “Getting details of a specific resource”Example terraform state show aws_vpc.vpc
output
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" }}