A collection of Packer builds for Ubuntu 20.04 (focal) which publish to the HCP Packer registry. Used to demonstrate image channels, ancestry, revocation, and Terraform integrations.
Packer v1.10.1 or higher.
To build all images, AWS and Azure credentials must be available using one of the following mechanisms:
- AWS: environment variables, credential file, or run from an EC2 instance with an instance profile
- Azure: CLI (
az login
), Azure AD interactive login, or run from an Azure VM with a managed identity
An HCP Packer organization, with a "Contributor" service principal key set via the HCP_CLIENT_ID
and HCP_CLIENT_SECRET
environment variables.
For Azure, an existing resource group where the builds will take place and images will be published.
Copy variables.pkrvars.hcl.example to variables.pkrvars.hcl and customize.
Run the build script:
./build.sh <build_name>
Where build_name
is one of the subfolders - base
(must be built first), db
, or web
.
Or if you're not on Linux/macOS, you can run Packer directly (ex: in a Windows PowerShell):
packer -var-file ./variables.pkrvars.hcl ./<base|db|web>
To build only the AWS or Azure images but not both, comment out the provider-specific references in each config file (data source, source block, and entry in the build->sources list).
Use the "Use as data source" code generator in the HCP Packer UI to generate a Terraform hcp_packer_artifact
data source block.
Example:
data "hcp_packer_artifact" "ubuntu20-nginx" {
bucket_name = "ubuntu20-nginx"
channel_name = "production"
platform = "aws"
region = "us-east-1"
}
# Then replace your existing references with
# data.hcp_packer_artifact.ubuntu20-nginx.external_identifier
To integrate with Terraform Cloud continuous validation, add a lifecycle postcondition block to your instance/VM resource:
AWS:
resource "aws_instance" "my_ec2" {
# ... resource config ...
lifecycle {
postcondition {
condition = self.ami == data.hcp_packer_artifact.ubuntu20-nginx.external_identifier
error_message = "A new source AMI is available in the HCP Packer channel."
}
}
}
Azure:
resource "azurerm_linux_virtual_machine" "my_vm" {
# ... resource config ...
lifecycle {
postcondition {
condition = self.source_image_id == data.hcp_packer_artifact.ubuntu20-nginx.external_identifier
error_message = "A new source image is available in the HCP Packer channel."
}
}
}