我有一段非常简单的Terraform代码:
provider "aws" {
region = "eu-west-1"
}
module ec2 {
source = "./ec2_instance"
name = "EC2 Instance 1"
}
模块在哪里:
variable "name" {
default = "Default Name from ec2_instance.tf"
}
resource "aws_instance" "example" {
ami = "ami-e5083683"
instance_type = "t2.nano"
subnet_id = "subnet-3e976259"
associate_public_ip_address = true
security_groups = [ "sg-7310e10b" ]
tags {
Name = "${var.name}"
}
}
当我第一次运行它时,我得到以下输出:
security_groups.#: "" => "1"
security_groups.1642973399: "" => "sg-7310e10b"
但是,下次我尝试
plan
时,我得到: security_groups.#: "0" => "1" (forces new resource)
security_groups.1642973399: "" => "sg-7310e10b" (forces new resource)
是什么赋予了?!
最佳答案
您错误地将 vpc_security_group_id 分配给 security_groups 而不是 vpc_security_group_ids 。将 security_groups = [“sg-7310e10b”] 更改为 vpc_security_group_ids = [“sg-7310e10b”] ,一切正常。