Skip to content

Commit c172941

Browse files
authored
Added support for network_interface and arn (terraform-aws-modules#136)
1 parent 8af6add commit c172941

File tree

7 files changed

+96
-27
lines changed

7 files changed

+96
-27
lines changed

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ data "aws_ami" "ubuntu-xenial" {
8787

8888
## Notes
8989

90-
* `network_interface` can't be specified together with `associate_public_ip_address`, which makes `network_interface`
91-
not configurable using this module at the moment
90+
* `network_interface` can't be specified together with `vpc_security_group_ids`, `associate_public_ip_address`, `subnet_id`. See [basic example](https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/tree/master/examples/basic) for details.
9291
* 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).
9392
* 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`.
9493

@@ -98,7 +97,7 @@ data "aws_ami" "ubuntu-xenial" {
9897
| Name | Description | Type | Default | Required |
9998
|------|-------------|:----:|:-----:|:-----:|
10099
| ami | ID of AMI to use for the instance | string | n/a | yes |
101-
| associate\_public\_ip\_address | If true, the EC2 instance will have associated public IP address | bool | `"false"` | no |
100+
| associate\_public\_ip\_address | If true, the EC2 instance will have associated public IP address | bool | `"null"` | no |
102101
| cpu\_credits | The credit option for CPU usage (unlimited or standard) | string | `"standard"` | no |
103102
| disable\_api\_termination | If true, enables EC2 Instance Termination Protection | bool | `"false"` | no |
104103
| ebs\_block\_device | Additional EBS block devices to attach to the instance | list(map(string)) | `[]` | no |
@@ -109,14 +108,14 @@ data "aws_ami" "ubuntu-xenial" {
109108
| instance\_count | Number of instances to launch | number | `"1"` | no |
110109
| instance\_initiated\_shutdown\_behavior | Shutdown behavior for the instance | string | `""` | no |
111110
| instance\_type | The type of instance to start | string | n/a | yes |
112-
| ipv6\_address\_count | A number of IPv6 addresses to associate with the primary network interface. Amazon EC2 chooses the IPv6 addresses from the range of your subnet. | number | `"0"` | no |
113-
| ipv6\_addresses | Specify one or more IPv6 addresses from the range of the subnet to associate with the primary network interface | list(string) | `[]` | no |
111+
| ipv6\_address\_count | A number of IPv6 addresses to associate with the primary network interface. Amazon EC2 chooses the IPv6 addresses from the range of your subnet. | number | `"null"` | no |
112+
| ipv6\_addresses | Specify one or more IPv6 addresses from the range of the subnet to associate with the primary network interface | list(string) | `"null"` | no |
114113
| key\_name | The key name to use for the instance | string | `""` | no |
115114
| monitoring | If true, the launched EC2 instance will have detailed monitoring enabled | bool | `"false"` | no |
116115
| name | Name to be used on all resources as prefix | string | n/a | yes |
117116
| network\_interface | Customize network interfaces to be attached at instance boot time | list(map(string)) | `[]` | no |
118117
| placement\_group | The Placement Group to start the instance in | string | `""` | no |
119-
| private\_ip | Private IP address to associate with the instance in a VPC | string | `""` | no |
118+
| private\_ip | Private IP address to associate with the instance in a VPC | string | `"null"` | no |
120119
| private\_ips | A list of private IP address to associate with the instance in a VPC. Should match the number of instances. | list(string) | `[]` | no |
121120
| root\_block\_device | Customize details about the root block device of the instance. See Block Devices below for details | list(map(string)) | `[]` | no |
122121
| 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. | bool | `"true"` | no |
@@ -127,15 +126,19 @@ data "aws_ami" "ubuntu-xenial" {
127126
| use\_num\_suffix | Always append numerical suffix to instance name, even if instance_count is 1 | bool | `"false"` | no |
128127
| user\_data | The user data to provide when launching the instance | string | `""` | no |
129128
| volume\_tags | A mapping of tags to assign to the devices created by the instance at launch time | map(string) | `{}` | no |
130-
| vpc\_security\_group\_ids | A list of security group IDs to associate with | list(string) | n/a | yes |
129+
| vpc\_security\_group\_ids | A list of security group IDs to associate with | list(string) | `"null"` | no |
131130

132131
## Outputs
133132

134133
| Name | Description |
135134
|------|-------------|
135+
| arn | List of ARNs of instances |
136136
| availability\_zone | List of availability zones of instances |
137137
| credit\_specification | List of credit specification of instances |
138+
| ebs\_block\_device\_volume\_ids | List of volume IDs of EBS block devices of instances |
138139
| id | List of IDs of instances |
140+
| instance\_state | List of instance states of instances |
141+
| ipv6\_addresses | List of assigned IPv6 addresses of instances |
139142
| key\_name | List of key names of instances |
140143
| password\_data | List of Base-64 encoded encrypted password data for the instance |
141144
| placement\_group | List of placement groups of instances |
@@ -144,6 +147,7 @@ data "aws_ami" "ubuntu-xenial" {
144147
| private\_ip | List of private IP addresses assigned to the instances |
145148
| public\_dns | List of public DNS names assigned to the instances. For EC2-VPC, this is only available if you've enabled DNS hostnames for your VPC |
146149
| public\_ip | List of public IP addresses assigned to the instances, if applicable |
150+
| root\_block\_device\_volume\_ids | List of volume IDs of root block devices of instances |
147151
| security\_groups | List of associated security groups of instances |
148152
| subnet\_id | List of IDs of VPC subnets of instances |
149153
| tags | List of tags of instances |

examples/basic/README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
# Basic EC2 instance
22

3-
Configuration in this directory creates EC2 instances with minimum set of arguments. It will also assign Elastic IP (EIP) to an instance.
4-
5-
Unspecified arguments for security group id and subnet are inherited from the default VPC.
6-
7-
This example outputs instance id and public DNS name as a single value and as a list.
3+
Configuration in this directory creates EC2 instances with different sets of arguments (with Elastic IP, with network interface attached, with credit specifications).
84

95
## Usage
106

@@ -25,12 +21,14 @@ Note that this example may create resources which can cost money. Run `terraform
2521
|------|-------------|
2622
| credit\_specification | Credit specification of EC2 instance (empty list for not t2 instance types) |
2723
| credit\_specification\_t2\_unlimited | Credit specification of t2-type EC2 instance |
24+
| ebs\_block\_device\_volume\_ids | List of volume IDs of EBS block devices of instances |
2825
| ids | List of IDs of instances |
2926
| ids\_t2 | List of IDs of t2-type instances |
3027
| instance\_id | EC2 instance ID |
3128
| instance\_public\_dns | Public DNS name assigned to the EC2 instance |
3229
| placement\_group | List of placement group |
3330
| public\_dns | List of public DNS names assigned to the instances |
31+
| root\_block\_device\_volume\_ids | List of volume IDs of root block devices of instances |
3432
| t2\_instance\_id | EC2 instance ID |
3533
| tags | List of tags |
3634
| vpc\_security\_group\_ids | List of VPC security group ids assigned to the instances |

examples/basic/main.tf

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,16 @@ resource "aws_placement_group" "web" {
6161
resource "aws_kms_key" "this" {
6262
}
6363

64+
resource "aws_network_interface" "this" {
65+
count = 1
66+
67+
subnet_id = tolist(data.aws_subnet_ids.all.ids)[count.index]
68+
}
69+
6470
module "ec2" {
6571
source = "../../"
6672

67-
instance_count = 1
73+
instance_count = 2
6874

6975
name = "example-normal"
7076
ami = data.aws_ami.amazon_linux.id
@@ -127,6 +133,25 @@ module "ec2_with_t3_unlimited" {
127133
associate_public_ip_address = true
128134
}
129135

136+
module "ec2_with_network_interface" {
137+
source = "../../"
138+
139+
instance_count = 1
140+
141+
name = "example-network"
142+
ami = data.aws_ami.amazon_linux.id
143+
instance_type = "c5.large"
144+
placement_group = aws_placement_group.web.id
145+
146+
network_interface = [
147+
{
148+
device_index = 0
149+
network_interface_id = aws_network_interface.this[0].id
150+
delete_on_termination = false
151+
}
152+
]
153+
}
154+
130155
# This instance won't be created
131156
module "ec2_zero" {
132157
source = "../../"

examples/basic/outputs.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ output "vpc_security_group_ids" {
1818
value = module.ec2.vpc_security_group_ids
1919
}
2020

21+
output "root_block_device_volume_ids" {
22+
description = "List of volume IDs of root block devices of instances"
23+
value = module.ec2.root_block_device_volume_ids
24+
}
25+
26+
output "ebs_block_device_volume_ids" {
27+
description = "List of volume IDs of EBS block devices of instances"
28+
value = module.ec2.ebs_block_device_volume_ids
29+
}
30+
2131
output "tags" {
2232
description = "List of tags"
2333
value = module.ec2.tags

main.tf

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@ locals {
22
is_t_instance_type = replace(var.instance_type, "/^t[23]{1}\\..*$/", "1") == "1" ? true : false
33
}
44

5-
######
6-
# Note: network_interface can't be specified together with associate_public_ip_address
7-
######
85
resource "aws_instance" "this" {
96
count = var.instance_count
107

118
ami = var.ami
129
instance_type = var.instance_type
1310
user_data = var.user_data
14-
subnet_id = element(
11+
subnet_id = length(var.network_interface) > 0 ? null : element(
1512
distinct(compact(concat([var.subnet_id], var.subnet_ids))),
1613
count.index,
1714
)
@@ -63,7 +60,16 @@ resource "aws_instance" "this" {
6360
}
6461
}
6562

66-
source_dest_check = var.source_dest_check
63+
dynamic "network_interface" {
64+
for_each = var.network_interface
65+
content {
66+
device_index = network_interface.value.device_index
67+
network_interface_id = lookup(network_interface.value, "network_interface_id", null)
68+
delete_on_termination = lookup(network_interface.value, "delete_on_termination", false)
69+
}
70+
}
71+
72+
source_dest_check = length(var.network_interface) > 0 ? null : var.source_dest_check
6773
disable_api_termination = var.disable_api_termination
6874
instance_initiated_shutdown_behavior = var.instance_initiated_shutdown_behavior
6975
placement_group = var.placement_group

outputs.tf

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ output "id" {
33
value = aws_instance.this.*.id
44
}
55

6+
output "arn" {
7+
description = "List of ARNs of instances"
8+
value = aws_instance.this.*.arn
9+
}
10+
611
output "availability_zone" {
712
description = "List of availability zones of instances"
813
value = aws_instance.this.*.availability_zone
@@ -18,6 +23,11 @@ output "key_name" {
1823
value = aws_instance.this.*.key_name
1924
}
2025

26+
output "password_data" {
27+
description = "List of Base-64 encoded encrypted password data for the instance"
28+
value = aws_instance.this.*.password_data
29+
}
30+
2131
output "public_dns" {
2232
description = "List of public DNS names assigned to the instances. For EC2-VPC, this is only available if you've enabled DNS hostnames for your VPC"
2333
value = aws_instance.this.*.public_dns
@@ -28,6 +38,11 @@ output "public_ip" {
2838
value = aws_instance.this.*.public_ip
2939
}
3040

41+
output "ipv6_addresses" {
42+
description = "List of assigned IPv6 addresses of instances"
43+
value = aws_instance.this.*.ipv6_addresses
44+
}
45+
3146
output "primary_network_interface_id" {
3247
description = "List of IDs of the primary network interface of instances"
3348
value = aws_instance.this.*.primary_network_interface_id
@@ -43,11 +58,6 @@ output "private_ip" {
4358
value = aws_instance.this.*.private_ip
4459
}
4560

46-
output "password_data" {
47-
description = "List of Base-64 encoded encrypted password data for the instance"
48-
value = aws_instance.this.*.password_data
49-
}
50-
5161
output "security_groups" {
5262
description = "List of associated security groups of instances"
5363
value = aws_instance.this.*.security_groups
@@ -68,6 +78,21 @@ output "credit_specification" {
6878
value = aws_instance.this.*.credit_specification
6979
}
7080

81+
output "instance_state" {
82+
description = "List of instance states of instances"
83+
value = aws_instance.this.*.instance_state
84+
}
85+
86+
output "root_block_device_volume_ids" {
87+
description = "List of volume IDs of root block devices of instances"
88+
value = [for device in aws_instance.this.*.root_block_device : device.*.volume_id]
89+
}
90+
91+
output "ebs_block_device_volume_ids" {
92+
description = "List of volume IDs of EBS block devices of instances"
93+
value = [for device in aws_instance.this.*.ebs_block_device : device.*.volume_id]
94+
}
95+
7196
output "tags" {
7297
description = "List of tags of instances"
7398
value = aws_instance.this.*.tags

variables.tf

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ variable "monitoring" {
7070
variable "vpc_security_group_ids" {
7171
description = "A list of security group IDs to associate with"
7272
type = list(string)
73+
default = null
7374
}
7475

7576
variable "subnet_id" {
@@ -87,13 +88,13 @@ variable "subnet_ids" {
8788
variable "associate_public_ip_address" {
8889
description = "If true, the EC2 instance will have associated public IP address"
8990
type = bool
90-
default = false
91+
default = null
9192
}
9293

9394
variable "private_ip" {
9495
description = "Private IP address to associate with the instance in a VPC"
9596
type = string
96-
default = ""
97+
default = null
9798
}
9899

99100
variable "private_ips" {
@@ -123,13 +124,13 @@ variable "iam_instance_profile" {
123124
variable "ipv6_address_count" {
124125
description = "A number of IPv6 addresses to associate with the primary network interface. Amazon EC2 chooses the IPv6 addresses from the range of your subnet."
125126
type = number
126-
default = 0
127+
default = null
127128
}
128129

129130
variable "ipv6_addresses" {
130131
description = "Specify one or more IPv6 addresses from the range of the subnet to associate with the primary network interface"
131132
type = list(string)
132-
default = []
133+
default = null
133134
}
134135

135136
variable "tags" {

0 commit comments

Comments
 (0)