I’ve worked through the Amazon AWS CLI commands to create and then to deploy an ECS cluster using ec2 containers and an Application Load Balancer to deploy a docker nodejs app.
In bash. Because the straightest line between command line and automation with no wasted motion is bash. Once.
But if you want a supportable script that can be extended, read in six months and understood, this can’t really stay bash. The only people who write bash with respect are sysadmins. System Ops. But Devops requires developers to be able to read, extend and support scripting at times, and the nature of bash makes reusable modular code require a lot of hoops to emulate. Better to move to python.
AWS boto3 has, once you start crafting the commands to use it, complete access to anything available directly through the command line. The first boto3 function I needed looked for an AMI with a regex name string and a tag Status set to “Active”
def find_ecs_ami(region): """ AWSCLI: IMG_ID=`aws ec2 describe-images --filters "Name=name,Values=catapult-devops-ecs-container-*" \ "Name=tag:Status,Values=Active" --region us-east-1 | jq '.Images[].ImageId' \ | sed 's/\"//g' | tr -d '\040\011\012\015'` name is atapult-devops-ecs-container-* tag is Status:Active That's not going to change... """ ec2 = boto3.resource('ec2', region_name=region) image = list(ec2.images.filter(Filters=[{'Name':'name', 'Values':['catapult-devops-ecs-container-*']},{'Name':'tag:Status', 'Values':['Active']}]).all()) ami = str(image[0]) # cast first element of returned list (only element with filter) as string ami_arr = ami.split('=') # split the string on '=' ami_id = ami_arr[1] # take the ami id with quote and ')' ami_id = ami_id.replace("')", '') ami_id = ami_id.replace("'", '') return(ami_id)
This took a bit to work through – I had to discover how to read the boto3 docs, which are complete but not immediately intuitive. And find enough example to decipher the docs long enough to get it to work. Eventually I got
ami-09019dc02aa3167ba
The function after that would be create_cluster(), then create_instances(), then create_task_def(), create_elb(), create_listener(), create_target_group(). I’ve got create_instance() and create_cluster(). Once you get the boto3 docs, the implementation is very reusable, way more so than bash…
— doug
P.S.: I’ve used the Umbrella Corp logo tongue-in-cheek for devops projects because, devops really is the stuff of life itself…