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?


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.



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 ‘’ 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 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.


terraform plan -out configure.tfplan


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.


terraform apply


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: "" =



How to inspect terraform infrastructure?

The terraform infrastructure can be verified by running the terraform output


terraform show


Page 5

terraform show

docker_container. nextimage:1.1-linux:

id = 10bca5531a25e5093d955e93fbb444080bbfb7f2408c826a5a561aef05e847cd

bridge =

gateway =

image = sha256:bedece1f06cc142829698e6ba8f04d7f92e7f1b94b985e14b65f5

ip_address =

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.

ORA-12154: TNS:could not resolve the connect identifier specified

Most people will encounter this error when their application tries to connect to an Oracle database service, but it can also be raised by one database instance trying to connect to another database service via a database link.

Jeremiah Wilton | March 4, 2009

12c Upgrade Bug with SQL Tuning Advisor

Learn the steps to take on your 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

Recover a Table from an RMAN Backup in an Oracle 12c

This blog post will is to show a table restore for one table in a container database.

Megan Elphingstone | February 2, 2017

Work with Us

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


Work for Us

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