Terraform State — mapping between configuration and real infrastructure.
1# Backend configuration2terraform {3 backend "s3" {4 bucket = "my-terraform-state"5 key = "prod/terraform.tfstate"6 region = "us-west-2"7 dynamodb_table = "terraform-locks"8 encrypt = true9 }10}1112# State commands13# terraform state list14# terraform state show aws_instance.web15# terraform state mv aws_instance.old aws_instance.new16# terraform state rm aws_instance.orphan17# terraform import aws_instance.existing i-1234567890abcdef01819# Workspaces20# terraform workspace new dev21# terraform workspace select prod22# terraform workspace list2324# Data sources for remote state25data "terraform_remote_state" "vpc" {26 backend = "s3"27 config = {28 bucket = "my-terraform-state"29 key = "shared/vpc/terraform.tfstate"30 region = "us-west-2"31 }32}3334resource "aws_instance" "web" {35 subnet_id = data.terraform_remote_state.vpc.outputs.public_subnet_id36}
Best Practices: