I’m using packer to create AWS AMIs from a custom built image. When starting the build, I don’t know which distribution is inside the image. I start a machine using packer (aws-chroot), mount an additional volume and copy stuff from my image to that volume.
Now after I have copied all files to the correct directories, I’d like to issue a cat /etc/os-release | grep UBUNTU_CODENAME
to find out the distribution I’m building an AMI for. And I would like to use this information as a tag attached to the AMI created.
This is how the relevant part of the packer file looks like:
source "amazon-chroot" "pc" {
# start with totally empty volume
from_scratch = true
# general stuff
ami_description = "Created from foo/packer in Build ${var.arg_build_number}"
ami_name = "${var.arg_ami_name}"
ami_regions = ["eu-central-1"]
ami_virtualization_type = "hvm"
region = "eu-central-1"
tags = {
Buildno = "${var.arg_build_number}"
Name = "my-little-ami-${var.arg_build_number}"
}
# disk type/size
nvme_device_path = "/dev/nvme1n1p"
device_path = "/dev/sdf"
root_device_name = "/dev/xvda"
root_volume_size = 3
root_volume_type = "gp3"
ami_block_device_mappings {
delete_on_termination = true
device_name = "/dev/xvda"
volume_type = "gp3"
}
# enable fast networking
ena_support = true
# force overwrite ami with same name
# .. helpful for debug runs
force_delete_snapshot = true
force_deregister = true
# prepare disk layout
pre_mount_commands = [
"parted /dev/nvme1n1 mklabel msdos mkpart primary ext4 1M 100% set 1 boot on print",
"mkfs.ext4 /dev/nvme1n1p1",
]
# run actual host setup
post_mount_commands = [
"${path.root}/install_host.sh {{.MountPath}}",
]
}
build {
sources = [ "amazon-chroot.pc" ]
post-processor "manifest" {
output = "manifest.json"
}
}
My goal is to add another tag Codename
in the tags
section of the AMI. But I don’t know if it is possible to retrieve information gathered inside the build command and use it outside.
Does anybody have an idea if this is possible at all?