【问题标题】:How to use different different user_data template for multiple instance with ec2 module如何通过 ec2 模块对多个实例使用不同的 user_data 模板
【发布时间】:2020-04-05 16:45:48
【问题描述】:

我是 terraform 的新手,我的要求是将多个实例启动到具有不同 user_data 的通用 ec2 模块。

我的 maif.tf 文件:

    provider "aws" {
    region     = "${var.region}"
    profile    = "${var.aws_profile}" #.aws/credentials
    }
variable "count" {
  default = 3
}
variable "hostnames" {
  default = {
    "0" = "testcase_1.sh"
    "1" = "testcase_2.sh"
    "2" = "testcase_3.sh"
  }
}
data "template_file" "user_data" {
  // here we expand multiple template_files - the same number as we have instances
  count = "${var.count}"
  template = "wrf_testcase.sh"
  vars {
    // that gives us access to use count.index to do the lookup
    hostname = "${lookup(var.hostnames, count.index)}"
  }
}
module "ec2" {
    source = "/././modules/ec2"
    count = 3
    user_data              = "${element(data.template_file.user-data.rendered, count.index)}"
}

我的 ec2 模块:

 resource "aws_instance" "application" {

  count                         = "${var.count}"
  ami                           = "${var.ami}"
  availability_zone             = "${var.availability_zone}"
  ebs_optimized                 = "${var.ebs_optimized}"
  instance_type                 = "${var.instance_type}"
  key_name                      = "${var.key_name}"
  monitoring                    = "${var.monitoring}"
  vpc_security_group_ids        = ["${var.security_group_ids}"]
  subnet_id                     = "${var.subnet_id}"
  associate_public_ip_address   = "${var.associate_public_ip_address}"
  iam_instance_profile          = "${var.iam_instance_profile}"
  user_data                     = "${var.user_data}"
  tags                          = "${merge(var.tags, map("Name", 
  format("%s", var.instance_name)))}"
}

错误:模块“ec2”:计数变量仅在资源内有效

【问题讨论】:

  • 错误:模块“ec2”:计数变量仅在资源内有效
  • 您能否编辑您的问题以包括您尝试过的内容以及为什么这对您不起作用。如果它出错,则显示您的确切错误。如果它没有出错但没有给你你想要的东西,你能清楚地解释为什么它没有做你想要的吗?
  • 这个我试过了。
  • 它告诉你不能在模块中使用关键字count。有一大堆重复的问题,包括前几天的这个:stackoverflow.com/questions/56241761/…

标签: count terraform


【解决方案1】:

count.index 不适用于模块的因此更好的方法在单个 .tf 文件中使用它会起作用。

variable "instance_count" {
  default = 3
}

variable "hostnames" {
  default = ["testcase_1.sh", "testcase_2.sh",
  "testcase_3.sh"]
}

data "template_file" "user-data" {
  count    = "${length(var.hostnames)}"
  template = "${file("${element(var.hostnames, count.index)}")}"
}

resource "aws_instance" "application" {
  count                       = "${var.instance_count}"
  ami                         = "${var.ami}"
  availability_zone           = "${var.availability_zone}"
  ebs_optimized               = "${var.ebs_optimized}"
  instance_type               = "${var.instance_type}"
  key_name                    = "${var.key_name}"
  monitoring                  = "${var.monitoring}"
  vpc_security_group_ids      = ["${var.security_group_ids}"]
  subnet_id                   = "${var.subnet_id}"
  associate_public_ip_address = "${var.associate_public_ip_address}"
  iam_instance_profile        = "${var.iam_instance_profile}"
  user_data                   = "${element(data.template_file.user-data.*.rendered, count.index)}"
  tags = "${merge(var.tags, map("Name", format("%s", var.instance_name)))}"
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-05
    • 2018-03-17
    • 2012-01-11
    • 2018-10-06
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 1970-01-01
    相关资源
    最近更新 更多