diff --git a/README.md b/README.md index 00c9dd15..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 @@ -111,7 +112,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 | yes | +| 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 | 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" {