Following the 12 Factor App definitions, we'll put together an AMI that has the software installed on the image, then combine it with configuration for the destination environment for a release AMI.
Think of "build" as installation and release as configuration. See build, release, run on the 12 Factor App site.
The following command will launch an AMI, install the sample web service with a blank configuration file:
packer build packer/packer-build.json
Once we have an AMI with our application installed, we need to apply the configuration for it. In this simple example, we're allowing the port the web service attaches to to be configured. Ansible is used to supply the config values. We also set the service to start when the instance is spun up.
Supply the AMI ID from the previous step in this command:
packer build -var 'source_ami=ami-ca056ea2' packer/packer-release.json
Now the AMI has the application installed, configuration values are in place and the service will start when the AMI is started. This makes quick startup for easy autoscaling and eliminates the need to configure new instances as they are spun up.
Use Packer's -machine-readable
flag to get an easily parsable output. Use this to supply AMI IDs to build steps.
A solid workflow:
- Compile source to executable and run unit tests.
- Integration tests (optional but recommended).
- Package code into a deployable artifact. May be a standalone executable or many files rolled into an RPM.
- 12 Factor 'build' step: using Packer, install the executable or RPM onto an instance, save when done.
- 12 Factor 'release' step: using Packer and Ansible, apply desired configuration to the installed application. Set the application to run on startup then save the AMI.
- Deploy new AMI.