본문 바로가기
T101 4기

4-1. terraform 상태관리 - 이론

by yeongki0944 2024. 7. 6.

1. terraform.tfstate 파일

{
  "version": 4,
  "terraform_version": "1.8.5",
  "serial": 5,
  "lineage": "c13e6102-903a-b2f1-6481-1e6ce048ad39",
  "outputs": {},
  "resources": [],
  "check_results": null
}
항목 설명
version - 상태 파일(.tfstate) 형식의 버전
terraform_version - terraform의 버전
serial - 상태 파일(.tfstate)이 업데이트된 횟수
lineage - 동일한 설정에서 생성된 상태 파일을 식별하는 고유 ID  -> ????
outputs - terraform 실행 후 출력된 값(output)
resources - terraform이 관리하는 인프라 리소스 목록
check_results  

 

2. version vs serial

2-1. version

  • 상태파일( .tfstate )의 형식의 버전
  • 변경시점 : terraform의 버전이 변경되어, ( .tfstate ) 파일의 형식이 변경될 때

2-2. serial

  • 상태파일( .tfstate )가 수정된 횟수
  • 변경시점 : 사용자가 인프라 변경을 적용(terrafor apply)할 때

 

 

3. tfstate 파일 - resources

  "resources": [
    {
      "module": "module.vpc[\"client-webapp\"]",
      "mode": "managed",
      "type": "aws_vpc",
      "name": "this",
      "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
      "instances": [
        {
          "index_key": 0,
          "schema_version": 1,
          "attributes": {
            "arn": "arn:aws:ec2:us-east-2:560412178918:vpc/vpc-0b25065b799f65829",
            "assign_generated_ipv6_cidr_block": false,
            "cidr_block": "10.0.0.0/16",
            "default_network_acl_id": "acl-0b60e518f5e8c01d8",
            "default_route_table_id": "rtb-0e40af5b62e2c5b6e",
            "default_security_group_id": "sg-00943d661ee7ff1f0",
            "dhcp_options_id": "dopt-03218b7b822bc7ba2",
            "enable_classiclink": false,
            "enable_classiclink_dns_support": false,
            "enable_dns_hostnames": false,
            "enable_dns_support": true,
            "id": "vpc-0b25065b799f65829",
            "instance_tenancy": "default",
            "ipv4_ipam_pool_id": null,
            "ipv4_netmask_length": null,
            "ipv6_association_id": "",
            "ipv6_cidr_block": "",
            "ipv6_cidr_block_network_border_group": "",
            "ipv6_ipam_pool_id": "",
            "ipv6_netmask_length": 0,
            "main_route_table_id": "rtb-0e40af5b62e2c5b6e",
            "owner_id": "560412178918",
            "tags": {
              "Name": ""
            },
            "tags_all": {
              "Name": ""
            }
          },
          "sensitive_attributes": [],
          "private": "eyJzY2hlbWFfdmVyc2lvbiI6IjEifQ==",
          "create_before_destroy": true
        }
      ]
    }
  ]

 

항목 설명
module - Terraform 모듈의 이름
mode - 리소스의 모드 
    - data           : 기존 인프라 정보 조회에 사용
    - managed   : 인프라를 배포하고 관리할 때 사용
type - 리소스의 유형
    - aws_instance
    - aws_vpc
    - aws_s3_bucket 등..
name - 리소스의 이름
provider - 리소스를 생성하는 데 사용된 프로바이더
    - provider["registry.terraform.io/hashicorp/aws"])  : AWS 프로바이더
instances - 리소스의 인스턴스 정보
    - 각 인스턴스의 스키마 버전, 속성, 민감한 속성 등