packer-plugin-amazon icon indicating copy to clipboard operation
packer-plugin-amazon copied to clipboard

add elastic_ip_allocation_id support to EBS builders

Open tjnicholas opened this issue 8 months ago • 4 comments

Description

Introduces a new elastic_ip_allocation_id string field on RunConfig that allows users to associate a pre-existing Elastic IP with the Packer build instance immediately after launch.

This is useful when the build host, or other build time dependency, needs to be reachable from a known, stable public IP — for example, to satisfy firewall allowlists without relying on an auto-assigned address.

Implementation details:

  • Adds StepAssociateEIP to the multistep pipeline in both the ebs and ebssurrogate builders, positioned after instance launch and before password/SSH steps so the EIP is in place before any connection is attempted.
  • On Run, the step calls AssociateAddress then re-describes the instance and writes the refreshed object back to the state bag, ensuring SSHHost picks up the EIP as PublicIpAddress.
  • On Cleanup, the step calls DisassociateAddress (but does not release the EIP — it belongs to the caller).
  • Extends the Ec2Client interface with AssociateAddress and DisassociateAddress; the real *ec2.Client already satisfies both.
  • Validates that the value matches ^eipalloc-[0-9a-f]+$ and that it is not combined with associate_public_ip_address, which would be redundant and ambiguous.

Resolved Issues

This addresses the AWS portion of #36 , but not the Azure portion. Closes #558, or rather enables a useful answer.

Rollback Plan

If a change needs to be reverted, we will roll out an update to the code within 7 days.

tjnicholas avatar Mar 18 '26 02:03 tjnicholas

CLA assistant check
All committers have signed the CLA.

hashicorp-cla-app[bot] avatar Mar 18 '26 02:03 hashicorp-cla-app[bot]

CLA assistant check

Thank you for your submission! We require that all contributors sign our Contributor License Agreement ("CLA") before we can accept the contribution. Read and sign the agreement

Learn more about why HashiCorp requires a CLA and what the CLA includes

Have you signed the CLA already but the status is still pending? Recheck it.

hashicorp-cla-app[bot] avatar Mar 18 '26 02:03 hashicorp-cla-app[bot]

I've tested that this change functions as I expect using a basic packer config. The specified EIP is associated and disassociated as expected.

I do want to note that this change was developed using Claude. I'm not a Go programmer so do take it with an appropriate grain of salt.

data "amazon-ami" "autogenerated_1" {
  filters = {
    architecture        = "x86_64"
    name                = "amzn2-ami-hvm-2.0.*"
    root-device-type    = "ebs"
    virtualization-type = "hvm"
  }
  most_recent = true
  owners      = ["137112412989"] # amazon
  region      = "ap-southeast-2"
}
source "amazon-ebs" "eip_test" {
  region        = "ap-southeast-2"
  instance_type = "t3.micro"
  source_ami    = "${data.amazon-ami.autogenerated_1.id}"
  ssh_username  = "ec2-user"
  ami_name      = "packer-eip-test-{{timestamp}}"
  elastic_ip_allocation_id = "eipalloc-0123456789abcdefg"
  subnet_id    = "subnet-0123456789abcdefg"
  temporary_security_group_source_public_ip = true
}
build {
  sources = ["source.amazon-ebs.eip_test"]
}

tjnicholas avatar Mar 18 '26 02:03 tjnicholas

Anything I can add to this to make merging more attractive? I wonder, for example, if it would be better to be able to specify a list of eip IDs. That would make it easier to have multiple packer runs happen in parallel.

tjnicholas avatar Apr 12 '26 21:04 tjnicholas