← Back to all posts

Useful bash scripts: awslookup

01 March 2019

Written by 


Nadeem Shabir
Automation Lead


But we are hackers and hackers have black terminals with green font colors ~ John Nunemaker

This post is the first in a series on useful bash aliases and shell customisations that developers here at Talis use for their own personal productivity. In this post I introduce an alias I wrote called awslookup that allows me retrieve information about ec2 instances across multiple aws accounts.


Background

At Talis, we run most of our infrastructure on AWS. This is spread over multiple accounts, which exist to separate our production infrastructure from development/staging infrastructure. When we release new versions of our software, we bring in new instances, and retire old instances as the new ones accept traffic. We also use auto scaling to bring in more instances during peek periods. This means that if we ever need to jump onto an instance we first have to look up its details. In the past I’ve used tools like ElasticFox, a Firefox plugin, which listed your ec2 instances as well as provided lots of other useful functionality:

ElasticFox

Sadly, ElasticFox has been defunct for some time, and though it has inspired many other, similar, graphical user interfaces none of them really appealed to me.

As I spend most of my time looking at a terminal window, what I really wanted was a way to query any of our AWS accounts for information about our ec2 infrastructure from within my terminal window, and in such as way that I could take the output and combine it with other commands to do interesting things. I wanted something lightweight, that did one thing, and did it well.

awslookup

The AWS command line interface is very good for this but querying using it can be quite difficult. I created a small bash function/alias, called awslookup as a wrapper around an AWS CLI query that lets me list information about instances based, specifically, on their tag:Name.

For example, running the command awslookup staging '*web*' lists all the instances in our staging/development account that have web anywhere in their name tag:

Awslookup Table Output

Where this really becomes useful is that I can format the output as plain, tab delimited text, and when I do so I can pipe the output from awslookup to any number of other tools. For example here’s the plain text output:

Awslookup Text Output

and now I can extract just the hostname (using awk) for each instance and pipe this to other tools:

Awslookup Pipe Output

Next up …

In the next post in this series, I’ll show you how I used the output from awslookup to solve a completely different problem: Automatically changing my terminal theme when I connect to a remote machine in any of our aws accounts.