Terraform-将多个子网分配给EC2实例

时间:2018-12-05 15:59:28

标签: amazon-web-services terraform terraform-provider-aws

我有2个子网(每个可用区1个)

select * from (SELECT
farm_products.id as ID,  
farm_products.product as Product1,  
shop_products.product as Product2,
row_number() over(partition by farm_products.id order by 
farm_products.id) rank 
FROM  
farm_products,  
shop_products,  
shop_farm
WHERE
farm_products.id = shop_farm.farm_id and  
shop_farm.farm_id = shop_products.id and  
farm_products.product != shop_products.product)
where rank=1
;

我想将这两个子网添加到EC2实例:

resource "aws_subnet" "infrastructure_services" {
  count                   = "${var.enable_infrastructure_services_subnet ? 2 : 0}"
  vpc_id                  = "${aws_vpc.this.id}"
  cidr_block              = "${cidrsubnet(var.cidr_block,5,count.index + 6)}"
  availability_zone       = "${element(var.availability_zones, count.index)}"
  map_public_ip_on_launch = false

  tags = "${
    merge( map("Name", format("%s %s %s", var.env_name, count.index == 0 ? "Infrastructure Services A" : "Infrastructure Services B", "Subnet")), 
    var.global_tags, 
    var.vpc_tags)}"
}

但得到:

resource "aws_instance" "this" {
  count                   = "${length(var.instance_names)}"
  ami                     = "${var.ami}"
  instance_type           = "${element(var.instance_type, count.index)}"
  availability_zone       = "${element(var.availability_zones, count.index)}"
  disable_api_termination = "${var.disable_api_termination}"
  key_name                = "${var.key_name}"
  ebs_optimized           = "${var.ebs_optimized}"
  subnet_id="${element(aws_subnet.infrastructure_services.id,count.index)}"

  vpc_security_group_ids=["${aws_security_group.infrastructure_services.id}"]

1 个答案:

答案 0 :(得分:1)

您从列表中缺少* splat运算符语法。相反,您应该使用aws_subnet.infrastructure_services.*.id来引用资源列表。

因此,您的实例资源应如下所示:

resource "aws_instance" "this" {
  count                   = "${length(var.instance_names)}"
  ami                     = "${var.ami}"
  instance_type           = "${element(var.instance_type, count.index)}"
  availability_zone       = "${element(var.availability_zones, count.index)}"
  disable_api_termination = "${var.disable_api_termination}"
  key_name                = "${var.key_name}"
  ebs_optimized           = "${var.ebs_optimized}"
  subnet_id               = "${element(aws_subnet.infrastructure_services.*id, count.index)}"
  vpc_security_group_ids  = ["${aws_security_group.infrastructure_services.id}"]
}

也许还应该指出,除非您需要使用列表长度的模来遍历列表,否则可以使用更简单的list[index]语法而不是element(list, index])

resource "aws_instance" "this" {
  count                   = "${length(var.instance_names)}"
  ami                     = "${var.ami}"
  instance_type           = "${var.instance_type[count.index]}"
  availability_zone       = "${var.availability_zones[count.index])}"
  disable_api_termination = "${var.disable_api_termination}"
  key_name                = "${var.key_name}"
  ebs_optimized           = "${var.ebs_optimized}"
  subnet_id               = "${aws_subnet.infrastructure_services.*id[count.index])}"
  vpc_security_group_ids  = ["${aws_security_group.infrastructure_services.id}"]
}