简体   繁体   English

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

[英]Using Count in Terraform to create Launch Configuration

I have 3 different version of an AMI, for 3 different nodes in a cluster. 我有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"
{
  ...
}

I would like to create 3 different Launch Configuration and Auto Scaling Group using each of the AMIs respectively. 我想分别使用每个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}"
 ...
}

However, I am not able use aws_ami.node1 , aws_ami.node2 , aws_ami.node3 using the cound.index the way I have shown above. 但是,我不能aws_ami.node1上面显示的aws_ami.node3通过cound.index使用aws_ami.node1aws_ami.node2aws_ami.node3 I get the following error: 我收到以下错误:

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

Is there another way I can do this in Terraform? 我可以在Terraform中执行另一种方法吗?

Indexing data sources isn't something that's doable; 索引数据源不是可行的事情; at the moment. 在这一刻。

You're likely better off simply dropping the data sources you've defined and codifying the image IDs into a Terraform map variable. 您最好将已定义的data源放下并将图像ID编码到Terraform map变量中。

variable "node_image_ids" {
  type        = "map"

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

Then, consume it: 然后,食用它:

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

The downside of this is that you'll need to manually update the image id when images are upgraded. 这样做的缺点是,在升级图像时,您需要手动更新图像ID。

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

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