Skip to content

Commit

Permalink
added parameter to attach aws_eip
Browse files Browse the repository at this point in the history
  • Loading branch information
tokarev-artem committed May 4, 2024
1 parent 4f8387d commit cf9afee
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 10 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand All @@ -203,12 +204,14 @@ No modules.
| <a name="input_cpu_options"></a> [cpu\_options](#input\_cpu\_options) | Defines CPU options to apply to the instance at launch time. | `any` | `{}` | no |
| <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 |
| <a name="input_create"></a> [create](#input\_create) | Whether to create an instance | `bool` | `true` | no |
| <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 |
| <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 |
| <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 |
| <a name="input_disable_api_stop"></a> [disable\_api\_stop](#input\_disable\_api\_stop) | If true, enables EC2 Instance Stop Protection | `bool` | `null` | no |
| <a name="input_disable_api_termination"></a> [disable\_api\_termination](#input\_disable\_api\_termination) | If true, enables EC2 Instance Termination Protection | `bool` | `null` | no |
| <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 |
| <a name="input_ebs_optimized"></a> [ebs\_optimized](#input\_ebs\_optimized) | If true, the launched EC2 instance will be EBS-optimized | `bool` | `null` | no |
| <a name="input_eip_public_domain"></a> [eip\_public\_domain](#input\_eip\_public\_domain) | Indicates if this EIP is for use in VPC | `string` | `"vpc"` | no |
| <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 |
| <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 |
| <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 |
Expand Down Expand Up @@ -286,7 +289,7 @@ No modules.
| <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 |
| <a name="output_private_ip"></a> [private\_ip](#output\_private\_ip) | The private IP address assigned to the instance |
| <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 |
| <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 |
| <a name="output_public_ip"></a> [public\_ip](#output\_public\_ip) | The public IP address assigned to the instance, if applicable. |
| <a name="output_root_block_device"></a> [root\_block\_device](#output\_root\_block\_device) | Root block device information |
| <a name="output_spot_bid_status"></a> [spot\_bid\_status](#output\_spot\_bid\_status) | The current bid status of the Spot Instance Request |
| <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 |
Expand Down
16 changes: 8 additions & 8 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 17 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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_public_domain

tags = merge(var.tags)
}
3 changes: 2 additions & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,18 @@ 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_public_domain" {
description = "Indicates if this EIP is for use in VPC"
default = "vpc"
type = string
}
2 changes: 2 additions & 0 deletions wrappers/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ 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_public_domain = try(each.value.eip_public_domain, var.defaults.eip_public_domain, "vpc")
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, [])
Expand Down

0 comments on commit cf9afee

Please sign in to comment.