首页 > 解决方案 > 在 Terraform 中使用不同的模板创建多个启动配置

问题描述

我正在尝试编写一些 Terraform 代码来使用一个资源块创建多个启动配置。它变得棘手的部分是每个启动配置我需要为每个启动配置使用一个独特的模板。我正在使用 Terraform 0.11.10,所以我认为需要使用 count 。下面的代码使用相同的模板创建多个启动配置,我需要能够为每个配置使用不同的模板。任何意见将是有益的。

启动配置

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
  }

模板文件

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

  vars {
    node                   = "Node${count.index + 1}_#"
    master                 = "${count.index == 0 ? 1 : 0}"
    licensee               = "${var.licensee}"
    licensee_key           = "${var.licensee_key}"
    cluster_name           = "${var.cluster_name}"
    ebs_volume             = "${element(aws_ebs_volume.volume.*.id, count.index)}"
    volume_size            = "${var.volume_size}"
    volume_type            = "${var.volume_type}"
    ansible_git_ssh_key    = "${var.ansible_git_ssh_key}"
    ansible_pull_url       = "${var.ansible_pull_url}"
    ansible_playbook_file  = "${var.ansible_playbook_file}"
    ansible_inventory_file = "${var.ansible_inventory_file}"
    ansible_pull_branch    = "${var.ansible_pull_branch}"
  }

标签: amazon-web-servicesforeachcountterraformlaunch-configuration

解决方案


推荐阅读