Managing Heroku apps and add-ons using Terraform

Heroku is a popular Platform-as-a-Service (PaaS), where you have absolutely no control over the infrastructure. But even for such platforms, Terraform can automate and manage things for you, so Heroku can do the rest. We'll create an app (a simple GitHub Hubot: http://hubot.github.com/), but feel free to use your own. On top of this app, we'll automatically plug a Heroku add-on (redis) and deploy everything.

Getting ready

To step through this recipe, you will need the following:

  • A working Terraform installation
  • A Heroku account (https://www.heroku.com/)
  • An optional Slack Token
  • An Internet connection

How to do it…

First things first: we need to define the Heroku provider. It consists of an e-mail address and an API key. Let's create generic variables for that in variables.tf:

variable "heroku_email" {
  default     = "[email protected]"
  description = "Heroku account email"
}

variable "heroku_api_key" {
  default     = "12345"
  description = "Heroku account API key"
}

Don't forget to override them in terraform.tfvars:

heroku_email = "[email protected]"
heroku_api_key = "52eef461-5e34-47d8-8191-ede7ef6cf9bg"

Now we can create the Heroku provider with the information we have:

provider "heroku" {
  email   = "${var.heroku_email}"
  api_key = "${var.heroku_api_key}"
}

Creating a Heroku application with Terraform

Instead of clicking through Heroku to create an application, let's do it right from Terraform. We want to run our app in Europe and we want Hubot to connect to Slack, so we need to provide a Slack token as well. Let's start by creating default values in variables.tf:

variable "heroku_region" {
  default = "us"
  description = "Heroku region"
}

variable "slack_token" {
  default = "xoxb-1234-5678-1234-5678"
  description = "Slack Token"
}

Now we can create our first Heroku app with its variables using the heroku_app resource, in heroku.tf:

resource "heroku_app" "hubot" {
  name   = "iac-book-hubot"
  region = "${var.heroku_region}"

  config_vars {
    HUBOT_SLACK_TOKEN = "${var.slack_token}"
  }
}

That's it! As simple as it seems.

Add some output in outputs.tf so we have better information about our app, like the Heroku app URL and environment variables:

output "heroku URL" {
  value = "${heroku_app.hubot.web_url}"
}

output "heroku_vars" {
  value = "${heroku_app.hubot.all_config_vars}"
}

output "heroku Git URL" {
  value = "${heroku_app.hubot.git_url}"
}

Adding Heroku add-ons using Terraform

Some add-ons need Redis to store data. Instead of going through the web application and enabling add-ons, let's instead use the heroku_addon resource. It takes a reference to the app to link the add-on to, and a plan (hobby-dev is free, so let's use that):

resource "heroku_addon" "redis" {
  app  = "${heroku_app.hubot.name}"
  plan = "heroku-redis:hobby-dev"
}

Using Heroku with Terraform

It's out of the scope of this book to show Heroku usage, but let's apply this terraform code:

$ terraform apply
[...]
Outputs:

heroku Git URL = https://git.heroku.com/iac-book-hubot.git
heroku URL = https://iac-book-hubot.herokuapp.com/
heroku_vars = {
  HUBOT_SLACK_TOKEN = xoxb-1234-5678-91011-00e4dd
}

If you don't have an application ready to ship on Heroku, let's try to deploy GitHub's chat robot Hubot. It's an easy application ready to use on Heroku. Quickly reading through the Hubot documentation, let's install the Hubot generator:

$ npm install -g yo generator-hubot

Create a new hubot:

$ mkdir src; cd src
$ yo hubot

Answer the questions and when you're done, using the usual heroku command, add the Heroku git remote for our Heroku app:

$ heroku git:remote --app iac-book-hubot

Now you can git push heroku and see your application being deployed, all using Terraform.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.252.37