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

Create/Update AMI of Instances through CF – https://linuxandevops.wordpress.com/2017/06/24/aws-update-ami-through-cf-template/

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 – https://linuxandevops.wordpress.com/2017/07/10/jenkins-install-and-plugins/

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.

Jenkins_Jobs.JPG

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

Jenkins_AMI1.JPG

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.

Jenkins_AMI2.JPG

Shell script is here:

rm -rf /tmp/play.properties
touch /tmp/play.properties

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/play.properties

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.

Jenkins_AMI3.JPG

Job1 – AMI configuration is done. Lets move to Job2 – UpdateCF.. Jenkins – AWS – Ansible – Rolling Update of AMI – Part 2

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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