What is Packer:
Packer is a freeware tool from Hashicorp which helps in creating and maintaining golden images. Having golden images ready for your application helps to reduce the time needed to spin up the new instance. This can be for a web application or a custom built application. HashiCorp Packer is easy to use and automates the creation of any type of machine image. It embraces modern configuration management by encouraging you to use automated scripts to install and configure the software within your Packer-made images.
This takes the ami of your choice, creates a ec2 instance out of it installs the software of your choice via a shell/ batch/powershell script. Once the tools are installed or updated, the packer tool shut down the server creates a new image as a "new-golden-image". The packer tool neatly then terminates the ec2 instance it had created and ends up with just and image and nothing else.
Like other tools from Hashi Corp, Packer is a Cross platform tool and can be used in a variety of public and private clouds. Whatever a packer tool does can also be done by a native AWS CLI tools, but this does the work in a much simple and easy way. Also the packer tool does the termination of the EC2 after the image creation/update hence reducing any need of manual clean-up as well as avoid cost escalations due to improper cleanup.
Packer is a small executable provided by hashicorp which can be downloaded for free for multiple different websites. Download the package from the website and keep it in a appropriate place. Since this is again a IaaC its advisable that the packer codes are stored in a GIT or the standard SVN repository.
Checkout the git repo, then populate the <keys.json> file with your Access Key ID and Secret Key ID (from your IAM user).
You may also want to run `git update-index --skip-worktree keys.json` so that your changes are not inadvertently pushed upstream.
Packer command format
Packer build -var-file=<keys.json> <instance-details.json>
packer build -var-file=mykeys.json us-west-2-Ubuntu14.04_SunJRE8.json
The json file mentioned above "instance-details.json" is the actual crux of the tool. This contains all the details that are needed to create the instance, and also contains the name and description of the new ami.
The default ami will be converted to a golden image by running a script as a user data while creating the instance. The information in the user data is basically a shell/ batch/PowerShell script which will help to perform the required installation in the new server. The below parameter already mentioned in the given json does the trick. Although the script can also be inside the same json under provisioners its better to have it in a separate script as it helps in better versioning and code management.
Here is a example shell script that installs nodejs in the machine.
Disclaimer: The information given here is based on the writers knowledge and understanding. Users are recommended to check the respective organizations for authenticity, correctness and latest update. We will not entertain any legal actions for any loss due to usage of the blogs posted here.