Terraform Module Example

Terraform Module Example

Sergey Kuzmich bio photo By Sergey Kuzmich Comment

In addition to the previous article I would also like to write about Terraform Modules as a way to boilerplate common deployment tasks.

Let’s make a module to quickly deploy WordPress website with DigitalOcean provider.

Getting Started

Commonly Terraform module contains the next items: input variables, output variables and main service definition. There are three files from previous article variables.tf, credentials.tf and service.tf.

Create a directory for your future Terraform module wordpress/ and let’s start with files of service definition. Rename service.tf to main.tf.

* since droplet name is required argument, use "${coalesce(var.droplet_name, var.domain)}". It returns the first non-empty value from provided arguments, so you are able to give custom droplet name, otherwise it will be the same as a domain name.

Next create provider.tf with provider definition:

* we skip token argument and will provide it via environment variable

At this moment there are complete service and provider definitions. But no module outputs and no input variables are defined, so do it.

DigitalOcean WordPress specific setup

DigitalOcean’s WordPress One-Click application requires few manual steps to make website available for the public, such as providing ServerName for apache configuration.

Usually you need to login with SSH to droplet and then it runs /opt/digitalocean/wp_setup.sh script to make required setup with interactive prompt, but we’ll perform the setup with Terraform.

Let’s take only things we need and put it into user_data.sh file to apply it as user_data on droplet creation.

But there is no domain we want to set for the droplet. Also we can’t easily pass Terraform variable into this script. Terraform has a solution for it called template_file.

Rename user_data.sh to user_data.tpl and replace $dom bash variable with Terraform’s ${domain} variable on line 7.

Now, create one more file to describe this template user_data.tf.

The latest thing is to use created template_file as a droplet’s user_data.

Distribution

I prefer use GitHub to store and distribute modules.

You need to create GitHub repository and just put all files into it.

Check out the repo with this configuration.

There are few more ways to distribute modules. You can check it out on HashiCorp Docs.

Usage

Create directory for your project and create service.tf file inside.

That’s it! You just need to provide valid credentials to perform this action.

Credentials

I use the next way to provide credentials for terraform:

  1. the file with actual credentials as a environment varialbes (somewhere in user’s home directory): export DIGITALOCEAN_TOKEN=8d50ac...059828b
  2. .env file in terraform configuration directory to wrap environment variables into Terraform way: export TF_VAR_token=$DIGITALOCEAN_TOKEN

DigitalOcean provider requires token arugment as a access_key, so I wrap $DIGITALOCEAN_TOKEN into $TF_VAR_token=$DIGITALOCEAN_TOKEN.

Now, to apply any changes I just source ~/do_credentials file (the first one) and run Terraform commands.

References

comments powered by Disqus