IaC, DevOps, Manage EC2 AMI, Launch Template and ASG with Packer and AWS CLI

DoggyFootGuy
2 min readOct 14, 2020

I recently were needed to expand my role to more in DevOps as current new small startup organization started to feel the necessity of more dedicated devOps guy and that’s me, new journey to DevOps.

First thing i noticed is that seems until now we have been managed EC2 AMIs all by hands, so i decided to scriptify possibly everything to manage EC2 AMIs and Launch Templates together.

I started to list down simple requirements, processes for that and find existing solutions, best practices but simple from internet and found one article, https://blog.skbali.com/2020/03/update-image-using-packer/, which still i should consider further to use in production.

Basic requirements,

  1. run EC2 instance with AMI image
  2. mutate the state (run commands) of instance created from step 1
  3. create new AMI image with instance from step 2
  4. modify launch template

also occasionally we may want to replace all running instances in situations like

  • when instances working with ASG

further consideration and steps would be

  • get AMI image running in ASG’s Launch Template for the base image
  • fix the launch template version configured in ASG with current version if it is default or latest to prevent from not intended scaling actions before we actually want to replace.

which should be done between step 3 and 4, and finally we can replace running instances with old launch template with new on demand.

  1. scale out instances by doubling Desired capacity and also Maximum capacity if needed.
  2. set scale in protection on instances with new launch template to prevent from not being terminated by any termination policies.
  3. scale in instances by modifying back Desired capacity and Maximum capacity.
  4. remove scale in protection on new instances.

Here are initial scripts, it requires JQ 1.6 (or above maybe), Packer, AWS CLI.

--

--