Skip to content

Commit 70fb952

Browse files
SweetOpsconst-bon
authored andcommitted
Add additional ENIs with EIPs capability (#17)
1 parent fb7832c commit 70fb952

File tree

7 files changed

+93
-29
lines changed

7 files changed

+93
-29
lines changed

.travis.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
addons:
2+
apt:
3+
packages:
4+
- git
5+
- make
6+
- curl
7+
8+
install:
9+
- make init
10+
11+
script:
12+
- make terraform:install
13+
- make terraform:get-plugins
14+
- make terraform:get-modules
15+
- make terraform:lint
16+
- make terraform:validate

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
SHELL := /bin/bash
2+
3+
-include $(shell curl -sSL -o .build-harness "https://git.io/build-harness"; echo .build-harness)
4+
5+
lint:
6+
$(SELF) terraform:install terraform:get-modules terraform:get-plugins terraform:lint terraform:validate

README.md

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# terraform-aws-ec2-instance
1+
# terraform-aws-ec2-instance [![Build Status](https://travis-ci.org/cloudposse/terraform-aws-ec2-instance.svg)](https://travis-ci.org/cloudposse/terraform-aws-ec2-instance)
22

33
Terraform Module for providing a server capable of running admin tasks. Use `terraform-aws-ec2-instance` to create and manage an admin instance.
44

@@ -18,7 +18,7 @@ module "admin_tier" {
1818
instance_type = "${var.instance_type}"
1919
vpc_id = "${var.vpc_id}"
2020
security_groups = ["${var.security_groups}"]
21-
subnets = ["${var.subnets}"]
21+
subnet = ["${var.subnet}"]
2222
associate_public_ip_address = "${var.associate_public_ip_address}"
2323
name = "${var.name}"
2424
namespace = "${var.namespace}"
@@ -62,30 +62,34 @@ resource "aws_ami_from_instance" "example" {
6262
| `instance_type` | `t2.micro` | The type of the creating instance (e.g. `t2.micro`) | No |
6363
| `vpc_id` | `` | The id of the VPC that the creating instance security group belongs to | Yes |
6464
| `security_groups` | [] | List of Security Group IDs allowed to connect to creating instance | Yes |
65-
| `subnets` | [] | List of VPC Subnet IDs creating instance launched in | Yes |
65+
| `subnet` | `` | VPC Subnet ID creating instance launched in | Yes |
6666
| `associate_public_ip_address` | `true` | Associate a public ip address with the creating instance. Boolean value | No |
67-
| `comparison_operator` | `GreaterThanOrEqualToThreshold` | Arithmetic operation to use when comparing the specified Statistic and Threshold | Yes |
68-
| `metric_name` | `StatusCheckFailed_Instance` | Name for the alarm's associated metric | Yes |
69-
| `evaluation_periods` | `5` | Number of periods over which data is compared to the specified threshold | Yes |
70-
| `metric_namespace` | `AWS/EC2` | Namespace for the alarm's associated metric | Yes |
71-
| `applying_period` | `60` | Period in seconds over which the specified statistic is applied | Yes |
72-
| `statistic_level` | `Maximum` | Statistic to apply to the alarm's associated metric | Yes |
73-
| `metric_threshold` | `1` | Value against which the specified statistic is compared | Yes |
74-
| `default_alarm_action` | `action/actions/AWS_EC2.InstanceId.Reboot/1.0` | String of action to execute when this alarm transitions into an ALARM state | Yes |
67+
| `comparison_operator` | `GreaterThanOrEqualToThreshold` | Arithmetic operation to use when comparing the specified Statistic and Threshold | No |
68+
| `metric_name` | `StatusCheckFailed_Instance` | Name for the alarm's associated metric | No |
69+
| `evaluation_periods` | `5` | Number of periods over which data is compared to the specified threshold | No |
70+
| `metric_namespace` | `AWS/EC2` | Namespace for the alarm's associated metric | No |
71+
| `applying_period` | `60` | Period in seconds over which the specified statistic is applied | No |
72+
| `statistic_level` | `Maximum` | Statistic to apply to the alarm's associated metric | No |
73+
| `metric_threshold` | `1` | Value against which the specified statistic is compared | No |
74+
| `default_alarm_action` | `action/actions/AWS_EC2.InstanceId.Reboot/1.0` | String of action to execute when this alarm transitions into an ALARM state | No |
75+
| `additional_ips_count` | `0` | Count of additional EIPs | No |
76+
7577

7678
## Outputs
7779

78-
| Name | Description |
79-
|:--------------------|:-------------------------------------------------------------------|
80-
| `id` | Disambiguated ID |
81-
| `private_dns` | Normalized name |
82-
| `private_ip` | Normalized namespace |
83-
| `public_ip` | Public IP of instance (or EIP ) |
84-
| `public_dns` | Public DNS of instance (or DNS of EIP) |
85-
| `ssh_key_pair` | Name of used AWS SSH key |
86-
| `security_group_id` | ID on the new AWS Security Group associated with creating instance |
87-
| `role` | Name of AWS IAM Role associated with creating instance |
88-
| `alarm` | CloudWatch Alarm ID |
80+
| Name | Description |
81+
|:---------------------|:-------------------------------------------------------------------|
82+
| `id` | Disambiguated ID |
83+
| `private_dns` | Normalized name |
84+
| `private_ip` | Normalized namespace |
85+
| `public_ip` | Public IP of instance (or EIP ) |
86+
| `public_dns` | Public DNS of instance (or DNS of EIP) |
87+
| `ssh_key_pair` | Name of used AWS SSH key |
88+
| `security_group_id` | ID on the new AWS Security Group associated with creating instance |
89+
| `role` | Name of AWS IAM Role associated with creating instance |
90+
| `alarm` | CloudWatch Alarm ID |
91+
| `additional_eni_ids` | Map of ENI with EIP |
92+
8993

9094
## References
9195
* Thanks to https://github.com/cloudposse/tf_bastion for the inspiration

eni.tf

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
locals {
2+
additional_ips_count = "${var.associate_public_ip_address && var.instance_enabled && var.additional_ips_count != "0" ? var.additional_ips_count : 0}"
3+
}
4+
5+
resource "aws_network_interface" "additional" {
6+
count = "${local.additional_ips_count}"
7+
subnet_id = "${var.subnet}"
8+
9+
security_groups = [
10+
"${compact(concat(list(var.create_default_security_group ? join("", aws_security_group.default.*.id) : ""), var.security_groups))}",
11+
]
12+
13+
tags {
14+
Name = "${module.label.id}"
15+
Namespace = "${var.namespace}"
16+
Stage = "${var.stage}"
17+
}
18+
}
19+
20+
resource "aws_network_interface_attachment" "additional" {
21+
count = "${local.additional_ips_count}"
22+
instance_id = "${aws_instance.default.id}"
23+
network_interface_id = "${aws_network_interface.additional.*.id[count.index]}"
24+
device_index = "${1 + count.index}"
25+
}
26+
27+
resource "aws_eip" "additional" {
28+
count = "${local.additional_ips_count}"
29+
vpc = "true"
30+
network_interface = "${aws_network_interface.additional.*.id[count.index]}"
31+
}

main.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ resource "aws_instance" "default" {
106106

107107
key_name = "${var.ssh_key_pair}"
108108

109-
subnet_id = "${var.subnets[0]}"
109+
subnet_id = "${var.subnet}"
110110

111111
tags {
112112
Name = "${module.label.id}"
@@ -116,9 +116,9 @@ resource "aws_instance" "default" {
116116
}
117117

118118
resource "aws_eip" "default" {
119-
count = "${var.associate_public_ip_address && var.instance_enabled ? 1 : 0}"
120-
instance = "${aws_instance.default.id}"
121-
vpc = true
119+
count = "${var.associate_public_ip_address && var.instance_enabled ? 1 : 0}"
120+
network_interface = "${aws_instance.default.primary_network_interface_id}"
121+
vpc = "true"
122122
}
123123

124124
# Restart dead or hung instance

outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@ output "role" {
3333
output "alarm" {
3434
value = "${join("", aws_cloudwatch_metric_alarm.default.*.id)}"
3535
}
36+
37+
output "additional_eni_ids" {
38+
value = "${zipmap(aws_network_interface.additional.*.id, aws_eip.additional.*.public_ip)}"
39+
}

variables.tf

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ variable "security_groups" {
2121
default = []
2222
}
2323

24-
variable "subnets" {
25-
type = "list"
26-
}
24+
variable "subnet" {}
2725

2826
variable "namespace" {}
2927

@@ -110,3 +108,8 @@ variable "instance_enabled" {
110108
description = "Flag for creating an instance. Set to false if it is necessary to skip instance creation"
111109
default = "true"
112110
}
111+
112+
variable "additional_ips_count" {
113+
description = "Count of additional EIPs"
114+
default = "0"
115+
}

0 commit comments

Comments
 (0)