zaki work log

作業ログやら生活ログやらなんやら

[Terraform] EC2のEIP設定とdomain="vpc"定義とAWSプロバイダーのアップグレード

EC2にEIPを設定したくて以下のようなtfファイルを作成

resource "aws_instance" "sample" {
  :
}

resource "aws_eip" "sample-eip" {
  instance   = aws_instance.sample.id
  domain     = "vpc"
  depends_on = [aws_internet_gateway.igw]

  tags = {
    Name = "sample-eip"
  }
}

するとこんなエラー。

$ terraform plan
╷
│ Error: Value for unconfigurable attribute
│ 
│   with aws_eip.sample-eip,
│   on ec2.tf line 87, in resource "aws_eip" "sample":
│   87:   domain = "vpc"
│ 
│ Can't configure a value for "domain": its value will be decided automatically based on the result of applying this configuration.
╵

ドキュメントをよく見るとdomain = "vpc"の記述は新しい書き方で、元々はvpc = trueと記述するものが廃止予定になりその替わりの記述になった模様
現に「EC2 EIP Terraform」でググってヒットする多くの情報は今日時点ではvpc = trueになっており、実際vpc = trueと書くと特にdeprecateの警告表示もなく構築可能。

なので10割の確率で使ってる環境のTerraform関連バージョンだろうと見当を付けてアップグレードを実施。

まず現在のバージョン。
そういえばかなり前に1.5にアップグレードしてその時の状態。
リリースノートを見る限り、Terraform v1.5.3が去年の7月、AWSプロバイダ v3.76.1は去年の1月リリースだったみたいね。

$ terraform -version
Terraform v1.5.3
on linux_amd64
+ provider registry.terraform.io/hashicorp/aws v3.76.1

Your version of Terraform is out of date! The latest version
is 1.7.3. You can update by downloading from https://www.terraform.io/downloads.html

terraformaptで入れてるのでapt-get upgradeで更新したので割愛。

AWSプロバイダについては、required_providers定義をまず更新。
現在の最新版が5.36.0なので、とりあえず5以上になるようにする。(雑)

--- a/terraform/provider.tf
+++ b/terraform/provider.tf
@@ -2,7 +2,7 @@ terraform {
   required_providers {
     aws = {
       source  = "hashicorp/aws"
-      version = "~> 3.0"
+      version = "~> 5.0"
     }
   }

この状態で再度init-upgradeオプションを付与して実行。

$ terraform init -upgrade

Initializing the backend...

Initializing provider plugins...
- Finding hashicorp/aws versions matching "~> 5.0"...
- Installing hashicorp/aws v5.36.0...
- Installed hashicorp/aws v5.36.0 (signed by HashiCorp)

Terraform has made some changes to the provider dependency selections recorded
in the .terraform.lock.hcl file. Review those changes and commit them to your
version control system if they represent changes you intended to make.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

これで.terraform.lock.hclも更新される。

--- a/terraform/.terraform.lock.hcl
+++ b/terraform/.terraform.lock.hcl
@@ -2,24 +2,24 @@
 # Manual edits may be lost in future updates.
 
 provider "registry.terraform.io/hashicorp/aws" {
-  version     = "3.76.1"
-  constraints = "~> 3.0"
+  version     = "5.36.0"
+  constraints = "~> 5.0"
   hashes = [

アップグレード後のバージョンはこの通りで、EIPのdomain = "vpc"も問題なく動作する。

$ terraform providers -version
Terraform v1.7.3
on linux_amd64
+ provider registry.terraform.io/hashicorp/aws v5.36.0

この状態でEIP設定の定義をvpc = trueにしてみると、思った通り以下の警告が表示された。

╷
│ Warning: Argument is deprecated
│ 
│   with aws_eip.sample-eip,
│   on ec2.tf line 88, in resource "aws_eip" "sample-eip":
│   88:   vpc = true
│ 
│ use domain attribute instead
│ 
│ (and one more similar warning elsewhere)
╵

教訓:こまめにアップグレードしましょう。

あとそういえばEIPを自分で設定するのって初めてかも。(ポータルからも設定したことない)