From 1bbef67c8fbfefe42da4e246aea8a716bf874401 Mon Sep 17 00:00:00 2001 From: Craig Sands Date: Thu, 21 Feb 2019 11:38:14 -0500 Subject: [PATCH 1/3] Allow specifying a list of subnet IDs to iterate instances over --- README.md | 5 ++++- main.tf | 4 ++-- variables.tf | 7 +++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 00c9dd15..1f0f0bb8 100644 --- a/README.md +++ b/README.md @@ -111,13 +111,16 @@ data "aws_ami" "ubuntu-xenial" { | private\_ip | Private IP address to associate with the instance in a VPC | string | `""` | no | | root\_block\_device | Customize details about the root block device of the instance. See Block Devices below for details | list | `` | no | | source\_dest\_check | Controls if traffic is routed to the instance when the destination address does not match the instance. Used for NAT or VPNs. | string | `"true"` | no | -| subnet\_id | The VPC Subnet ID to launch in | string | n/a | yes | +| subnet\_id* | The VPC Subnet ID to launch in | string | n/a | no | +| subnet\_ids* | A list of VPC Subnet IDs to launch in | string | n/a | no | | tags | A mapping of tags to assign to the resource | map | `` | no | | tenancy | The tenancy of the instance (if the instance is running in a VPC). Available values: default, dedicated, host. | string | `"default"` | no | | user\_data | The user data to provide when launching the instance | string | `""` | no | | volume\_tags | A mapping of tags to assign to the devices created by the instance at launch time | map | `` | no | | vpc\_security\_group\_ids | A list of security group IDs to associate with | list | n/a | yes | +* One of 'subnet\_id' or 'subnet\_ids' is required. If both are provided, the value of 'subnet\_id' is prepended to the value of 'subnet\_ids'. + ## Outputs | Name | Description | diff --git a/main.tf b/main.tf index 46a1dc9e..4ce8e320 100644 --- a/main.tf +++ b/main.tf @@ -11,7 +11,7 @@ resource "aws_instance" "this" { ami = "${var.ami}" instance_type = "${var.instance_type}" user_data = "${var.user_data}" - subnet_id = "${var.subnet_id}" + subnet_id = "${element(distinct(compact(concat(list(var.subnet_id), var.subnet_ids))),count.index)}" key_name = "${var.key_name}" monitoring = "${var.monitoring}" vpc_security_group_ids = ["${var.vpc_security_group_ids}"] @@ -50,7 +50,7 @@ resource "aws_instance" "this_t2" { ami = "${var.ami}" instance_type = "${var.instance_type}" user_data = "${var.user_data}" - subnet_id = "${var.subnet_id}" + subnet_id = "${element(distinct(compact(concat(list(var.subnet_id), var.subnet_ids))),count.index)}" key_name = "${var.key_name}" monitoring = "${var.monitoring}" vpc_security_group_ids = ["${var.vpc_security_group_ids}"] diff --git a/variables.tf b/variables.tf index d4f9f87d..5a287c9a 100644 --- a/variables.tf +++ b/variables.tf @@ -57,6 +57,13 @@ variable "vpc_security_group_ids" { variable "subnet_id" { description = "The VPC Subnet ID to launch in" + default = "" +} + +variable "subnet_ids" { + description = "A list of VPC Subnet IDs to launch in" + default = [] + type = "list" } variable "associate_public_ip_address" { From a2fd294d0a70be72a948dd1debb321b74a7c2dde Mon Sep 17 00:00:00 2001 From: Craig Sands Date: Thu, 21 Feb 2019 11:42:04 -0500 Subject: [PATCH 2/3] Allow specifying a list of subnet IDs to iterate instances over --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1f0f0bb8..2d232415 100644 --- a/README.md +++ b/README.md @@ -111,8 +111,8 @@ data "aws_ami" "ubuntu-xenial" { | private\_ip | Private IP address to associate with the instance in a VPC | string | `""` | no | | root\_block\_device | Customize details about the root block device of the instance. See Block Devices below for details | list | `` | no | | source\_dest\_check | Controls if traffic is routed to the instance when the destination address does not match the instance. Used for NAT or VPNs. | string | `"true"` | no | -| subnet\_id* | The VPC Subnet ID to launch in | string | n/a | no | -| subnet\_ids* | A list of VPC Subnet IDs to launch in | string | n/a | no | +| subnet\_id* | The VPC Subnet ID to launch in | string | `""` | no | +| subnet\_ids* | A list of VPC Subnet IDs to launch in | string | `` | no | | tags | A mapping of tags to assign to the resource | map | `` | no | | tenancy | The tenancy of the instance (if the instance is running in a VPC). Available values: default, dedicated, host. | string | `"default"` | no | | user\_data | The user data to provide when launching the instance | string | `""` | no | From 43c9b6a032aa4e0b7197bbfc579316003ebbea2c Mon Sep 17 00:00:00 2001 From: Craig Sands Date: Thu, 21 Feb 2019 12:20:21 -0500 Subject: [PATCH 3/3] Allow specifying a list of subnet IDs to iterate instances over --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 2d232415..de7ba1ab 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,7 @@ data "aws_ami" "ubuntu-xenial" { * `network_interface` can't be specified together with `associate_public_ip_address`, which makes `network_interface` not configurable using this module at the moment * Changes in `ebs_block_device` argument will be ignored. Use [aws_volume_attachment](https://www.terraform.io/docs/providers/aws/r/volume_attachment.html) resource to attach and detach volumes from AWS EC2 instances. See [this example](https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/tree/master/examples/volume-attachment). +* One of `subnet_id` or `subnet_ids` is required. If both are provided, the value of `subnet_id` is prepended to the value of `subnet_ids`. ## Inputs @@ -119,8 +120,6 @@ data "aws_ami" "ubuntu-xenial" { | volume\_tags | A mapping of tags to assign to the devices created by the instance at launch time | map | `` | no | | vpc\_security\_group\_ids | A list of security group IDs to associate with | list | n/a | yes | -* One of 'subnet\_id' or 'subnet\_ids' is required. If both are provided, the value of 'subnet\_id' is prepended to the value of 'subnet\_ids'. - ## Outputs | Name | Description |