Skip to content
← Back to all posts

Useful bash scripts: awslookup

01 March 2019

Written by 


A photograph of Nadeem Shabir

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.

#!/bin/bash

# uses aws cli to lookup instances based on a filter on the Name tag
# $1 is the profile to use
# $2 is the filter to use
# $3 is the output format defaults to 'table'
# $4 is optional, the value doesn't matter but if passed in will result
#    in this function printing out the raw command its about to run
#    for debugging purposes
awslookup() {
  cmd="aws --profile $1 ec2 describe-instances --filters \"Name=tag:Name,Values=$2\" --query 'Reservations[].Instances[].[InstanceId,PublicDnsName,PrivateIpAddress,State.Name,InstanceType,join(\`,\`,Tags[?Key==\`Name\`].Value)]' --output ${3:-table}"
  if [ $# -eq 4 ]
  then
    echo "Running $cmd"
  fi
  eval $cmd
}

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.