Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create EIP association for EC2 #372

Conversation

victorcechinel
Copy link

@victorcechinel victorcechinel commented Jan 2, 2024

Description

Create a Public Elastic IP binding for EC2 instances.

Motivation and Context

With this change it is possible for the EC2 instance to be turned off and when turned on again the Public IP remains the same.

Implementation result:

terraform plan --target module.ec2_complete --target aws_eip.public --target aws_instance.this    

module.ec2_complete.data.aws_partition.current: Reading...
data.aws_ami.amazon_linux: Reading...
data.aws_availability_zones.available: Reading...
module.ec2_complete.data.aws_partition.current: Read complete after 0s [id=aws]
module.ec2_complete.data.aws_iam_policy_document.assume_role_policy[0]: Reading...
module.ec2_complete.data.aws_iam_policy_document.assume_role_policy[0]: Read complete after 0s [id=1256122602]
data.aws_availability_zones.available: Read complete after 1s [id=eu-west-1]
data.aws_ami.amazon_linux: Read complete after 1s [id=ami-05b5ad12f26245cad]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # module.ec2_complete.aws_eip.public[0] will be created
  + resource "aws_eip" "public" {
      + allocation_id        = (known after apply)
      + association_id       = (known after apply)
      + carrier_ip           = (known after apply)
      + customer_owned_ip    = (known after apply)
      + domain               = "vpc"
      + id                   = (known after apply)
      + instance             = (known after apply)
      + network_border_group = (known after apply)
      + network_interface    = (known after apply)
      + private_dns          = (known after apply)
      + private_ip           = (known after apply)
      + public_dns           = (known after apply)
      + public_ip            = (known after apply)
      + public_ipv4_pool     = (known after apply)
      + tags                 = {
          + "Example"    = "ex-complete"
          + "Name"       = "ex-complete"
          + "Repository" = "https://github.com/terraform-aws-modules/terraform-aws-ec2-instance"
        }
      + tags_all             = {
          + "Example"    = "ex-complete"
          + "Name"       = "ex-complete"
          + "Repository" = "https://github.com/terraform-aws-modules/terraform-aws-ec2-instance"
        }
      + vpc                  = (known after apply)
    }

  # module.ec2_complete.aws_instance.this[0] will be created
  + resource "aws_instance" "this" {
      + ami                                  = "ami-05b5ad12f26245cad"
      + arn                                  = (known after apply)
      + associate_public_ip_address          = true
      + availability_zone                    = "eu-west-1a"
      + cpu_core_count                       = (known after apply)
      + cpu_threads_per_core                 = (known after apply)
      + disable_api_stop                     = false
      + disable_api_termination              = (known after apply)
      + ebs_optimized                        = (known after apply)
      + get_password_data                    = false
      + hibernation                          = true
      + host_id                              = (known after apply)
      + host_resource_group_arn              = (known after apply)
      + iam_instance_profile                 = (known after apply)
      + id                                   = (known after apply)
      + instance_initiated_shutdown_behavior = (known after apply)
      + instance_lifecycle                   = (known after apply)
      + instance_state                       = (known after apply)
      + instance_type                        = "c5.xlarge"
      + ipv6_address_count                   = (known after apply)
      + ipv6_addresses                       = (known after apply)
      + key_name                             = (known after apply)
      + monitoring                           = (known after apply)
      + outpost_arn                          = (known after apply)
      + password_data                        = (known after apply)
      + placement_group                      = (known after apply)
      + placement_partition_number           = (known after apply)
      + primary_network_interface_id         = (known after apply)
      + private_dns                          = (known after apply)
      + private_ip                           = (known after apply)
      + public_dns                           = (known after apply)
      + public_ip                            = (known after apply)
      + secondary_private_ips                = (known after apply)
      + security_groups                      = (known after apply)
      + source_dest_check                    = true
      + spot_instance_request_id             = (known after apply)
      + subnet_id                            = (known after apply)
      + tags                                 = {
          + "Example"    = "ex-complete"
          + "Name"       = "ex-complete"
          + "Repository" = "https://github.com/terraform-aws-modules/terraform-aws-ec2-instance"
        }
      + tags_all                             = {
          + "Example"    = "ex-complete"
          + "Name"       = "ex-complete"
          + "Repository" = "https://github.com/terraform-aws-modules/terraform-aws-ec2-instance"
        }
      + tenancy                              = (known after apply)
      + user_data                            = (known after apply)
      + user_data_base64                     = "IyEvYmluL2Jhc2gKZWNobyAiSGVsbG8gVGVycmFmb3JtISIK"
      + user_data_replace_on_change          = true
      + vpc_security_group_ids               = (known after apply)

      + cpu_options {
          + amd_sev_snp      = (known after apply)
          + core_count       = 2
          + threads_per_core = 1
        }

      + credit_specification {}

      + ebs_block_device {
          + delete_on_termination = true
          + device_name           = "/dev/sdf"
          + encrypted             = true
          + iops                  = (known after apply)
          + kms_key_id            = (known after apply)
          + snapshot_id           = (known after apply)
          + tags                  = {
              + "MountPoint" = "/mnt/data"
            }
          + throughput            = 200
          + volume_id             = (known after apply)
          + volume_size           = 5
          + volume_type           = "gp3"
        }

      + enclave_options {
          + enabled = (known after apply)
        }

      + metadata_options {
          + http_endpoint               = "enabled"
          + http_protocol_ipv6          = "disabled"
          + http_put_response_hop_limit = 1
          + http_tokens                 = "optional"
          + instance_metadata_tags      = (known after apply)
        }

      + root_block_device {
          + delete_on_termination = true
          + device_name           = (known after apply)
          + encrypted             = true
          + iops                  = (known after apply)
          + kms_key_id            = (known after apply)
          + tags                  = {
              + "Name" = "my-root-block"
            }
          + throughput            = 200
          + volume_id             = (known after apply)
          + volume_size           = 50
          + volume_type           = "gp3"
        }

      + timeouts {}
    }

