Search code examples
amazon-web-servicesamazon-amipacker

Building AMI with imds v2 enabled from packer


Trying to enable imds v2 for the AMI built by Packer, as per Hashicorp website, there is a attribute for imds named "imds_support = v2.0" but if I add imds_support, packer build fails.

source "amazon-ebs" "ubuntu-builder-1" {
  ami_name        = "ubuntu-AMI-2023-04-24T06-01-48Z"
  skip_create_ami = false
  instance_type   = "t2.micro"
  communicator    = "ssh"
  region          = "us-east-1"
  subnet_id       = "subnet-99999"
  source_ami_filter {
    filters = {
      name                = "EC2-Ubuntu18.04-*"
      root-device-type    = "ebs"
      virtualization-type = "hvm"

    }
    most_recent = true
    owners      = ["22222"]
  }
  ami_regions = []
  ami_users   = []
  winrm_username                        = "Administrator"
  ssh_username                          = "ubuntu"
  imds_support                          = "v2.0"
  aws_polling {
    delay_seconds = 120
    max_attempts  = 60
  }
  metadata_options {
    http_endpoint               = "enabled"
    http_put_response_hop_limit = "1"
    http_tokens                 = "required"
  }
  force_deregister      = false
  force_delete_snapshot = false
  iam_instance_profile = "AmazonSSMRoleForInstancesQuickSetup"

}

Error message when we do code build - An argument named "imds_support" is not expected here

Referred packer document - https://developer.hashicorp.com/packer/plugins/builders/amazon/ebs#imds_support


Solution

  • The support for imds in EBS in the Packer Amazon plugin was added in version 1.2.2. You would need to ensure your installed version of the plugin is at least at that version:

    packer {
      required_plugins {
        amazon = {
          version = ">= 1.2.2" # preferably "~> 1.2.0" for latest patch version
          source = "github.com/hashicorp/amazon"
        }
      }
    }