diff --git a/README.md b/README.md index 8f22cb7d..d9f0fa28 100644 --- a/README.md +++ b/README.md @@ -179,6 +179,7 @@ No modules. | Name | Type | |------|------| +| [aws_eip.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eip) | resource | | [aws_iam_instance_profile.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_instance_profile) | resource | | [aws_iam_role.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource | | [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. | [cpu\_options](#input\_cpu\_options) | Defines CPU options to apply to the instance at launch time. | `any` | `{}` | no | | [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 | | [create](#input\_create) | Whether to create an instance | `bool` | `true` | no | +| [create\_eip](#input\_create\_eip) | Determines whether a public EIP will be created and associated with the instance. | `bool` | `false` | no | | [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 | | [create\_spot\_instance](#input\_create\_spot\_instance) | Depicts if the instance is a spot instance | `bool` | `false` | no | | [disable\_api\_stop](#input\_disable\_api\_stop) | If true, enables EC2 Instance Stop Protection | `bool` | `null` | no | | [disable\_api\_termination](#input\_disable\_api\_termination) | If true, enables EC2 Instance Termination Protection | `bool` | `null` | no | | [ebs\_block\_device](#input\_ebs\_block\_device) | Additional EBS block devices to attach to the instance | `list(any)` | `[]` | no | | [ebs\_optimized](#input\_ebs\_optimized) | If true, the launched EC2 instance will be EBS-optimized | `bool` | `null` | no | +| [eip\_domain](#input\_eip\_domain) | Indicates if this EIP is for use in VPC | `string` | `"vpc"` | no | +| [eip\_tags](#input\_eip\_tags) | A map of additional tags to add to the eip | `map(string)` | `{}` | no | | [enable\_volume\_tags](#input\_enable\_volume\_tags) | Whether to enable volume tags (if enabled it conflicts with root\_block\_device tags) | `bool` | `true` | no | | [enclave\_options\_enabled](#input\_enclave\_options\_enabled) | Whether Nitro Enclaves will be enabled on the instance. Defaults to `false` | `bool` | `null` | no | | [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. | [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 | | [private\_ip](#output\_private\_ip) | The private IP address assigned to the instance | | [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 | -| [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 | +| [public\_ip](#output\_public\_ip) | The public IP address assigned to the instance, if applicable. | | [root\_block\_device](#output\_root\_block\_device) | Root block device information | | [spot\_bid\_status](#output\_spot\_bid\_status) | The current bid status of the Spot Instance Request | | [spot\_instance\_id](#output\_spot\_instance\_id) | The Instance ID (if any) that is currently fulfilling the Spot Instance request | diff --git a/examples/complete/main.tf b/examples/complete/main.tf index f14dfd9a..465e7902 100644 --- a/examples/complete/main.tf +++ b/examples/complete/main.tf @@ -32,14 +32,14 @@ module "ec2_complete" { name = local.name - ami = data.aws_ami.amazon_linux.id - instance_type = "c5.xlarge" # used to set core count below - availability_zone = element(module.vpc.azs, 0) - subnet_id = element(module.vpc.private_subnets, 0) - vpc_security_group_ids = [module.security_group.security_group_id] - placement_group = aws_placement_group.web.id - associate_public_ip_address = true - disable_api_stop = false + ami = data.aws_ami.amazon_linux.id + instance_type = "c5.xlarge" # used to set core count below + availability_zone = element(module.vpc.azs, 0) + subnet_id = element(module.vpc.private_subnets, 0) + vpc_security_group_ids = [module.security_group.security_group_id] + placement_group = aws_placement_group.web.id + create_eip = true + disable_api_stop = false create_iam_instance_profile = true iam_role_description = "IAM role for EC2 instance" diff --git a/main.tf b/main.tf index 2f291307..749e67d4 100644 --- a/main.tf +++ b/main.tf @@ -603,3 +603,20 @@ resource "aws_iam_instance_profile" "this" { create_before_destroy = true } } + +################################################################################ +# Elastic IP +################################################################################ + +resource "aws_eip" "this" { + count = local.create && var.create_eip && !var.create_spot_instance ? 1 : 0 + + instance = try( + aws_instance.this[0].id, + aws_instance.ignore_ami[0].id, + ) + + domain = var.eip_domain + + tags = merge(var.tags, var.eip_tags) +} diff --git a/outputs.tf b/outputs.tf index b10773d4..3f57b650 100644 --- a/outputs.tf +++ b/outputs.tf @@ -89,8 +89,9 @@ output "public_dns" { } output "public_ip" { - 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" + description = "The public IP address assigned to the instance, if applicable." value = try( + aws_eip.this[0].public_ip, aws_instance.this[0].public_ip, aws_instance.ignore_ami[0].public_ip, aws_spot_instance_request.this[0].public_ip, diff --git a/variables.tf b/variables.tf index d5b8bc0e..38a1b5b2 100644 --- a/variables.tf +++ b/variables.tf @@ -408,3 +408,25 @@ variable "iam_role_tags" { type = map(string) default = {} } + +################################################################################ +# Elastic IP +################################################################################ + +variable "create_eip" { + description = "Determines whether a public EIP will be created and associated with the instance." + type = bool + default = false +} + +variable "eip_domain" { + description = "Indicates if this EIP is for use in VPC" + type = string + default = "vpc" +} + +variable "eip_tags" { + description = "A map of additional tags to add to the eip" + type = map(string) + default = {} +} diff --git a/wrappers/main.tf b/wrappers/main.tf index 494d2cbc..9ba0cdb4 100644 --- a/wrappers/main.tf +++ b/wrappers/main.tf @@ -13,12 +13,15 @@ module "wrapper" { cpu_options = try(each.value.cpu_options, var.defaults.cpu_options, {}) cpu_threads_per_core = try(each.value.cpu_threads_per_core, var.defaults.cpu_threads_per_core, null) create = try(each.value.create, var.defaults.create, true) + create_eip = try(each.value.create_eip, var.defaults.create_eip, false) create_iam_instance_profile = try(each.value.create_iam_instance_profile, var.defaults.create_iam_instance_profile, false) create_spot_instance = try(each.value.create_spot_instance, var.defaults.create_spot_instance, false) disable_api_stop = try(each.value.disable_api_stop, var.defaults.disable_api_stop, null) disable_api_termination = try(each.value.disable_api_termination, var.defaults.disable_api_termination, null) ebs_block_device = try(each.value.ebs_block_device, var.defaults.ebs_block_device, []) ebs_optimized = try(each.value.ebs_optimized, var.defaults.ebs_optimized, null) + eip_domain = try(each.value.eip_domain, var.defaults.eip_domain, "vpc") + eip_tags = try(each.value.eip_tags, var.defaults.eip_tags, {}) enable_volume_tags = try(each.value.enable_volume_tags, var.defaults.enable_volume_tags, true) enclave_options_enabled = try(each.value.enclave_options_enabled, var.defaults.enclave_options_enabled, null) ephemeral_block_device = try(each.value.ephemeral_block_device, var.defaults.ephemeral_block_device, [])