2022.04.03   2022.04.09

【terraform】refresh-onlyとは。S3で試す

Terraform    

refresh-onlyとは

クラウド環境(※1)とterraform.tfstateファイル(※2)に差分があった場合に、クラウド環境の差分をterraform.tfstateファイルに適用するオプションです。

ただし、実行しても差分が解消されるのはterraform.tfstateファイルだけなので、元のソースコード(xxx.tf)は修正されません。

差分の確認にはterraform planを使用します。

もし、クラウド環境とterraform.tfstateファイルに差分があった場合はNote: Objects have changed outside of Terraformと表示され、それ以降に差分内容が表示されます。

つまり、refresh-onlyを実行するとNote: Objects have changed outside of Terraformの内容がterraform.tfstateファイルに適用されるという動きをします。

※1 実体
※2 terraform管理ファイル。terraform applyの結果を保存

refresh-only は次のコマンドで実行できます。

terraform apply -refresh-only 

refresh-onlyの動作確認

次のコードでAWSのS3を作成します。

# config.tf

terraform {
  required_version = "~> 1.1"

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.5"
    }
  }
}

provider "aws" {
  region = "ap-northeast-1"
}
# S3.tf

resource "aws_s3_bucket" "tf-bucket" {
  bucket = "tf-bucket-20220403"

  tags = {
    Name = "tf-bucket"
  }
}

マネージメントコンソールで上記コードでは定義していないDateタグを設定します。

設定後terraform planを実行します。
すると、コード外で差分が発生したと警告されます。(Note: Objects have changed outside of Terraform)
また-----------以下にソースコード(s3.tf)とAWS環境の差分も表示されます。

% terraform plan                 

Note: Objects have changed outside of Terraform

Terraform detected the following changes made outside of Terraform since the last "terraform
apply":

  # aws_s3_bucket.tf-bucket has changed
  ~ resource "aws_s3_bucket" "tf-bucket" {
        id                          = "tf-bucket-20220403"
      ~ tags                        = {
          + "Date" = "2022-04-03"
            # (1 unchanged element hidden)
        }
      ~ tags_all                    = {
          + "Date" = "2022-04-03"
            # (1 unchanged element hidden)
        }
        # (10 unchanged attributes hidden)

        # (1 unchanged block hidden)
    }
------------------------------------------------------------------------------------------------------------------------------
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the
following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # aws_s3_bucket.tf-bucket will be updated in-place
  ~ resource "aws_s3_bucket" "tf-bucket" {
        id                          = "tf-bucket-20220403"
      ~ tags                        = {
          - "Date" = "2022-04-03" -> null
            # (1 unchanged element hidden)
        }
      ~ tags_all                    = {
          - "Date" = "2022-04-03" -> null
            # (1 unchanged element hidden)
        }
        # (10 unchanged attributes hidden)

        # (1 unchanged block hidden)
    }

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

そこで、terraform apply -refresh-onlyを実行します。

% terraform apply -refresh-only  

Note: Objects have changed outside of Terraform

# 中略

  Enter a value: yes

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

再びterraform planを実行します。
するとNote: Objects have changed outside of Terraformがの内容が消え、
ソースコード(s3.tf)とAWS環境の差分のみが表示されます。

% terraform plan               

Terraform used the selected providers to generate the following execution plan. Resource actions
are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # aws_s3_bucket.tf-bucket will be updated in-place
  ~ resource "aws_s3_bucket" "tf-bucket" {
        id                          = "tf-bucket-20220403"
      ~ tags                        = {
          - "Date" = "2022-04-03" -> null
            # (1 unchanged element hidden)
        }
      ~ tags_all                    = {
          - "Date" = "2022-04-03" -> null
            # (1 unchanged element hidden)
        }
        # (10 unchanged attributes hidden)

        # (1 unchanged block hidden)
    }

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

ただ-refresh-onlyだけではS3.tfにコードの内容が適用されないので、AWS上の設定通りにS3.tfへコードを追記します。

resource "aws_s3_bucket" "tf-bucket" {
  bucket = "tf-bucket-20220403"

  tags = {
    Name = "tf-bucket"
    Date = "2022-04-03" # 追加
  }
}

これでterraform planを実行すると差分がなくなったことを確認できます。

% terraform plan

No changes. Your infrastructure matches the configuration.
コメント
現在コメントはありません。
コメントする
コメント入力

名前 (※ 必須)

メールアドレス (※ 必須 画面には表示されません)

送信