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.
-
Create the VPC using the
vpc.yaml
CloudFormation template. Note the VPC and Subnet IDs and updatepacker-files/packer.json
with the values. While you are editing packer.json make sure that the AMI ID for thesource_ami
is 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 inpacker-files
into the checkout repository, then commit and push. -
Create the ECR repository using
ecr-repository.yaml
. -
Use the following commands to build the Docker container and push it to the repository, substitute the variables in block capitals as appropriate:
cd container
docker build --rm -t <USERNAME>/packer:latest .
aws ecr get-login --region <AWS REGION>
Run the
docker login
command that's output byaws 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-role.yaml
andcodepipeline-role.yaml
. -
Edit
codebuild-project.yaml
to reference the container you just created on line 15 - I should really make this automagic using!Sub
. -
Create the CodeBuild project using the
codebuild-project.yaml
file. -
Create the CodePipeline using the
codepipeline.yaml
file.
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.