Build custom AMI on AWS
THE REPOSITORY: https://github.com/mrtrkmnhub/ubuntu-packer
In this blog post, provisioning and customizing images using packer will be shown with a template repository.
If you are asking or wondering what is Packer, the official definition is :
Packer is a free and open source tool for creating golden images for multiple platforms from a single source configuration. (From Official Website).
This post includes provisioning of ubuntu image on AWS and local.
In an ideal repository of Packer template, it would be nice to have a skeleton where it includes
scripts folders along packer configuration file with a readme. Overall, the structure of folder might look like this :
│ └── preseed.cfg
│ ├── cleanup.sh # cleans up /tmp
│ ├── install_tools.sh # installs custom tools
│ └── setup.sh # setting up config in system wise
├── ubuntu-20.04.json # packer config for ubuntu 20.04
In this setup,
http/preseed.cfg defines answers to the questions which may be asked during installation of Ubuntu operating system. More information regarding to preseed.cfg file can be checked from its wiki
scriptsfolder composed of bash scripts, chef, ansible or any other installer configuration files or scripts which will install customized tools and define settings of ubuntu image.
uploadsfolder includes all files, deb packages, or any other files which will be copied to image which will be inside customized image.
Anatomy of Packer Configuration File
Any packer file composed of three main components which are ;
Define the desired platform and platform configurations, including API Key information and desired source images. Example snippet is given from the Packer file:
In the builders config, we are defining some set of keys in JSON file, which are very obvious from its name, we are considering to build image locally. All the keys are important in given builders config however most important and might need to update time to time is
iso_urls which are the places where packer download iamges and customize it according to your scripts. Another crucial key is to have
true which means that there will be no GUI running when packer command is executed to run the Packer JSON file.
Defines how to configure the image most likely by your using existing configuration management tools like Ansible, Chef, Puppet or pure bash scripts.
In our example, bash scripts will be provided to install tools and update configuration of ubuntu image to make it customized. Provisioner section of a Packer JSON file can be seen as below:
Here we are defining existing bash scripts in order to execute in the process of customizing Ubuntu image. The steps under provisioners are pretty clear.
Related to the builder, runs after the image is built, it is generally used to generate or apply artifacts. In this example, it is not required however more information can be found here: post processors
How packer works on the machine image during the creation. By default it is over SSH communication and it does not need to be defined explicitly. More information can be found here: communicator
Over all packer file can be seen as follow:
How to run locally
This file can be run from the place where ubuntu-20.04.json file is located.
$ packer build ubuntu-20.04.json
It will start to build custom image by installing tools which are defined under scripts and configure username and password according to preseed.cfg and setup.sh files.
It is more practical and preferrable to use if you already have an cloud option to consider. This packer configuration will create custom image directly on cloud and save it to AMIs to your AWS account.
The anatomy of packer files is similar, only section which needs to be changed compared to local one, is builders section. It is defining all required AWS variables and AMIs to customize.
As an cloud example AWS will be used to create custom image.
Builders on Cloud
In this configuration, all keys are important to consider, however there are some which are crucial and required to run it. More information about the keys can be found here: Amazon AMI Builder
We would like to create a custom Ubuntu-20.04 image on cloud and save it as AMI to run it later, we are searching its pattern from available AMIs on AWS Management Console or it can be found through out this website : https://cloud-images.ubuntu.com/locator/ec2/
Once you have declared which AMI to customize, it needs to be located under
source_ami_filter with wildcards and owners. Setting
true means that when this Packer JSON file is executed it will fetch and customize last updated AMI.
Access Key, Secret Key are required and should not be exposed to public in any moment, if exposed, they need to be updated immediately. They will be used to communicate with AWS to fire up instances to create custom image according to given settings defined in builders and provisioners.
The values of keys are defined in variables and parsed from out of it.
In variables section, username, instance_type, aws_access_key, aws_secret_key variables should be set correctly to create the image on cloud. Other variables are optional and variables section can be populated more.
Customize settings on Cloud
On cloud builds, cloud configuration file should be used instead of
preseed.cfg to customize settings. The defaults.cfg file where it contains custom settings such as default username, password, changing visudo file and more. Example defaults.cfg can be as follow:
More information regarding to defaults.cfg file can be found here and customized more: https://cloudinit.readthedocs.io/en/latest/topics/examples.html
Once variables are set, it can be run in same way with the local one.
$ packer build aws_packer.json
Complete packer JSON file : aws_packer.json
As a summary, Packer is really cool tool to use to automate the process of creating custom images and it can be used for Dockers as well. For local example in this post, it will produce OVA file to import, on cloud it will generate custom AMI under your AWS account.
All scripts and config files can be found in this repository: https://github.com/mrtrkmnhub/ubuntu-packer
Originally published at https://mrturkmen.com on April 17, 2021.