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.
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/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.
Job1 – AMI configuration is done. Lets move to Job2 – UpdateCF.. Jenkins – AWS – Ansible – Rolling Update of AMI – Part 2