繁体   English   中英

在Terraform中使用Count创建启动配置

[英]Using Count in Terraform to create Launch Configuration

我有3个不同版本的AMI,用于集群中的3个不同节点。

data "aws_ami" "node1"
{
  # Use the most recent AMI that matches the pattern below in 'values'.
  most_recent = true

  filter {
    name = "name"
    values = ["AMI_node1*"]
  }

  filter {
    name = "tag:version"
    values = ["${var.node1_version}"]
  }

}

data "aws_ami" "node2"
{
  # Use the most recent AMI that matches the pattern below in 'values'.
  most_recent = true

  filter {
    name = "name"
    values = ["AMI_node2*"]
  }

  filter {
    name = "tag:version"
    values = ["${var.node2_version}"]
  }

}

data "aws_ami" "node3"
{
  ...
}

我想分别使用每个AMI创建3个不同的启动配置和Auto Scaling组。

resource "aws_launch_configuration" "node"
{
  count = "${local.node_instance_count}"
  # Name-prefix must be used otherwise terraform fails to perform updates to existing launch configurations due to
  # a name conflict: LCs are immutable and the LC cannot be destroyed without destroying attached ASGs as well, which
  # terraform will not do. Using name-prefix lets a new LC be created and swapped into the ASG.
  name_prefix = "${var.environment_name}-node${count.index + 1}-"
  image_id = "${data.aws_ami.node[count.index].image_id}"
  instance_type = "${var.default_ec2_instance_type}"
 ...
}

但是,我不能aws_ami.node1上面显示的aws_ami.node3通过cound.index使用aws_ami.node1aws_ami.node2aws_ami.node3 我收到以下错误:

Error reading config for aws_launch_configuration[node]: parse error at 1:39: expected "}" but found "."

我可以在Terraform中执行另一种方法吗?

索引数据源不是可行的事情; 在这一刻。

您最好将已定义的data源放下并将图像ID编码到Terraform map变量中。

variable "node_image_ids" {
  type        = "map"

  default = {
    "node1" = "1234434"
    "node2" = "1233334"
    "node3" = "1222434"
  }
}

然后,食用它:

image_id = "${lookup(var.node_image_ids, concat("node", count.index), "some_default_image_id")}"

这样做的缺点是,在升级图像时,您需要手动更新图像ID。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM