Picking up on Diogo’s last post on how to obliterate all resources on your AWS Account, I thought it could also be useful to, instead, list all you have running.
Since I’m long overdue on a Go post, I’m going to share a one file app that uses the Go AWS SDK for to crawl each region for all taggable resources and pretty printing it on stdout, organised by Service type (e.g. EC2, ECS, ELB, etc.), Product Type (e.g. Instance, NAT, subnet, cluster, etc.).
The AWS SDK allows to retrieve all ARNs for taggable resources, so that’s all the info I’ll use for our little app.
Note: If you prefer jumping to full code code, please scroll until the end and read the running instructions before.
The objective
The main goal is to get structured information from the ARNs retrieved, so the first thing is to create a type that serves as a blue print for what I’m trying to achieve. Because I want to keep it simple, let’s call this type SingleResource
.
Also, since we are taking care of the basics, we can also define the TraceableRegions that we want the app to crawl through.
Finally, to focus the objective, let’s also create a function that accepts a slice of []*SingleResource
and will convert will print it out as a table to stdout:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TraceableRegions is a list of AWS regions we want to crawl | |
var TraceableRegions = […]string{"us-east-1", "us-east-2", "us-west-1", "us-west-2", "ca-central-1", "eu-central-1", "eu-west-1", "eu-west-2", "eu-west-3", "eu-north-1", "ap-northeast-1", "ap-northeast-2", "ap-northeast-3", "ap-southeast-1", "ap-southeast-2", "ap-south-1", "sa-east-1"} | |
// SingleResource defines how we want to describe each AWS resource | |
type SingleResource struct { | |
Region *string | |
Service *string | |
Product *string | |
Details *string | |
ID *string | |
ARN *string | |
} | |
// PrettyPrintResources makes use of a nice golang library to show | |
// tables on stdout. Check it out: github.com/olekukonko/tablewriter | |
func PrettyPrintResources(resources []*SingleResource) { | |
var data [][]string | |
for _, r := range resources { | |
row := []string{ | |
DerefNilPointerStrings(r.Region), | |
DerefNilPointerStrings(r.Service), | |
DerefNilPointerStrings(r.Product), | |
DerefNilPointerStrings(r.ID), | |
} | |
data = append(data, row) | |
} | |
table := tablewriter.NewWriter(os.Stdout) | |
table.SetHeader([]string{"Region", "Service", "Product", "ID"}) | |
table.SetBorder(true) // Set Border to false | |
table.AppendBulk(data) | |
table.Render() | |
} |
https://gist.github.com/joaoferrao/610482a2394b17546e12d1c172f5198e.js