Jenkins – AWS – Ansible – Rolling Update of AMI – Part 1

Create/Update AMI of Instances through CF –

But this is manual as I need to take a snapshot of EC2 Instance and update CF stack with new AMI. It is time to automate the rolling update of AMI using Jenkins and Ansible.

Jenkins install and configuration documented here –

Created two jobs – Job1 is AMI which is to take snapshot of EC2 Instance and Job2 is UpdateCF which updates the stack with new AMI.


Lets look at Job1 – AMI – which takes a single parameter – instance id


As there is no requirement of Git requirement – we can leave “Source Code Management” and “Build Triggers” – running this job is manual.

I have installed aws-cli on the Jenkins server so creating AMI using “aws ec2 create-image” command. As you can see below I am writing AMIId as AMI=$AMI_Id to temporary file which we can read from Job2 while updating CF stack.


Shell script is here:

rm -rf /tmp/
touch /tmp/

AMI_Id=`aws ec2 create-image --instance-id $InstanceId --name "BuildImage$BUILD_NUMBER" --no-reboot | grep 'ImageId' | cut -d '"' -f4`
echo AMI=$AMI_Id > /tmp/

Now in the post build section we should call Job2 – UpdateCF. We need to trigger Job2 only when Job1 is successfully built/stable and the temporary file is present.


Job1 – AMI configuration is done. Lets move to Job2 – UpdateCF….


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s