1

我正在尝试实现一个模块,我试图在已经创建的子网中旋转多个实例(通过 terraform),但我不确定如何在模块中实际使用计数以及如何从 s3 存储桶数据源中选择值来旋转multi-az 中的实例,这是我在模块目录中的资源的样子

resource "aws_instance" "ec2-instances" {
  count = "${var.count_num }"
  ami = "${data.aws_ami.ubuntu.id}"
  instance_type = "${var.machine_type}"
  key_name = "${var.key_name}"
  #vpc_security_group_ids = ["${aws_security_group.jumpbox-sec-group.id}"]
  vpc_security_group_ids =  ["${var.sec-group}"]
  disable_api_termination = "${var.is_production ? true : false}"
  subnet_id = "${element(var.es_stg_subnets, count.index)}" <--- This won't work , i need to use data-source as s3 
  tags {
    #Name = "${var.master_name}-${count.index+1}"
    Name = "${var.instance-tag}-${count.index+1}"
    Type = "${var.instance-type-tag}"
  }
  root_block_device {
    volume_size = "${var.instance-vol-size}"
    volume_type = "gp2"
  }
}

这是实际的模块:

module "grafana-stg" {
  source = "../../modules/services/gen-ec2"
  #ami_id = "${data.aws_ami.ubuntu.id}"
  instance_type = "${var.grafana_machine_type}"
  key_name = "jumpbox"
  vpc_security_group_ids = ["${aws_security_group.grafana-sec-group.id}"]

  #subnets = "${data.terraform_remote_state.s3_bucket_state.subnet-public-prod-1a}"

  subnet_id = ??????????????????

  disable_api_termination = "${var.is_production ? true : false}"


}
4

2 回答 2

1

我会考虑使用data source.

利用Data Sources

Terraform 有数据源的概念。您可以从 AWS 中提取资源所需的信息。在您的gen-ec2.tf文件中 -

// In order to get subnets, you need the VPC they belong to.
// Note you can filter on a variety of different tags.
data "aws_vpc" "selected" {
  tags {
    Name = "NameOfVPC"
  }
}

// This will then retrieve all subnet ids based on filter
data "aws_subnet_ids" "private" {
  vpc_id = "${data.aws_vpc.selected.id}"
  tags {
    Tier = "private*"
  }
}

resource "aws_instance" "ec2-instances" {
  count = "${length(data.aws_subnet_ids.private.ids)}"
  ami = "${data.aws_ami.ubuntu.id}"
  instance_type = "${var.machine_type}"
  key_name = "${var.key_name}"
  vpc_security_group_ids =  ["${var.sec-group}"]
  disable_api_termination = "${var.is_production ? true : false}"
  subnet_id = "${element(data.aws_subnet_ids.private.*.ids, count.index)}" 
  tags {
    Name = "${var.instance-tag}-${count.index+1}"
    Type = "${var.instance-type-tag}"
  }
  root_block_device {
    volume_size = "${var.instance-vol-size}"
    volume_type = "gp2"
  }
}

您的模块现在看起来像这样 -

module "grafana-stg" {
  source = "../../modules/services/gen-ec2"
  #ami_id = "${data.aws_ami.ubuntu.id}"
  instance_type = "${var.grafana_machine_type}"
  key_name = "jumpbox"
  vpc_security_group_ids = ["${aws_security_group.grafana-sec-group.id}"]
  disable_api_termination = "${var.is_production ? true : false}"
}
于 2018-04-19T04:54:23.750 回答
0

对我来说,当我使用 Terraform v0.12.5 时,下面的代码片段运行良好

data "aws_subnet_ids" "public_subnet_list" {
  vpc_id = "${var.vpc_id}"
  tags = {
    Tier = "Public"
  }
}


resource "aws_instance" "example" {
  count         = 2
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  subnet_id     = tolist(data.aws_subnet_ids.public_subnet_list.ids)[count.index]
}



于 2019-09-13T09:36:36.913 回答