jaetech.org

A blog about doing things with computers


Blog | Archive | Twitter | Github | Rss

Terraform outputs in Ruby Code with ruby-tfoutputs

19 Oct 2016 | ruby, terraform, outputs, aws

This post details a way to get your terraform outputs into your Ruby code. I wrote a little ruby gem to handle it. Why might you do this? Well good question, sometimes people like to write some scripting that uses things from terraform outputs. Of course you can just make a command line call to terraform output which is pretty simple. After writing this gem I was wondering whether or not I should have bothered:

I probably shouldn't have bothered!

wtf is it

So basically you’ve got your terraform outputs. You can grab them with:

terraform output outputname

However sometimes people want to do other things with them like generate ERB templates. Instead of wrapping the terraform command line it’s possible to use a rubygem I created which should pull everything together for you. Better still, you can use many different terraform states so you might have 3 stored as file and 2 in Amazon s3. You can access them all through one source.

For help setting it up please see the github repo README.md

It’s still at an early stage so there may still be one or two bugs. If I’m honest it was a bit of a hack job and I’m not very proud of the quality of the code, but it seems to work!

Backends

Currently there are only two sources where you can retrieve states from: S3 or file. An example using both of these is below:

config = [{:backend => 's3',:options => {:bucket_name => 'my-bucket-name-goes-here',
           :bucket_region => 'eu-west-1', :bucket_key => 'terraform.tfstate' }
         },
          {:backend => 'file', :options => { :file_path => '/path/to/state/file/terraform.tfstate' } }
        ]
state_reader = TfOutputs.configure(config)
puts(state_reader.my_output_name)

However, it is designed to be extensible in terms of backends. So if it’d be useful to have other things like etcd, consul and others feel free to contribute or raise an issue.

Is it going to be useful?

I reckon most people will just wrap the terraform commandline, but if doing so causes a lot of spaghetti code and long shell scripts maybe this can help.


Older · View Archive (11)

Get up and running with Puppetlabs AWS

There’s an increasing number of ways to provision AWS infrastructure. I’ve already mentioned terraform which is my current frontrunner. There’s also also a couple of other options like the ansible cloud modules, Cloud Formation or writing your own custom stuff with an AWS sdk. In this post I’ll be looking at something I came across fairly recently: Puppetlabs-aws.

Newer

AWS Account structure - An Opinionated Post

I tend to work on a lot greenfield projects where we create AWS stuff from scratch. When I roll down somewhere the very first thing I need to think about is account structure. There are pros and cons to various account structures. There’s no ‘right’ way to do this but I’m going to cover why I generally prefer multiple accounts.