たとえばこんな Terraform 定義があります。
resource "aws_instance" "this" {
ami = "ami-*****************"
instance_type = "t4g.small"
associate_public_ip_address = false
# その他省略
}
resource "aws_eip" "this" {
domain = "vpc"
}
resource "aws_eip_association" "this" {
instance_id = aws_instance.this.id
allocation_id = aws_eip.this.id
}terraform apply を実行するとちゃんとプロビジョニングできます。
が、その後 terraform plan を実行すると、何も変更していないはずなのに associate_public_ip_address を変えたねってことで差分として検知されちゃいます。
# module.aws_instance.this must be replaced
-/+ resource "aws_instance" "this" {
# 途中省略
~ associate_public_ip_address = true -> false # forces replacementこれはバグ… とまでは言い切れないもの Terraform の動作がそうなっているために発生します。Issue がたてられています。
- Enhancement: Support modifying EC2 instance public IP setting without forcing replacement · Issue #37238 · hashicorp/terraform-provider-aws
- https://github.com/hashicorp/terraform-provider-aws/issues/37238
この問題を回避するためには、EIP を割り当てるインスタンスは associate_public_ip_address を指定しないようにしましょう。
resource "aws_instance" "this" {
ami = "ami-*****************"
instance_type = "t4g.small"
# associate_public_ip_address = false # 👈️ EIP を割り当てるインスタンスは associate_public_ip_address を指定しない
# その他省略
}
resource "aws_eip" "this" {
domain = "vpc"
}
resource "aws_eip_association" "this" {
instance_id = aws_instance.this.id
allocation_id = aws_eip.this.id
}