Skip to content

Commit 57cdd5d

Browse files
feat: Added parameter to attach aws_eip (#391)
1 parent 4f8387d commit 57cdd5d

File tree

6 files changed

+57
-10
lines changed

6 files changed

+57
-10
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ No modules.
179179

180180
| Name | Type |
181181
|------|------|
182+
| [aws_eip.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eip) | resource |
182183
| [aws_iam_instance_profile.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_instance_profile) | resource |
183184
| [aws_iam_role.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
184185
| [aws_iam_role_policy_attachment.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
@@ -203,12 +204,15 @@ No modules.
203204
| <a name="input_cpu_options"></a> [cpu\_options](#input\_cpu\_options) | Defines CPU options to apply to the instance at launch time. | `any` | `{}` | no |
204205
| <a name="input_cpu_threads_per_core"></a> [cpu\_threads\_per\_core](#input\_cpu\_threads\_per\_core) | Sets the number of CPU threads per core for an instance (has no effect unless cpu\_core\_count is also set) | `number` | `null` | no |
205206
| <a name="input_create"></a> [create](#input\_create) | Whether to create an instance | `bool` | `true` | no |
207+
| <a name="input_create_eip"></a> [create\_eip](#input\_create\_eip) | Determines whether a public EIP will be created and associated with the instance. | `bool` | `false` | no |
206208
| <a name="input_create_iam_instance_profile"></a> [create\_iam\_instance\_profile](#input\_create\_iam\_instance\_profile) | Determines whether an IAM instance profile is created or to use an existing IAM instance profile | `bool` | `false` | no |
207209
| <a name="input_create_spot_instance"></a> [create\_spot\_instance](#input\_create\_spot\_instance) | Depicts if the instance is a spot instance | `bool` | `false` | no |
208210
| <a name="input_disable_api_stop"></a> [disable\_api\_stop](#input\_disable\_api\_stop) | If true, enables EC2 Instance Stop Protection | `bool` | `null` | no |
209211
| <a name="input_disable_api_termination"></a> [disable\_api\_termination](#input\_disable\_api\_termination) | If true, enables EC2 Instance Termination Protection | `bool` | `null` | no |
210212
| <a name="input_ebs_block_device"></a> [ebs\_block\_device](#input\_ebs\_block\_device) | Additional EBS block devices to attach to the instance | `list(any)` | `[]` | no |
211213
| <a name="input_ebs_optimized"></a> [ebs\_optimized](#input\_ebs\_optimized) | If true, the launched EC2 instance will be EBS-optimized | `bool` | `null` | no |
214+
| <a name="input_eip_domain"></a> [eip\_domain](#input\_eip\_domain) | Indicates if this EIP is for use in VPC | `string` | `"vpc"` | no |
215+
| <a name="input_eip_tags"></a> [eip\_tags](#input\_eip\_tags) | A map of additional tags to add to the eip | `map(string)` | `{}` | no |
212216
| <a name="input_enable_volume_tags"></a> [enable\_volume\_tags](#input\_enable\_volume\_tags) | Whether to enable volume tags (if enabled it conflicts with root\_block\_device tags) | `bool` | `true` | no |
213217
| <a name="input_enclave_options_enabled"></a> [enclave\_options\_enabled](#input\_enclave\_options\_enabled) | Whether Nitro Enclaves will be enabled on the instance. Defaults to `false` | `bool` | `null` | no |
214218
| <a name="input_ephemeral_block_device"></a> [ephemeral\_block\_device](#input\_ephemeral\_block\_device) | Customize Ephemeral (also known as Instance Store) volumes on the instance | `list(map(string))` | `[]` | no |
@@ -286,7 +290,7 @@ No modules.
286290
| <a name="output_private_dns"></a> [private\_dns](#output\_private\_dns) | The private DNS name assigned to the instance. Can only be used inside the Amazon EC2, and only available if you've enabled DNS hostnames for your VPC |
287291
| <a name="output_private_ip"></a> [private\_ip](#output\_private\_ip) | The private IP address assigned to the instance |
288292
| <a name="output_public_dns"></a> [public\_dns](#output\_public\_dns) | The public DNS name assigned to the instance. For EC2-VPC, this is only available if you've enabled DNS hostnames for your VPC |
289-
| <a name="output_public_ip"></a> [public\_ip](#output\_public\_ip) | The public IP address assigned to the instance, if applicable. NOTE: If you are using an aws\_eip with your instance, you should refer to the EIP's address directly and not use `public_ip` as this field will change after the EIP is attached |
293+
| <a name="output_public_ip"></a> [public\_ip](#output\_public\_ip) | The public IP address assigned to the instance, if applicable. |
290294
| <a name="output_root_block_device"></a> [root\_block\_device](#output\_root\_block\_device) | Root block device information |
291295
| <a name="output_spot_bid_status"></a> [spot\_bid\_status](#output\_spot\_bid\_status) | The current bid status of the Spot Instance Request |
292296
| <a name="output_spot_instance_id"></a> [spot\_instance\_id](#output\_spot\_instance\_id) | The Instance ID (if any) that is currently fulfilling the Spot Instance request |

examples/complete/main.tf

+8-8
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ module "ec2_complete" {
3232

3333
name = local.name
3434

35-
ami = data.aws_ami.amazon_linux.id
36-
instance_type = "c5.xlarge" # used to set core count below
37-
availability_zone = element(module.vpc.azs, 0)
38-
subnet_id = element(module.vpc.private_subnets, 0)
39-
vpc_security_group_ids = [module.security_group.security_group_id]
40-
placement_group = aws_placement_group.web.id
41-
associate_public_ip_address = true
42-
disable_api_stop = false
35+
ami = data.aws_ami.amazon_linux.id
36+
instance_type = "c5.xlarge" # used to set core count below
37+
availability_zone = element(module.vpc.azs, 0)
38+
subnet_id = element(module.vpc.private_subnets, 0)
39+
vpc_security_group_ids = [module.security_group.security_group_id]
40+
placement_group = aws_placement_group.web.id
41+
create_eip = true
42+
disable_api_stop = false
4343

4444
create_iam_instance_profile = true
4545
iam_role_description = "IAM role for EC2 instance"

main.tf

+17
Original file line numberDiff line numberDiff line change
@@ -603,3 +603,20 @@ resource "aws_iam_instance_profile" "this" {
603603
create_before_destroy = true
604604
}
605605
}
606+
607+
################################################################################
608+
# Elastic IP
609+
################################################################################
610+
611+
resource "aws_eip" "this" {
612+
count = local.create && var.create_eip && !var.create_spot_instance ? 1 : 0
613+
614+
instance = try(
615+
aws_instance.this[0].id,
616+
aws_instance.ignore_ami[0].id,
617+
)
618+
619+
domain = var.eip_domain
620+
621+
tags = merge(var.tags, var.eip_tags)
622+
}

outputs.tf

+2-1
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ output "public_dns" {
8989
}
9090

9191
output "public_ip" {
92-
description = "The public IP address assigned to the instance, if applicable. NOTE: If you are using an aws_eip with your instance, you should refer to the EIP's address directly and not use `public_ip` as this field will change after the EIP is attached"
92+
description = "The public IP address assigned to the instance, if applicable."
9393
value = try(
94+
aws_eip.this[0].public_ip,
9495
aws_instance.this[0].public_ip,
9596
aws_instance.ignore_ami[0].public_ip,
9697
aws_spot_instance_request.this[0].public_ip,

variables.tf

+22
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,25 @@ variable "iam_role_tags" {
408408
type = map(string)
409409
default = {}
410410
}
411+
412+
################################################################################
413+
# Elastic IP
414+
################################################################################
415+
416+
variable "create_eip" {
417+
description = "Determines whether a public EIP will be created and associated with the instance."
418+
type = bool
419+
default = false
420+
}
421+
422+
variable "eip_domain" {
423+
description = "Indicates if this EIP is for use in VPC"
424+
type = string
425+
default = "vpc"
426+
}
427+
428+
variable "eip_tags" {
429+
description = "A map of additional tags to add to the eip"
430+
type = map(string)
431+
default = {}
432+
}

wrappers/main.tf

+3
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ module "wrapper" {
1313
cpu_options = try(each.value.cpu_options, var.defaults.cpu_options, {})
1414
cpu_threads_per_core = try(each.value.cpu_threads_per_core, var.defaults.cpu_threads_per_core, null)
1515
create = try(each.value.create, var.defaults.create, true)
16+
create_eip = try(each.value.create_eip, var.defaults.create_eip, false)
1617
create_iam_instance_profile = try(each.value.create_iam_instance_profile, var.defaults.create_iam_instance_profile, false)
1718
create_spot_instance = try(each.value.create_spot_instance, var.defaults.create_spot_instance, false)
1819
disable_api_stop = try(each.value.disable_api_stop, var.defaults.disable_api_stop, null)
1920
disable_api_termination = try(each.value.disable_api_termination, var.defaults.disable_api_termination, null)
2021
ebs_block_device = try(each.value.ebs_block_device, var.defaults.ebs_block_device, [])
2122
ebs_optimized = try(each.value.ebs_optimized, var.defaults.ebs_optimized, null)
23+
eip_domain = try(each.value.eip_domain, var.defaults.eip_domain, "vpc")
24+
eip_tags = try(each.value.eip_tags, var.defaults.eip_tags, {})
2225
enable_volume_tags = try(each.value.enable_volume_tags, var.defaults.enable_volume_tags, true)
2326
enclave_options_enabled = try(each.value.enclave_options_enabled, var.defaults.enclave_options_enabled, null)
2427
ephemeral_block_device = try(each.value.ephemeral_block_device, var.defaults.ephemeral_block_device, [])

0 commit comments

Comments
 (0)