user_data template_file中的aws_autoscaling_group和count.index

时间:2018-09-24 20:58:21

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

我有template_file包含user_data用于launch_template的支持和ASG。

我想要实现的是在每个ASG实例中使用数组变量中的一个元素

variable "my_array_var" {
  type = "list"
  default = [ "one", "two" ]
}

data "template_file" "node-user-data" {
  count = "${length(var.my_array_var)}"
  template = <<EOF
#cloud-config
runcmd:
  - [ my-command, ${element(var.my_array_var, count.index)} ]
EOF
}

resource "aws_launch_template" "node-lt" {
  name = "node-lt"
  image_id = "${var.instance_ami}"
  instance_type = "${var.instance_type_node}"

  user_data = "${base64encode(data.template_file.node-user-data.rendered)}"
}

resource "aws_autoscaling_group" "node-asg" {
  name = "node-asg"
  desired_capacity = 2
  max_size = 2
  min_size = 2

  launch_template {
    id = "${aws_launch_template.node-lt.id}"
    version = "$$Latest"
  }
}

有没有办法用terraform做到这一点?

0 个答案:

没有答案