👋 I’m Tobie, developer at Flux. I joined Flux in August last year to help them tackle a huge problem — we’re still getting paper receipts, with no way to link exactly what we’ve bought with our bank transactions. We’re working on solving that.

Note: This post is intended for a technical audience! 💻

Terraform is a great tool that we’ve been using a lot at Flux, it lets you “write, plan and create infrastructure as code”, this means you can version control a vast majority of the code which relates to your infrastructure. Terraform has features like plan which let you see which resources are affected by the change you’ve made. Today I’m going to give you an example which can help you get up and running quickly with it.

Here I’ll go through a quick example of using Terraform to manage Datadog monitors, in my opinion it’s a good place to start as it enables you to understand and play with the technology in a useful way, without making a significant investment.

Installing Terraform

First of all, you’ll need to install Terraform, it’s usually pretty simple:

Mac OS X: brew install terraform

Windows: choco install terraform

Other platforms, see official Terraform documentation.

Terraform ships with enough providers to get you started by default — though you can add more as you need. Providers generally represent a third party API/service.

Now, you should create a folder to contain all the Terraform goodness, from now on I’ll assume you’ve created a folder called terraform .

Getting started

Configure initial provider

You can configure the provider by either passing the keys via an environment variable, the keys you’ll need can be found on the Datadog website and you’ll need to pass the following:

DATADOG_APP_KEY

DATADOG_API_KEY

Alternatively you can configure them like so (place this in terraform/datadog.tf )

Create your first monitor

Creating your first monitor with Terraform is rather simple, here I’ll assume you already have some metrics going into Datadog that we can report on, I’m using one of the metrics AWS provides. Start by placing this snippet into terraform/datadog.tf .

Once you’re happy we can move onto the next step, initialising Terraform, ensuring providers are there and so on.

Change directory, into the terraform folder: cd terraform

Run terraform init

Next for the most exciting part, run terraform plan . This will show you what will change - I won't show the output here as it's very similar to the output for terraform apply which is shown below.

If you’re happy to go ahead and create the monitor, run terraform apply - first you'll see an output of plan, type yes and then the changes will be applied.

This is where the power of Terraform starts to become clear, you will see what is going to change and in some cases this can protect you from unintended side effects.

Having invested quite a lot of time into Terraform at Flux, we’ve found it really helps keep things in sync, for example: hosting a static site — you can create a S3 bucket, CloudFront distribution and a DNS entry into Route53, all at the same time, referencing each other using the Terraform HCL syntax.

Now for the purposes of this post, we’ll change something on the monitor so you can start to see how useful Terraform is, in this case I’ve changed warning to 40 and alert to 50. After that running terraform plan you'll see something like the following:

If you’re happy with the changes, run terraform apply - and that's it! Time for a ☕️.

Further reading

This post isn’t intended to be a comprehensive guide to Terraform — it’s a tool with a significant amount of scope, that said if you’re intending to use it properly in production/with a team, the following are useful.

Contact me

If you have any questions, comments or feedback regarding this post, feel free to leave them below. If you’d like to see more posts like this, let us know!

If you’d prefer you can get in touch with me directly via Twitter, follow me on GitHub, or Keybase.