Provision
In the previous tutorial, you created your first image with Packer. However, the image you built essentially repackaged an existing AMI. The real utility of Packer comes from automated provisioning in order to install and configure software in the machines prior to turning them into images.
Historically, pre-baked images have been frowned upon because changing them was so tedious and slow. Because Packer is completely automated (including provisioning) images can be changed quickly and integrated with modern configuration management tools such as Chef or Puppet.
In this tutorial, you will complete your image by installing Redis on it. Although pre-installing Redis to your AMI is a small example, it should give you an idea of what Packer provisioners can do.
Prerequisites
This tutorial assumes that you are continuing from the previous tutorials. If not, follow the steps below before continuing.
Install Packer
Create a directory named
packer_tutorial
and paste the following configuration into a file namedaws-ubuntu.pkr.hcl
.Initialize the Packer template.
Once you have successfully initialized the template, you can continue with the rest of this tutorial.
Add provisioner to template
Using provisioners allows you to completely automate modifications to your image. You can use shell scripts, file uploads, and integrations with modern configuration management tools such as Chef or Puppet.
Find source.amazon-ebs.ubuntu
and note that uses the SSH communicator. By specifying the ssh_username
attribute, Packer is able to SSH into EC2 instance using a temporary keypair and security group to provision your instances.
To write your first provisioner, add the following block into your Packer template, inside the build block and underneath the sources
assignment.
This block defines a shell
provisioner which sets an environment variable named FOO
in the shell execution environment and runs the commands in the inline
attribute. This provisioner will wait 30 seconds before installing Redis, then create a file named example.txt
that contains FOO is hello world
.
Your build block should look like the following.
Build image
AMI names must be unique. So you must update your AMI name before building the image.
Tip
The below snippet is formatted as a diff to give you context about
which parts of your configuration you need to change. Replace the content
displayed in red with the content displayed in green, leaving out the leading
+
and -
signs.
Build the image with the provisioner.
In the output, you will find the Provisioning with shell script
that confirms that the Packer ran the provision step. Notice how Packer also outputs the first inline command (Installing Redis
).
Visit the AWS AMI page to verify that Packer successfully built your AMI.
Add more provisioners
The shell provisioner demonstrated above is extremely powerful and flexible. For complex provisioning, you can pass entire shell scripts rather than the inline
declarations shown above.
You can run as many provisioners as you'd like. Provisioners run in the order they are declared.
Replace your existing build block with the following. This adds a provisioner to the end of your build block that will print a message in the shell execution environment.
Build and verify updated image
Update your AMI name before building the image.
Then build the image again.
Notice how there are two Provisioning with shell-script
executions. The second provisioning step displays the expected message.
Visit the AWS AMI page to verify that Packer successfully built your AMI.
Next steps
In this tutorial, you used provisioners to preinstall Redis and create a file in your AMI. You can apply these same principles to provisioning and configuring any Packer images. Continue to the next tutorial to make your Packer template more robust with variables.
Refer to the following resources for additional details on the concepts covered in this tutorial:
- Read more about the Packer provisioners.
- Learn more about how to use Packer provisioner blocks.
- Learn how to use Terraform and Packer to provision an EC2 instance with your SSH keys by following the Provision Infrastructure with Packer tutorial.