Plan: 12 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + ec2_complete_arn                                = (known after apply)
  + ec2_complete_availability_zone                  = "eu-west-1a"
  + ec2_complete_capacity_reservation_specification = (known after apply)
  + ec2_complete_ebs_block_device                   = (known after apply)
  + ec2_complete_ephemeral_block_device             = (known after apply)
  + ec2_complete_iam_instance_profile_arn           = (known after apply)
  + ec2_complete_iam_instance_profile_id            = (known after apply)
  + ec2_complete_iam_instance_profile_unique        = (known after apply)
  + ec2_complete_iam_role_arn                       = (known after apply)
  + ec2_complete_iam_role_name                      = (known after apply)
  + ec2_complete_iam_role_unique_id                 = (known after apply)
  + ec2_complete_id                                 = (known after apply)
  + ec2_complete_instance_state                     = (known after apply)
  + ec2_complete_primary_network_interface_id       = (known after apply)
  + ec2_complete_private_dns                        = (known after apply)
  + ec2_complete_public_dns                         = (known after apply)
  + ec2_complete_public_eip                         = (known after apply)
  + ec2_complete_public_ip                          = (known after apply)
  + ec2_complete_root_block_device                  = (known after apply)
  + ec2_complete_tags_all                           = {
      + Example    = "ex-complete"
      + Name       = "ex-complete"
      + Repository = "https://github.com/terraform-aws-modules/terraform-aws-ec2-instance"
    }
╷
│ Warning: Resource targeting is in effect
│ 
│ You are creating a plan with the -target option, which means that the result of this plan may not represent all of the changes requested by the current configuration.
│ 
│ The -target option is not for routine use, and is provided only for exceptional situations such as recovering from errors or mistakes, or when Terraform specifically suggests to use it as
│ part of an error message.
╵

─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.

Breaking Changes

How Has This Been Tested?

  • I have updated at least one of the examples/* to demonstrate and validate my change(s)
  • I have tested and validated these changes using one or more of the provided examples/* projects
  • I have executed pre-commit run -a on my pull request

@victorcechinel victorcechinel changed the title feat(add-eip-public): Create EIP association for EC2 feat: Create EIP association for EC2 Jan 2, 2024
@victorcechinel
Copy link
Author

@antonbabenko can you do the review, please?

Copy link
Member

@antonbabenko antonbabenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost great, but a few comments to make it more flexible.

@bryantbiggs WDYT?

instance = try(
aws_instance.this[0].id,
aws_instance.ignore_ami[0].id,
null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
null,

# Elastic IP
################################################################################

resource "aws_eip" "public" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
resource "aws_eip" "public" {
resource "aws_eip" "this" {

Why call it "public"? Can it be of another type?


tags = merge(var.tags, var.eip_public_tags)

depends_on = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need in "depends_on" since it is already having references in "instance" argument.

@@ -85,6 +85,8 @@ module "ec2_complete" {
}
]

create_eip_public = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a way to "create EIP", "bring your own EIP" and use aws_eip_association to manage the relations.

# Elastic IP
################################################################################

output "eip_public_id" {
Copy link
Member

@bryantbiggs bryantbiggs Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all of these variables and outputs should be stripped of public - there is no notion of a private elastic IP, its just an elastic IP (eip) and those are public

Copy link

This PR has been automatically marked as stale because it has been open 30 days
with no activity. Remove stale label or comment or this PR will be closed in 10 days

@github-actions github-actions bot added the stale label Feb 18, 2024
Copy link

This PR was automatically closed because of stale in 10 days

@github-actions github-actions bot closed this Feb 29, 2024
Copy link

github-actions bot commented Apr 1, 2024

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants