Ersatz AWS Cost Allocation

I’m evaluating a tool to help keep tabs on our AWS spend. One challenge is that they don’t yet support AWS Cost Allocation tags. I find these to be a godsend for working out who created AWS resources. So I agreed to whip up a quick lambda script to get the cost allocation tag and expose as a plain user tag.

Cost allocation tags are written to a report in an S3 bucket if you enable the feature. Reading the report for the current month, pulling out the aws:createdBy tag and writing an ersatz:createdBy tag for each EC2 instance is easy.

Naturally my estimation of an afternoon to write and deploy this was wrong: it didn’t take too long to write, but deployment was time-consuming:

  • Pandas didn’t work out. Getting Python code with C deps onto Lambda isn’t straightforward. I switched to using the CSV library.

  • 5 minutes isn’t a lot of time when you’re parsing logs in a Lambda function. I had to make some changes to ensure that time wasn’t wasted reading entries that didn’t matter.

  • I also spun my wheels trying to use some tools to upload the function and it’s depencies. Ended up using a Makefile to create the final zip archive that gets uploaded to Lambda.

It’s running every hour in our account. There’s a lag between spinning up an instance and seeing the tags, which we can’t do much about.

I’ve put it in a gist.

-- DevOps New Zealand