Creating an AMI Bakery
I want to create my EC2 instances as Immutable Infrastructure and to speed up boot time by doing as little configuration at boot time as possible. The way to do this is to pre-build, or bake, your AMIs with your application already installed.
The demo in this repository shows how to use Packer in conjunction with AWS CodeCommit, AWS CodePipeline, and AWS CodeBuild, to automatically create a new Amazon Linux AMI with all of the current updates applied based on the contents of a Git repository.
Steps to Build the Demo
Create the VPC using the
vpc.yamlCloudFormation template. Note the VPC and Subnet IDs and update
packer-files/packer.jsonwith the values. While you are editing packer.json make sure that the AMI ID for the
source_amiis set for your environment (it's the Amazon Linux AMI in EU-West-1).
Create the Git repository using
codecommit.yaml, checkout the empty repository, copy the files in
packer-filesinto the checkout repository, then commit and push.
Create the ECR repository using
Use the following commands to build the Docker container and push it to the repository, substitute the variables in block capitals as appropriate:
docker build --rm -t <USERNAME>/packer:latest .
aws ecr get-login --region <AWS REGION>
docker logincommand that's output by
aws ecr ....
docker tag <USERNAME>/packer:latest <AWS ACCOUNT>.dkr.ecr.<AWS REGION>.amazonaws.com/packer:latest
docker push <AWS ACCOUNT>.dkr.ecr.<AWS REGION>.amazonaws.com/packer:latest
Create the roles for CodeBuild and CodePipeline using the files
codebuild-project.yamlto reference the container you just created on line 15 - I should really make this automagic using
Create the CodeBuild project using the
Create the CodePipeline using the
Once step 8 completes the CodePipeline should automatically detected the Git repository and start executing the pipeline. After 10 minutes of so you should be able to go to the AMI section of the EC2 page on the AWS console and see your new image.