LoginSignup
6
0

More than 3 years have passed since last update.

Terraformのtfstateファイルを消すとどうなるか

Last updated at Posted at 2020-02-03

Terraformを実行してリソースを作成するとtfstateというファイルが作成されます。

以下公式ドキュメントより

https://www.terraform.io/docs/state/purpose.html
https://www.terraform.io/docs/state/index.html

Terraformは、管理対象のインフラストラクチャと構成に関する状態を保存する必要があります。Terraformはこの状態を使用して、実世界のリソースを構成にマッピングし、メタデータを追跡し、大規模なインフラストラクチャのパフォーマンスを向上させます。

Terraformは通常、構成を使用して依存関係の順序を決定します。ただし、Terraform構成からリソースを削除する場合、Terraformはそのリソースを削除する方法を知っている必要があります。Terraformは、構成にないリソースのマッピングが存在することを確認し、破棄する予定です。ただし、構成が存在しないため、構成のみから順序を決定することはできません。

正しい動作を保証するために、Terraformは、状態内の最新の依存関係セットのコピーを保持します。現在、Terraformは、構成から1つまたは複数のアイテムを削除するときに、状態から正しい破壊順序を決定できます。

このtfstateファイルを削除するとリソースの追加作成や削除が非常に困難になります。

検証してみる

適当に公式のサンプルコードを参考にec2を作成する。

data "aws_ami" "ubuntu" {
  most_recent = true

  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }

  owners = ["099720109477"] # Canonical
}

resource "aws_instance" "web" {
  ami           =  data.aws_ami.ubuntu.id
  instance_type = "t2.micro"

  tags = {
    Name = "HelloWorld"
  }
}

※ちなみにコメントのCanonicalというのはubuntuを開発してる企業です。

公式ドキュメントの通りにterraform planすると、

Warning: Interpolation-only expressions are deprecated

と怒られますが、これは現行のterraform 0.12では推奨されない書き方なのが原因です。
(エラーメッセージにまんま書いていますが)

data.aws_ami.ubuntu.id

と書けば怒られません。

実行してみる

$ terraform applyすると、デフォルトだと実行したディレクトリに

terraform.tfstate
terraform.tfstate.backup

というファイルが作成されます。
中身を見ると以下のようなjson形式ファイルになっています。

]$ cat terraform.tfstate
{
  "version": 4,
  "terraform_version": "0.12.16",
  "serial": 3,
  "lineage": "xxxxxxxxxxxxxxxxxxxx",
  "outputs": {},
  "resources": [
    {
      "mode": "data",
      "type": "aws_ami",
      "name": "ubuntu",
      "provider": "provider.aws",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "architecture": "x86_64",
            "block_device_mappings": [
              {
                "device_name": "/dev/sda1",
                "ebs": {
                  "delete_on_termination": "true",
                  "encrypted": "false",
                  "iops": "0",
~略~

これを削除すると、このファイルの中身と同一のものを作成しないとterraform destroy できなくなります。
試しに消してやってみると

$ terraform destroy
data.aws_ami.ubuntu: Refreshing state...

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:

Terraform will perform the following actions:

Plan: 0 to add, 0 to change, 0 to destroy.

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

参照するstateがないのでterrformで作成したリソースを消せなくなります。

さらにterraformで作成したリソースに、マネジメントコンソール等から変更を加えてしまうと、
tfstateから乖離してdestroyもapplyできなくなるので、terraformを使うのであればtfstateファイル管理がものすごい大事です。
(構築中に消して死にそうになったのは内緒)

6
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
0