hashicorp / packer-plugin-amazon

Packer plugin for Amazon AMI Builder
https://www.packer.io/docs/builders/amazon
Mozilla Public License 2.0
69 stars 104 forks source link

Error Encountered When Building AMI with capacity_reservation_id #473

Closed satya-kotari closed 1 month ago

satya-kotari commented 2 months ago

Error Encountered When Building AMI with capacity_reservation_id

I'm currently attempting to utilize the capacity_reservation_id attribute in Packer to launch the template. However, I keep encountering the following error: _Build 'amazon-ebs.autogenerated1' errored after 1 second 875 milliseconds: Error launching source instance: InvalidParameterCombination: The request can include either a CapacityReservationId or a CapacityReservationPreference but not both.

Reproduction Steps

Steps to reproduce this issue

Packer version

1.10.2

Simplified Packer Template
packer {
  required_version = ">= 1.10.2"
  required_plugins {
    amazon = {
      source  = "github.com/hashicorp/amazon"
      version = ">= 1.3.1"
    }
    ansible = {
      source  = "github.com/hashicorp/ansible"
      version = "~> 1"
    }
  }

source "amazon-ebs" "autogenerated_1" {
  ami_description = "Image for P5."
  ami_name        = "Ami-p5test"
  max_retries = 1000
  capacity_reservation_id = "cr-0908086"
  iam_instance_profile    = "PackerBuildRole_profile"
  instance_type           = "p5.48xlarge"
  launch_block_device_mappings {
    delete_on_termination = true
    device_name           = "/dev/sda1"
    volume_size           = 300
    volume_type           = "gp2"
  }
  region       = "us-east-1"
  source_ami   = "${data.amazon-ami.autogenerated_1.id}"
  ssh_username = "ubuntu"
  subnet_id    = "subnet-04XXX"
  vpc_id = "vpc-XXX"
}

The problem lies in the code block between lines 890 and 892 of the following file: https://github.com/hashicorp/packer-plugin-amazon/blob/01cd50e05c90d905af09cc298b24011ec3227cd9/builder/common/run_config.go. Ideally, we shouldn't set it to "none" because doing so disables the use of the capacity_reservation_id. According to AWS, we can only pass one parameter in this scenario. Ref: https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CapacityReservationSpecification.html

Describes an instance's Capacity Reservation targeting option. You can specify only one parameter at a time. If you specify CapacityReservationPreference and CapacityReservationTarget, the request fails

Any help would be much appreciated.

Thank you!

lbajolet-hashicorp commented 1 month ago

Hey @satya-kotari,

Sorry I've not answered your issue earlier, missed it entierly.

This does look like a bug, I'll take a look at the plugin ASAP, and if it is indeed a bug on our end, I'll try to push a fix then.

Thanks for reporting this!

lbajolet-hashicorp commented 1 month ago

Hi @satya-kotari,

I've opened a PR that should address your case, I've added a bunch of unit tests for Prepare, but I'm a firm believer in real-life testing :)

Would you be able to build the plugin and test building your configuration with it? If the change I made works, I believe this should fix your issue, but please let me know if this is really the case.

Thanks in advance!