Packer
Updated at 2017-03-19 16:43
Packer is tool for creating machine images of various formats. Packer allows turning image creation into an automated process.
Creates image >> Chef/Puppet/Ansible updates and installs software >> Save image
For example:
- AWS EC2 uses AMI format
- Azure uses VHD format
- OpenStack have their own format
- DigitalOcean has snapshots
- Docker has Docker image format
- VMware uses VMDK/VMX format
- VirtualBox uses OFV format
brew install packer
packer --version
Packer uses JSON template files. Make sure you have builder role defined in AWS if you plan on running this template.
- Remember to set the region and AMI you want which changes what AMI you should use.
builders
defines what kind of machine is booted and turned into an image- Here we define amazon-ebs builder
- Credentials for the builder come from instance profile
{
"builders": [{
"type": "amazon-ebs",
"region": "eu-west-1",
"source_ami": "ami-d8f4deab",
"iam_instance_profile": "PackerBuildInstanceRole",
"instance_type": "t2.micro",
"ssh_username": "ubuntu",
"ami_name": "packer-example-{{timestamp}}"
}]
}
# Check that the template is valid after modifications
packer validate example.json
# Short description what the template does.
packer inspect example.json
# Build the image(s) in the cloud.
packer build example.json
Template files might have the following root level items:
variables
variables that you define when runningpacker build
builders
each builder will create exactly one machine image build.description
optional description what the template does._anything
any root level item prefixed with_
means comment in templatesmin_packer_version
allows limiting to specific packer version.provisioners
what is done after creating the image but before saving it like executing scripts locally, scripts on the remote machine, uploading files and executing Ansible playbooks.post-processors
what actions are done after building the images.
You could copy the AMI to multiple AWS regions. That takes at least a couple minutes so is not included in the previous example.
"ami_regions": ["eu-west-1", "eu-west-2"],
You could add digitalocean
builder to also create DigitalOcean snapshot in parallel with the AWS image.
"variables": {
"do_api_token": ""
}
...
{
"type": "digitalocean",
"api_token": "{{user `do_api_token`}}",
"image": "ubuntu-14-04-x64",
"region": "ams1",
"size": "512mb",
"ssh_username": "root"
}
If you want to remove the example AMI, notice that AWS images have two components. AMI definition that you need to deregister and the snapshot of the EBS that you need to delete. Both are removed in EC2 console.