Getting started with Terraform.

Terraform allows you to setup Infrastructure as expressed by code, (Infrastructure As Code IaC). It’s platform independent, and supports a wide range of different things, from cloud providers like Azure, AWS, GCP, to databases to DNS, pretty much everything.

What were going to do:

  1. Install Terraform on Debian
  2. Install Docker
  3. Create a terraform script to download a docker image of jekyll
  4. Modify our terraform script to spin up the jekyll container.

Setting up your workstation

I’m using Debian 10 (buster) for my installation here but Terraform can literally be installed on anything since it is written in golang.

1. Install Terraform


Goto the following link on the terraform website Terraform Download page

And click either 32-bit or 64-bit for windows download.

This will download terraform.exe, a standalone executable, I like to copy this into c:\terrafrom\ you will want to add this to your path variable.

Add to PATH environment variable
  1. Click start and type Control Panel
  2. In the search box at the top right type “path”
  3. In the results you will see “Edit the system environment variables” Edit the system environment variables
  4. This will bring up the system properties System Properties
  5. Click Environment Properties button at the bottom
  6. The Environment Variables screen will be displayed Environment Variables
  7. Selct the Path Variable under System Variables at the bottom, and click Edit.
  8. Click New and add a new variable of “C:\terraform\” Edit environment variable
  9. Fire up Command Prompt or Powershell and type terraform, you should get the terraform output in any directory you are in. Terraform output


Download the most recent package for your operating system from the terraform download page

curl -O
Unzip the package into our /usr/local/bin to install it locally
sudo unzip -d /usr/local/bin

Confirm it’s working

Now lets run the following command to check that it’s working and available

terraform -version

You should receive the following version output Terraform v0.12.10

Terraform version

As you can see Terraform is still not at version 1, it’s still in beta and is in active development and each version can and has change quite drastically. So just be aware of this.

2. Install Docker


Goto the following link: Docker for windows

And install Docker for windows following the instructions.


If you already have docker installed locally you can skip these steps

Install the prerequisites to add a new repository over https

sudo apt-get update && apt-get install apt-transport-https ca-certificates curl software-properties-common

Import the GPG key from docker

curl -fsSL | sudo apt-key add -

Add the docker apt repository to our repository list

sudo add-apt-repository "deb [arch=amd64] $(lsb_release -cs) stable"

update apt and install Docker CE

sudo apt-get update
sudo apt-get install docker-ce

3. Creating our first Terraform script

Pull a docker image from the Terraform Docker provider

Create our new script

mkdir terraform-gettingstarted

Create the script content

Now open up the in your favourite text editor and add the following:

# Download the latest jekyll image
resource "docker_image" "image_id" {
  name = "jekyll:latest"

Initialise our Terraform directory

This command will initialise our directory by creating a .terraform directory. And download any providers that are necessary.

terraform init

This will download the terraform docker provider. The providers are stored in the .terraform directory. If you look in .terraform/plugins/linux_amd64/ you will see the terraform provider.

View the changes that will happen

terraform plan

By running terraform plan we will see all the changes that are going to be applied.

Run our new script

Once we have viewed the plan, and we are happy it isn’t doing something we didn’t expect we will run the following command:

terraform apply

This will download the docker image that we specified in the file.

See what was applied

terraform show

This will return the docker image id that we asked it to download

4. Interpolation syntax

Interpolational syntax is a way for you to reference variables and other objects from within your infrastructure.

The documentation on explains this in depth. (Link)[]

Create a docker container of the image

Let’s add to our file and actually create a container using the image above, referenced with interpolation syntax

Open and edit

Let’s go ahead and open the file that we created above, and add the following code below:

# Start the container
resource "docker_container" "container_id" {
  name = "blog"
  image = "${docker_image.image_id.latest}"
  ports {
    internal = "2368"
    external = "80"

You can see the $ sign, this is how we are referencing the image above.

Run our changes

Now lets view the plan of the changes by running the below:

terraform plan

You’ll see that it is referencing our id of our image

Now lets apply our changes

terraform apply

Now if you run:

docker ps -a

You will see our container is running with the ports open.

If you now browse to port 80 in a web browser you will see the jekyll blog setup.

Clear terraform infrastructure

To remove all the infrastructure we just created above we can use the destroy command.

terraform destroy

It will tell you what you are about to destroy, and require you to type yes.