Skip to content

Commit c92e80c

Browse files
craigsandsantonbabenko
authored andcommitted
Allow multiple subnet ids (#67)
* Allow specifying a list of subnet IDs to iterate instances over
1 parent e6d2f70 commit c92e80c

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ data "aws_ami" "ubuntu-xenial" {
8484
* `network_interface` can't be specified together with `associate_public_ip_address`, which makes `network_interface`
8585
not configurable using this module at the moment
8686
* 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).
87+
* 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`.
8788

8889
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
8990
## Inputs
@@ -111,7 +112,8 @@ data "aws_ami" "ubuntu-xenial" {
111112
| private\_ip | Private IP address to associate with the instance in a VPC | string | `""` | no |
112113
| root\_block\_device | Customize details about the root block device of the instance. See Block Devices below for details | list | `<list>` | no |
113114
| 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 |
114-
| subnet\_id | The VPC Subnet ID to launch in | string | n/a | yes |
115+
| subnet\_id* | The VPC Subnet ID to launch in | string | `""` | no |
116+
| subnet\_ids* | A list of VPC Subnet IDs to launch in | string | `<list>` | no |
115117
| tags | A mapping of tags to assign to the resource | map | `<map>` | no |
116118
| tenancy | The tenancy of the instance (if the instance is running in a VPC). Available values: default, dedicated, host. | string | `"default"` | no |
117119
| user\_data | The user data to provide when launching the instance | string | `""` | no |

main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ resource "aws_instance" "this" {
1111
ami = "${var.ami}"
1212
instance_type = "${var.instance_type}"
1313
user_data = "${var.user_data}"
14-
subnet_id = "${var.subnet_id}"
14+
subnet_id = "${element(distinct(compact(concat(list(var.subnet_id), var.subnet_ids))),count.index)}"
1515
key_name = "${var.key_name}"
1616
monitoring = "${var.monitoring}"
1717
vpc_security_group_ids = ["${var.vpc_security_group_ids}"]
@@ -50,7 +50,7 @@ resource "aws_instance" "this_t2" {
5050
ami = "${var.ami}"
5151
instance_type = "${var.instance_type}"
5252
user_data = "${var.user_data}"
53-
subnet_id = "${var.subnet_id}"
53+
subnet_id = "${element(distinct(compact(concat(list(var.subnet_id), var.subnet_ids))),count.index)}"
5454
key_name = "${var.key_name}"
5555
monitoring = "${var.monitoring}"
5656
vpc_security_group_ids = ["${var.vpc_security_group_ids}"]

variables.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ variable "vpc_security_group_ids" {
5757

5858
variable "subnet_id" {
5959
description = "The VPC Subnet ID to launch in"
60+
default = ""
61+
}
62+
63+
variable "subnet_ids" {
64+
description = "A list of VPC Subnet IDs to launch in"
65+
default = []
66+
type = "list"
6067
}
6168

6269
variable "associate_public_ip_address" {

0 commit comments

Comments
 (0)