Skip to content

Commit b5ded90

Browse files
authored
Allow associating a public IP to the instance without assigning an Elastic IP (#25)
* Update variable descriptions * Update README * Add `assign_eip_address` to enable/disable assigning EIP to the instance regardless of public IP
1 parent 7f393e7 commit b5ded90

File tree

3 files changed

+54
-48
lines changed

3 files changed

+54
-48
lines changed

README.md

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -73,29 +73,30 @@ resource "aws_ami_from_instance" "example" {
7373
| `name` | `` | Name (e.g. `bastion` or `db`) | Yes |
7474
| `attributes` | `[]` | Additional attributes (e.g. `policy` or `role`) | No |
7575
| `tags` | `{}` | Additional tags (e.g. `map("BusinessUnit","XYZ")` | No |
76-
| `ami` | `` | By default it is an AMI provided by Amazon with Ubuntu 16.04 | No |
77-
| `instance_enabled` | `true` | Flag for creating an instance. Set to false if it is necessary to skip instance creation | No |
78-
| `create_default_security_group` | `true` | Flag for creation default Security Group with Egress traffic allowed only | No |
79-
| `ssh_key_pair` | `` | SSH key pair to be provisioned on instance | Yes |
80-
| `instance_type` | `t2.micro` | The type of the creating instance (e.g. `t2.micro`) | No |
81-
| `vpc_id` | `` | The ID of the VPC that the creating instance security group belongs to | Yes |
82-
| `security_groups` | `[]` | List of Security Group IDs allowed to connect to creating instance | Yes |
83-
| `allowed_ports` | `[]` | List of allowed ingress ports e.g. ["22", "80", "443"] | No |
84-
| `subnet` | `` | VPC Subnet ID creating instance launched in | Yes |
85-
| `associate_public_ip_address` | `true` | Associate a public ip address with the creating instance. Boolean value | No |
76+
| `ami` | `` | By default it is the AMI provided by Amazon with Ubuntu 16.04 | No |
77+
| `instance_enabled` | `true` | Flag to control the instance creation. Set to false if it is necessary to skip instance creation | No |
78+
| `create_default_security_group` | `true` | Create default Security Group with only Egress traffic allowed | No |
79+
| `ssh_key_pair` | `` | SSH key pair to be provisioned on the instance | Yes |
80+
| `instance_type` | `t2.micro` | The type of the instance (e.g. `t2.micro`) | No |
81+
| `vpc_id` | `` | The ID of the VPC that the instance security group belongs to | Yes |
82+
| `security_groups` | `[]` | List of Security Group IDs allowed to connect to the instance | Yes |
83+
| `allowed_ports` | `[]` | List of allowed ingress ports, _e.g._ ["22", "80", "443"] | No |
84+
| `subnet` | `` | VPC Subnet ID the instance is launched in | Yes |
85+
| `associate_public_ip_address` | `true` | Associate a public IP address with the instance | No |
86+
| `assign_eip_address` | `true` | Assign an Elastic IP address to the instance | No |
8687
| `additional_ips_count` | `0` | Count of additional EIPs | No |
87-
| `private_ip` | `` | Private IP address to associate with the instance in a VPC | No |
88+
| `private_ip` | `` | Private IP address to associate with the instance in the VPC | No |
8889
| `source_dest_check` | `true` | Controls if traffic is routed to the instance when the destination address does not match the instance | No |
8990
| `ipv6_address_count` | `0` | Number of IPv6 addresses to associate with the primary network interface | No |
9091
| `ipv6_addresses` | `[]` | List of IPv6 addresses from the range of the subnet to associate with the primary network interface | No |
91-
| `root_volume_type` | `gp2` | Type of root volume. Can be `standard`, `gp2` or `io1` | No |
92+
| `root_volume_type` | `gp2` | Type of the root volume. Can be `standard`, `gp2` or `io1` | No |
9293
| `root_volume_size` | `10` | Size of the root volume in gigabytes | No |
9394
| `root_iops` | `0` | Amount of provisioned IOPS. This must be set with a `root_volume_type` of `io1` | No |
94-
| `ebs_device_name` | `[/dev/xvdb]` | Name of the ebs device to mount | No |
95-
| `ebs_volume_type` | `gp2` | Type of EBS volume. Can be standard, `gp2` or `io1` | No |
95+
| `ebs_device_name` | `[/dev/xvdb]` | Name of the EBS device to mount | No |
96+
| `ebs_volume_type` | `gp2` | Type of EBS volume. Can be `standard`, `gp2` or `io1` | No |
9697
| `ebs_volume_size` | `10` | Size of the EBS volume in gigabytes | No |
97-
| `ebs_iops` | `0` | Amount of provisioned IOPS. This must be set with a `ebs_volume_type` of `io1` | No |
98-
| `ebs_volume_count` | `0` | Count of EBS which will be attched to instance | No |
98+
| `ebs_iops` | `0` | Amount of provisioned IOPS. This must be set if `ebs_volume_type` is set to `io1` | No |
99+
| `ebs_volume_count` | `0` | Count of EBS volumes that will be attached to the instance | No |
99100
| `delete_on_termination` | `true` | Whether the volume should be destroyed on instance termination | No |
100101
| `comparison_operator` | `GreaterThanOrEqualToThreshold` | Arithmetic operation to use when comparing the specified Statistic and Threshold | No |
101102
| `metric_name` | `StatusCheckFailed_Instance` | Name for the alarm's associated metric | No |
@@ -111,16 +112,16 @@ resource "aws_ami_from_instance" "example" {
111112
| Name | Description |
112113
|:-------------------------------|:-------------------------------------------------------------------|
113114
| `id` | Disambiguated ID |
114-
| `private_dns` | Private DNS of instance |
115-
| `private_ip` | Private IP of instance |
116-
| `public_ip` | Public IP of instance (or EIP ) |
117-
| `public_dns` | Public DNS of instance (or DNS of EIP) |
115+
| `private_dns` | Private DNS of the instance |
116+
| `private_ip` | Private IP of the instance |
117+
| `public_ip` | Public IP of the instance (or EIP ) |
118+
| `public_dns` | Public DNS of the instance (or DNS of EIP) |
118119
| `ssh_key_pair` | Name of used AWS SSH key |
119-
| `security_group_id` | ID on the new AWS Security Group associated with creating instance |
120-
| `role` | Name of AWS IAM Role associated with creating instance |
120+
| `security_group_id` | ID of the AWS Security Group associated with the instance |
121+
| `role` | Name of the AWS IAM Role associated with the instance |
121122
| `alarm` | CloudWatch Alarm ID |
122-
| `additional_eni_ids` | Map of ENI with EIP |
123-
| `ebs_ids` | ID of EBSs |
123+
| `additional_eni_ids` | Map of ENI to EIP |
124+
| `ebs_ids` | IDs of EBSs |
124125
| `primary_network_interface_id` | ID of the instance's primary network interface |
125126
| `network_interface_id` | ID of the network interface that was created with the instance |
126127

main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@ resource "aws_instance" "default" {
117117
}
118118

119119
resource "aws_eip" "default" {
120-
count = "${var.associate_public_ip_address == "true" && var.instance_enabled == "true" ? 1 : 0}"
120+
count = "${var.associate_public_ip_address == "true" && var.assign_eip_address == "true" && var.instance_enabled == "true" ? 1 : 0}"
121121
network_interface = "${aws_instance.default.primary_network_interface_id}"
122122
vpc = "true"
123123
}
124124

125125
resource "null_resource" "eip" {
126-
count = "${var.associate_public_ip_address == "true" && var.instance_enabled == "true" ? 1 : 0}"
126+
count = "${var.associate_public_ip_address == "true" && var.assign_eip_address == "true" && var.instance_enabled == "true" ? 1 : 0}"
127127

128128
triggers {
129129
public_dns = "ec2-${replace(aws_eip.default.public_ip, ".", "-")}.${local.region == "us-east-1" ? "compute-1" : "${local.region}.compute"}.amazonaws.com"

variables.tf

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
variable "ssh_key_pair" {
2-
description = "SSH key pair to be provisioned on instance"
2+
description = "SSH key pair to be provisioned on the instance"
33
}
44

55
variable "associate_public_ip_address" {
6-
description = "Associate a public ip address with the creating instance"
6+
description = "Associate a public IP address with the instance"
7+
default = "true"
8+
}
9+
10+
variable "assign_eip_address" {
11+
description = "Assign an Elastic IP address to the instance"
712
default = "true"
813
}
914

1015
variable "user_data" {
11-
description = "The user data to provide when launching the instance. Do not pass gzip-compressed data via this argument"
16+
description = "Instance user data. Do not pass gzip-compressed data via this argument"
1217
default = ""
1318
}
1419

1520
variable "instance_type" {
16-
description = "The type of the creating instance"
21+
description = "The type of the instance"
1722
default = "t2.micro"
1823
}
1924

2025
variable "vpc_id" {
21-
description = "The ID of the VPC that the creating instance security group belongs to"
26+
description = "The ID of the VPC that the instance security group belongs to"
2227
}
2328

2429
variable "security_groups" {
25-
description = "List of Security Group IDs allowed to connect to creating instance"
30+
description = "List of Security Group IDs allowed to connect to the instance"
2631
type = "list"
2732
default = []
2833
}
@@ -34,7 +39,7 @@ variable "allowed_ports" {
3439
}
3540

3641
variable "subnet" {
37-
description = "VPC Subnet ID creating instance launched in"
42+
description = "VPC Subnet ID the instance is launched in"
3843
}
3944

4045
variable "namespace" {
@@ -71,12 +76,12 @@ variable "region" {
7176
}
7277

7378
variable "availability_zone" {
74-
description = "Availability Zone of instance launched in. If not set will be launched in frist AZ of region"
79+
description = "Availability Zone the instance is launched in. If not set, will be launched in the first AZ of the region"
7580
default = ""
7681
}
7782

7883
variable "ami" {
79-
description = "The AMI to use for the instance. By default it is an AMI provided by Amazon with Ubuntu 16.04"
84+
description = "The AMI to use for the instance. By default it is the AMI provided by Amazon with Ubuntu 16.04"
8085
default = ""
8186
}
8287

@@ -96,7 +101,7 @@ variable "monitoring" {
96101
}
97102

98103
variable "private_ip" {
99-
description = "Private IP address to associate with the instance in a VPC"
104+
description = "Private IP address to associate with the instance in the VPC"
100105
default = ""
101106
}
102107

@@ -127,23 +132,23 @@ variable "root_volume_size" {
127132
}
128133

129134
variable "root_iops" {
130-
description = "Amount of provisioned IOPS. This must be set with a root_volume_type of io1"
135+
description = "Amount of provisioned IOPS. This must be set if root_volume_type is set to `io1`"
131136
default = "0"
132137
}
133138

134139
variable "ebs_device_name" {
135140
type = "list"
136-
description = "Name of the ebs device to mount"
141+
description = "Name of the EBS device to mount"
137142
default = ["/dev/xvdb", "/dev/xvdc", "/dev/xvdd", "/dev/xvde", "/dev/xvdf", "/dev/xvdg", "/dev/xvdh", "/dev/xvdi", "/dev/xvdj", "/dev/xvdk", "/dev/xvdl", "/dev/xvdm", "/dev/xvdn", "/dev/xvdo", "/dev/xvdp", "/dev/xvdq", "/dev/xvdr", "/dev/xvds", "/dev/xvdt", "/dev/xvdu", "/dev/xvdv", "/dev/xvdw", "/dev/xvdx", "/dev/xvdy", "/dev/xvdz"]
138143
}
139144

140145
variable "ebs_volume_type" {
141-
description = "The type of ebs volume. Can be standard, gp2 or io1"
146+
description = "The type of EBS volume. Can be standard, gp2 or io1"
142147
default = "gp2"
143148
}
144149

145150
variable "ebs_volume_size" {
146-
description = "Size of the ebs volume in gigabytes"
151+
description = "Size of the EBS volume in gigabytes"
147152
default = "10"
148153
}
149154

@@ -153,7 +158,7 @@ variable "ebs_iops" {
153158
}
154159

155160
variable "ebs_volume_count" {
156-
description = "Count of EBS which will be attched to instance"
161+
description = "Count of EBS volumes that will be attached to the instance"
157162
default = "0"
158163
}
159164

@@ -172,7 +177,7 @@ variable "comparison_operator" {
172177
}
173178

174179
variable "metric_name" {
175-
description = "The name for the alarm's associated metric. Possible values you can find in https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/ec2-metricscollected.html ."
180+
description = "The name for the alarm's associated metric. Allowed values can be found in https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/ec2-metricscollected.html"
176181
default = "StatusCheckFailed_Instance"
177182
}
178183

@@ -182,22 +187,22 @@ variable "evaluation_periods" {
182187
}
183188

184189
variable "metric_namespace" {
185-
description = "The namespace for the alarm's associated metric. Possible values you can find in https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/aws-namespaces.html ."
190+
description = "The namespace for the alarm's associated metric. Allowed values can be found in https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/aws-namespaces.html"
186191
default = "AWS/EC2"
187192
}
188193

189194
variable "applying_period" {
190-
description = "The period in seconds over which the specified statistic is applied."
195+
description = "The period in seconds over which the specified statistic is applied"
191196
default = "60"
192197
}
193198

194199
variable "statistic_level" {
195-
description = "The statistic to apply to the alarm's associated metric. Possible values are: SampleCount, Average, Sum, Minimum, Maximum"
200+
description = "The statistic to apply to the alarm's associated metric. Allowed values are: SampleCount, Average, Sum, Minimum, Maximum"
196201
default = "Maximum"
197202
}
198203

199204
variable "metric_threshold" {
200-
description = "The value against which the specified statistic is compared."
205+
description = "The value against which the specified statistic is compared"
201206
default = "1"
202207
}
203208

@@ -206,12 +211,12 @@ variable "default_alarm_action" {
206211
}
207212

208213
variable "create_default_security_group" {
209-
description = "Create default Security Group with Egress traffic allowed only"
214+
description = "Create default Security Group with only Egress traffic allowed"
210215
default = "true"
211216
}
212217

213218
variable "instance_enabled" {
214-
description = "Flag for creating an instance. Set to false if it is necessary to skip instance creation"
219+
description = "Flag to control the instance creation. Set to false if it is necessary to skip instance creation"
215220
default = "true"
216221
}
217222

0 commit comments

Comments
 (0)