在 terraform apply
成功執行之後,資料夾裡會產生一份檔名為 terraform.tfstate
的狀態檔案,裡面放著建立機器資源的所有資料。
這個狀態檔包含組態檔宣告的資源,以及在供應商平台上實際建立的資源後的所有 ID 跟屬性資訊。
可以在檔案裡看到所有資源在 aws 上的 ID,可能是一台伺服器的 ID ,著是伺服器裡掛載的硬碟 ID。
Terraform 透過這份檔案管理機器資源,不論是在 terraform plan
時比對差異產生執行計劃,或是當執行 terraform destroy
要正確的刪除資源。都是依賴狀態檔在運作。
所以 (要講三次):
.tfstate
檔案到版本控制裡你可以直接用 cat
、less
等指令查看檔案內容,或是使用 terraform show
指令
$ terraform show
# aws_instance.example:
resource "aws_instance" "example" {
ami = "ami-0461b11e2fad8c14a"
...
availability_zone = "ap-northeast-1a"
cpu_core_count = 1
cpu_threads_per_core = 1
disable_api_termination = false
ebs_optimized = false
get_password_data = false
hibernation = false
id = "i-0ab21c3f140f6d9b2"
instance_state = "running"
instance_type = "t2.micro"
...
private_dns = "ip-172-31-18-145.ap-northeast-1.compute.internal"
private_ip = "172.31.18.145"
public_dns = "ec2-3-112-206-97.ap-northeast-1.compute.amazonaws.com"
public_ip = "3.112.206.97"
secondary_private_ips = []
security_groups = [
"default",
]
source_dest_check = true
subnet_id = "subnet-161fbc60"
tenancy = "default"
volume_tags = {}
vpc_security_group_ids = [
"sg-b42035d0",
]
credit_specification {
cpu_credits = "standard"
}
metadata_options {
http_endpoint = "enabled"
http_put_response_hop_limit = 1
http_tokens = "optional"
}
root_block_device {
delete_on_termination = true
device_name = "/dev/sda1"
encrypted = false
iops = 100
volume_id = "vol-086e8635eb3533e15"
volume_size = 8
volume_type = "gp2"
}
}