Terraform EB环境找不到具有明确依赖性的子网

时间:2019-06-06 08:34:17

标签: terraform terraform-provider-aws

我有一个Terraform配置,一个文件中包含各种AWS资源,包括一个VPC,三个私有,三个公共子网和一个EB环境。

我的Terraform版本是0.12.0,AWS提供程序版本是~> 2.12 VPC如下所示:

resource "aws_vpc" "terraform-vpc" {
  cidr_block                       = "${var.cidr_block}"
  assign_generated_ipv6_cidr_block = true
}

我的六个子网如下:

resource "aws_subnet" "private-a" {
  vpc_id            = "${aws_vpc.terraform-vpc.id}"
  availability_zone = "eu-west-2a"
  cidr_block        = "10.0.1.0/24"
}

resource "aws_subnet" "public-a" {
  vpc_id            = "${aws_vpc.terraform-vpc.id}"
  availability_zone = "eu-west-2a"
  cidr_block        = "10.0.2.0/24"
}

resource "aws_subnet" "private-b" {
  vpc_id            = "${aws_vpc.terraform-vpc.id}"
  availability_zone = "eu-west-2b"
  cidr_block        = "10.0.3.0/24"
}

resource "aws_subnet" "public-b" {
  vpc_id            = "${aws_vpc.terraform-vpc.id}"
  availability_zone = "eu-west-2b"
  cidr_block        = "10.0.4.0/24"
}

resource "aws_subnet" "private-c" {
  vpc_id            = "${aws_vpc.terraform-vpc.id}"
  availability_zone = "eu-west-2c"
  cidr_block        = "10.0.5.0/24"
}

resource "aws_subnet" "public-c" {
  vpc_id            = "${aws_vpc.terraform-vpc.id}"
  availability_zone = "eu-west-2c"
  cidr_block        = "10.0.6.0/24"
}

然后,在我的aws_elastic_beanstalk_environment资源中,我拥有:

...

setting {
    namespace = "aws:ec2:vpc"
    name      = "Subnets"
    value     = "#{aws_subnet.private-a.id},#{aws_subnet.private-b.id},#{aws_subnet.private-c.id}"
}

setting {
   namespace = "aws:ec2:vpc"
   name      = "ELBSubnets"
   value     = "#{aws_subnet.public-a.id},#{aws_subnet.public-b.id},#{aws_subnet.public-c.id}"
}

...

depends_on = [
  aws_security_group.default, # created earlier in the same file
  aws_subnet.public-a,
  aws_subnet.public-b,
  aws_subnet.public-c,
  aws_subnet.private-a,
  aws_subnet.private-b,
  aws_subnet.private-c
]

问题是我总是收到一个错误,指出其中一个子网不存在,例如:

Error: ConfigurationValidationException: Configuration validation exception: 
Invalid option value: '["#{aws_subnet.private-c.id}","#{aws_subnet.private-b.id}","#{aws_subnet.private-a.id}"]' 
(Namespace: 'aws:ec2:vpc', OptionName: 'Subnets'): The subnet '#{aws_subnet.private-c.id}' does not exist.

它始终与其他子网相关,有时是Subnets选项中使用的私有子网之一,有时是ELBSubnets选项中使用的公共子网之一。

我感到困惑的是,我已经将它们明确定义为依赖关系,尽管我认为即使没有明确的依赖关系也应该起作用。并且在terraform apply日志中,始终在eb环境之前创建所有子网:

aws_subnet.private-c: Creation complete after 1s [id=subnet-some-id]
aws_subnet.public-a: Creation complete after 1s [id=subnet-some-id]
aws_subnet.public-c: Creation complete after 1s [id=subnet-some-id]
aws_subnet.public-b: Creation complete after 1s [id=subnet-some-id]
aws_subnet.private-a: Creation complete after 6s [id=subnet-some-id]
aws_subnet.private-b: Creation complete after 6s [id=subnet-some-id]

...

aws_elastic_beanstalk_environment.default: Creating...

在检入AWS时,将按预期并由apply报告创建所有子网。该错误的原因可能是什么?

1 个答案:

答案 0 :(得分:0)

如ydaetskcor在子网内插中所述,并使用#代替$,因此#{aws_subnet.public-a.id},#{aws_subnet.public-b.id}应该是${aws_subnet.public-a.id},${aws_subnet.public-b.id}