Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / containers / docker

Getting Started with Terraform

5.00/5 (1 vote)
4 Feb 2020CPOL3 min read 2.8K  
Terraform allows you to setup Infrastructure as expressed by code (Infrastructure As Code IaC). This post helps you to get 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 We're 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.

Install Terraform

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

Shell
curl -O https://releases.hashicorp.com/terraform/0.12.10/terraform_0.12.10_linux_amd64.zip

Unzip the Package into our /usr/local/bin to Install It Locally

Shell
sudo unzip terraform_0.11.10_linux_amd64.zip -d /usr/local/bin

Confirm It’s Working

Now let's run the following command to check that it’s working and available.

Shell
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 change quite drastically. So just be aware of this.

Install Docker

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

Install the prerequisites to add a new repository over https:

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

Import the GPG key from docker:

Shell
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -

Add the docker apt repository to our repository list:

Shell
sudo add-apt-repository "deb [arch=amd64] 
https://download.docker.com/linux/debian $(lsb_release -cs) stable"

Update apt and install Docker CE:

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

Creating Our First Terraform Script

Pull a Docker Image From the Terraform Docker Provider

Create Our New Script

Shell
mkdir terraform-gettingstarted
touch main.tf

Create the Script Content

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

Shell
# 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.

Shell
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

Shell
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:

Shell
terraform apply

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

See What Was Applied

Shell
terraform show

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

Interpolation Syntax

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

The documentation on terraform.io explains this in depth.

Create a Docker Container of the Image

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

Open main.tf and Edit

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

Shell
# 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 let's view the plan of the changes by running the below:

Shell
terraform plan

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

Now let's apply our changes:

Shell
terraform apply

Now if you run:

Shell
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.

Shell
terraform destroy

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)