【问题标题】:Multiple template files with autoscaling groups and launch configurations with Terraform具有自动缩放组的多个模板文件和使用 Terraform 的启动配置
【发布时间】:2021-09-13 16:15:49
【问题描述】:

我有 3 个自动缩放组,它们都使用略有不同的模板文件。我拥有的 3 个模板文件的区别在于,每个模板文件在自动缩放组中的实例启动时附加不同的 EBS 卷。我试图弄清楚如何使用 count 将这些不同的模板文件传递给每个自动缩放组。目前,Terraform 代码设置为一个模板文件资源使用 1 个文件的位置,但我需要它来确定如何选择第二个和第三个模板文件。我已经对 Terraform 与之集成的 Cloudinit Config 资源进行了一些研究,但不确定这样的事情是否会对我有所帮助。任何意见,将不胜感激。以下是我当前 Terraform 代码的设置方式。

模板文件

data "template_file" "user_data" {
  count    = "${(var.enable ? 1 : 0) * var.number_of_zones}"
  template = "${file("userdata.sh")}"

  vars {
    node                   = "Node${count.index + 1}"
  }
}

启动配置

resource "aws_launch_configuration" "launch_configuration" {
count = "${(var.enable ? 1 : 0) * var.number_of_zones}"

  name      = "${var.cluster_name}-launch_node_${count.index}"
  key_name  = "${var.key_name2}"
  image_id  = "${lookup(var.amis, "${var.aws_region}.${var.licensee_key == "" && var.licensee == "" ? "enterprise" : "byol"}")}"
  user_data = "${element(data.template_file.user_data.*.rendered, count.index)}"

  security_groups = [
    "${aws_security_group.instance_security_group.id}",
  ]

  instance_type        = "${var.instance_type}"
  iam_instance_profile = "${aws_iam_instance_profile.instance_host_profile.name}"

  ebs_block_device {
    device_name = "/dev/sdf"
    no_device   = true
  }

    lifecycle {
    create_before_destroy = true
  }
}

用户数据脚本

#!/bin/bash

# Attach the right EBS volume
aws ec2 attach-volume --volume-id vol-xxxxxxxxxxxxxxxxx --instance_id `curl http://169.254.169.254/latest/meta-data/instance-id` --device /dev/sdf

我拥有的每个用户数据脚本都会在实例启动时挂载不同的卷。关于如何在不创建多个 template_file 资源块的情况下传递不同值的任何建议都会有所帮助。正在使用的 Terraform 版本是 0.11.10。

【问题讨论】:

  • 感谢@MarkB 的回复。关于如何使用此 aws_volume_attachment 资源附加到启动配置的任何建议?
  • 进展如何?还不清楚自己能做什么?

标签: amazon-web-services terraform autoscaling aws-auto-scaling launch-configuration


【解决方案1】:

您正在将 node 变量传递给您的 user_data 模板。您可以使用interpolation 在模板中引用它:

#!/bin/bash

# Attach the right EBS volume
aws ec2 attach-volume --volume-id ${node} --instance_id `curl http://169.254.169.254/latest/meta-data/instance-id` --device /dev/sdf

【讨论】:

    猜你喜欢
    • 2018-01-29
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 2019-06-22
    • 2018-12-11
    • 2017-11-22
    • 2021-09-07
    相关资源
    最近更新 更多