Select Page

Building a Terraform Infrastructure

Karthik Reddy | | August 15, 2019

What is Terraform?

Terraform is an ‘infrastructure as code’ software developed by HashiCorp. Terraform lets users apply a high-level configuration language or code to define a datacentre/infrastructure. A configuration file is created and from that configuration file, we can create an execution plan to build the infrastructure in cloud solutions such as those offered by AWS, OpenStack, Microsoft Azure, or Oracle Cloud Infrastructure.

The configuration file describes to Terraform the components that are needed. The config file can run either a single application or an entire infrastructure because executing the configuration file builds the desired infrastructure in the required platform. An execution plan describing the desired state is then generated, which is subsequently implemented to incorporate all the components of the configuration file into the described infrastructure. When the configuration is changed, terraform identifies those changes and then creates an incremental execution plan. Only these changes can be applied, and the changed components can be built.

As a tool, terraform lets you build, change, and version your infrastructure efficiently and safely.

Key features:

Infrastructure as Your Code

A high-level configuration syntax (HCL Terraform syntax or JSON) describes your infrastructure, which also creates a blueprint of your intended datacentre/infrastructure. By doing so, the infrastructure can be shared, versioned, and re-used.

Generates Execution Plans

There is also a “planning” step that generates an execution plan. The execution plan shows the changes to the existing infrastructure, which allows both verification of the configuration file and any changes that may be necessary.

Resource Graph

Terraform also builds a graphical representation of the resources and simultaneously builds any non-dependent resources, which increases the operational efficiency of the Terraform build process as the required resources are analyzed and built by the Terraform engine.

Change Automation

As humans are prone to errors, these errors are minimized by reducing the number of manual tasks, making it easy to manage the complex changesets/configuration changes within your infrastructure. As the execution plan and the resource graph are reviewed, the changes can be tracked and versioned as they are applied.

How to Configure Terraform?

Installation:

Begin by finding and downloading the zip-archived Terraform packages. Once downloaded, unzip the package. The program performs as a single binary/program named terraform.

Verifying Installation:

On completion of the installation of the binary, we can verify the configuration on the terraform by running terraform command.

Output:

terraform

Usage: terraform [--version] [--help] command [args]

The available execution commands are listed with the most common, useful commands identified first, and followed by less common and/or more advanced commands. Beginners on Terraform should stick with the common commands. The ‘help’ files and program documents give guidance for other commands.

Common commands:

apply: Builds or changes infrastructure

console: Interactive console for Terraform interpolations

What is the configuration file?

Terraform works based on a ‘configuration.tf.’ file. The configuration file will define your infrastructure, as providers and resources as those are defined in either an HCL Terraform syntax or JSON format.

Create Terraform Config:

The underlying infrastructure is handled by a ‘provider,’ an abstract/high-level management method, which also manages resource life cycles.

Resources are infrastructure components, such as containers, instances, or OS images.

Contents of the configuration file:

The text editor allows you to write the configure.tf file. The section below defines how to apply a configuration to a docker host:

"docker" {

host = "tcp://docker:6541/" }

Now we can start developing our infrastructure resources. Our first resource is the Docker image.

Any resource will have two parameters: TYPE, and NAME. The TYPE is a docker image and then the NAME is ‘nextimage.’ In the block, we also define the tags and name of the Docker Image.

image" {

name = " nextimage:1.1-linux"[1] }

We can define our container resource by identifying its resource type as ‘docker_container,’ and its name as ‘nextimage-server.’ The block will set the resource parameters, and also allows us to reference other resources, such as the image.

"docker_container" "nextimage-server" {

name = "nextimage-server"

image = "${ nextimage:1.1-linux }"

ports {

internal = 80,443

}

volumes {

container_path = "/usr/share/nextimage/html"

host_path = "/var/bin/www"

read_only = true

}

}

How to implement the Configuration file?

After the configuration file is defined, we will create an execution plan. We can define this in terraform to define the required state. The plan can be saved using -out.

Command:

terraform plan -out configure.tfplan

Output:

terraform plan -out configure.tfplan

Refreshing Terraform state in-memory before the plan…

The refreshed state will calculate the new plan, but is not persisted to local or remote state storage…

Path: configure.tfplan

+ docker_container.nginx-server

bridge: " computed "

gateway: " computed "

image: "${nextimage:1.1-linux}"

ip_address: " computed "

ip_prefix_length: " computed "

log_driver: "json-file"

must_run: "true"

name: "nextimage-server"

ports.#: "1"

ports.985670353.external: ""

ports.985670353.internal: "80, 443"

ports.985670353.ip: ""

ports.985670353.protocol: "tcp"

restart: "no"

volumes.#: "1"

volumes.3224431186.container_path: "/usr/share/nextimage/html"

volumes.3224431186.from_container: ""

volumes.3224431186.host_path: "/var/bin/www"

volumes.3224431186.read_only: "true"

volumes.3224431186.volume_name: ""

Applying terraform actions

Configurations to reach the desired state are applied after the plan has been created.

Command:

terraform apply

Output:

terraform apply

docker_image.nginx: Creating...

latest: "" = " computed "

name: "" = " nextimage:1.1-linux"

docker_image.nginx: Creation complete

docker_container.nginx-server: Creating...

bridge: "" = " computed "

gateway: "" = " computed "

image: "" =

"sha256:bedece1f06cc142829698e6ba8f04d7f92e7f1b94b985e14b65f55e6ae4858c

2

How to inspect terraform infrastructure?

The terraform infrastructure can be verified by running the terraform output

Command:

terraform show

Output:

Page 5

terraform show

docker_container. nextimage:1.1-linux:

id = 10bca5531a25e5093d955e93fbb444080bbfb7f2408c826a5a561aef05e847cd

bridge =

gateway = 192.168.1.1

image = sha256:bedece1f06cc142829698e6ba8f04d7f92e7f1b94b985e14b65f5

ip_address = 192.168.1.10

ip_prefix_length = 24

log_driver = json-file

must_run = true

name = nginx-server

How to update the infrastructure?

The changes can be made to the infrastructure to ensure the desired state is met. We can change the contents of the container to launch two instances.

[1]resource "docker_container" "nextimage-server" {

count = 2 name = "nextimage-server--${count.index+1}”

image = "${ nextimage:1.1-linux }"

ports {

internal = 80,443

}

volumes {

container_path = "/usr/share/nextimage/html"

host_path = "/var/bin/www"

read_only = true

And that’s it! Did it work for you?

Subscribe to Our Blog

Never miss a post! Stay up to date with the latest database, application and analytics tips and news. Delivered in a handy bi-weekly update straight to your inbox. You can unsubscribe at any time.

12c Upgrade Bug with SQL Tuning Advisor

This blog post outlines steps to take on Oracle upgrade 11.2 to 12.1 if you’re having performance problems. Oracle offers a patch and work around to BUG 20540751.

Megan Elphingstone | March 22, 2017
sharepoint ideas

9 Awesome Things You Can Do with SharePoint

This blog post discusses out-of-the-box uses for SharePoint that you can execute in just a few clicks without using any code.

Amol Gharat | May 23, 2017
Oracle DBA Skills

8 Things Every Beginner Oracle DBA Should Know

A checklist of eight critical skills and areas an entry-level Oracle DBA should be familiar with to succeed on the job.

Patrick Gates | November 30, 2016

Work with Us

Let’s have a conversation about what you need to succeed and how we can help get you there.

CONTACT US

Work for Us

Where do you want to take your career? Explore exciting opportunities to join our team.

EXPLORE JOBS