HomeAbout UsNews, company reports and blogBlogTechbash-my-aws: CloudFormation for Infrastructure Jockeys

bash-my-aws: CloudFormation for Infrastructure Jockeys

Article by Mike Bailey
Published on Read in 3 mins

dead air (noun): a period of silence especially during a radio broadcast

bash-my-aws (foss): set of bash functions that reduce dead air when managing Amazon AWS resources with CloudFormation.

DJs avoid dead air like the plague. We should too.

At the command line, dead air is the time between intention and outcome. It’s the time spent constructing commands and waiting for them to execute. It makes demonstrations tedious. It’s unacceptable for oft performed tasks.

Having the right tools for the job and knowing them well makes interactive use a breeze. bash-my-aws provides simple, powerful tools for managing Amazon AWS resources with CloudFormation.

A Simple Example – cf_list

cf_list returns a list of running CloudFormation stacks. It runs in under 2 seconds and is an easy to remember command.

$ time cf_list
example
example-mike
another-example
vpc-test
iam-roles
...100 more items
real 0m1.708s
user 0m0.224s
sys 0m0.071s

The AWS CLI provides a couple of simple commands to list stacks. The problems are that running “aws cloudformation list-stacks” takes me ~20 seconds to run and it gives more output than I need.

The AWS CLI lets us solve each of these issues through arguments and cf_list removes
the need to memorise and retype this every time it’s required.

$ type cf_list
cf_list is a function
cf_list ()
{
aws cloudformation list-stacks --stack-status CREATE_COMPLETE CREATE_FAILED CREATE_IN_PROGRESS DELETE_FAILED DELETE_IN_PROGRESS ROLLBACK_COMPLETE ROLLBACK_FAILED ROLLBACK_IN_PROGRESS UPDATE_COMPLETE UPDATE_COMPLETE_CLEANUP_IN_PROGRESS UPDATE_IN_PROGRESS UPDATE_ROLLBACK_COMPLETE UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS UPDATE_ROLLBACK_FAILED UPDATE_ROLLBACK_IN_PROGRESS --query "StackSummaries[].StackName" --output text | tr 't' 'n' | sort | grep "$1"
}

More Usage Examples

cf_create gives you tab completion for file names (missing from AWS CLI).

$ cf_create
Usage: cf_create stack [template-file] [params-file]

$ cf_create example      # creates stack called example using example.json

It’s also one of the functions that allows you to omit the template name if a file exists in the current directory that matches the stack name with ‘.json’ appended.

It’s even smart enough to detect that you’ve added ‘-blah’ to the stack name.

$ cf_create example-test # creates stack called example-test using example.json

cf_list returns a list of stacks matching (optional) pattern.

This is basically ‘ls’ with the ability to filter by a search string:

$ cf_list example # call without filter argument to return all stacks
example-app
example-app-test
example-app-dev

cf_diff compares a stack with a template.

This removes any doubt about the effect you’re about to have on a stack.

$ cf_diff
Usage: cf_diff stack [template-file]

$ cf_diff example
--- /dev/fd/63  2014-12-24 15:12:33.000000000 +1100
+++ /dev/fd/62  2014-12-24 15:12:33.000000000 +1100
@@ -113,7 +113,7 @@
         "t2.micro",
         "t2.small"
       ],
-      "Default": "m3.large",
+      "Default": "m3.small",
       "Description": "The type of the EC2 instance you want",
       "Type": "String"
     },

cf_update updates a stack:

$ cf_update
Usage: cf_update stack [template-file] [params-file]

$ cf_update example # creates stack called example using example.json 
...

cf_delete deletes a stack:

$ cf_delete
Usage: cf_delete stack

$ cf_delete example # deletes stack called example
...

cf_tail follows stack events:

The create/update tasks call this one but it can also be called directly. It watches events for a stack until it sees them complete or fail.

$ cf_tail who-is-my-am
----------------------------------------------------------------------
|                         DescribeStackEvents                        |
+--------------+----------------------+------------------------------+
|   Resource   |       Status         |            Type              |
+--------------+----------------------+------------------------------+
|  who-is-my-am|  CREATE_IN_PROGRESS  |  AWS::CloudFormation::Stack  |
|  S3Bucket    |  CREATE_COMPLETE     |  AWS::S3::Bucket             |
|  BucketPolicy|  CREATE_COMPLETE     |  AWS::S3::BucketPolicy       |
|  DNS         |  CREATE_COMPLETE     |  AWS::Route53::RecordSet     |
|  who-is-my-am|  CREATE_COMPLETE     |  AWS::CloudFormation::Stack  |
+--------------+----------------------+------------------------------+

Read more in the README

https://github.com/realestate-com-au/bash-my-aws

More from the blog

From Teaching to Software Development – A Year On

Category: Career stories
Published on

A vision of the future is a bet we are prepared to make

Category: Tech
Published on

One year at REA: Lessons, Growth, and Gratitude

Category: Life at REA
Published on

Introducing Argonaut – Part Three.

Category: Tech
Published on

Introducing Argonaut – Part Two.

Category: Tech
Published on

Introducing Argonaut – Part One.

Category: Tech
Published on

View all articles