8i | 9i | 10g | 11g | 12c | 13c | 18c | 19c | 21c | 23c | Misc | PL/SQL | SQL | RAC | WebLogic | Linux

Home » Articles » Misc » Here

Terraform : A Beginner's Guide

This article presents what I think is the minimum information needed to start using Terraform.

Related articles.

What is Terraform?

Terraform is a tool to define the infrastructure and services that make up a system. It allows us to not only build all the pieces of the system, but track and apply changes to a system over time. It's based on configuration files, which can be checked into version control, helping us achieve Infrastructure as Code.

Terraform is a very high-level view of the solution. For a system using cloud-based services, Terraform may be all you need to achieve infrastructure as code. For more traditional on-prem environments, or Infrastructure as a Service (IaaS) environments in the cloud, you may need to combine Terraform to manage your infrastructure, and a lower level tool such as Ansible to maintain the internal state of servers over their lifespan.

Common Terms

Before we get going, let's define some terms.

File Names and File Extensions

File names are not as important as file extensions. By default all files with the ".tf" extension will be loaded by Terraform. Even so, we will often see some common file names.

We don't need to use these file names, and we don't need to split Terraform definitions into different files. You'll see in some of my examples I keep the resource, variable and output definitions in a single file for simplicity. Having said that, module definitions should probably follow these naming conventions.

Variable Values

Assigning values to variables can be accomplished in several ways. Some ways were mentioned above, but there is a separate article about it here. There is no "best" method, but we should probably avoid putting variable values into source control. We don't want to risk accidentally exposing sensitive data to the outside world.

Installation

The installation process will vary depending on your client operating system. You can see examples of the installation for Windows and Linux here.

Init

Once we have a Terraform definition we're going to want to build something. The first step is to initialize the directory holding our files. We do this using the terraform init command.

We have a file called "main.tf" in the "C:\git\oraclebase\terraform\beginners\intro" directory with the following contents. It defines an input variable and an output variable.

variable "my_variable" {
  type    = string
  default = "default-value"
}

output "my_variable_output" {
  value = var.my_variable
}

We initialize the directory as follows.

cd C:\git\oraclebase\terraform\beginners\intro
terraform init

Once the initialization is complete, a ".terraform.lock.hcl" file is created.

Plan

With the directory initialized, we can use the terraform plan command to create an execution plan. This is like a dry run of a build, which allows us to check what operations will take place if we do a real build. In this case we aren't building anything, so all we get is variable definition and default value displayed.

terraform plan

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:

Terraform will perform the following actions:

Plan: 0 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + my_variable_output = "default-value"

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

We can save the plan using the -out flag.

terraform plan -out=plan.out

Apply

When we are ready to perform a build, we use the terraform apply command. If no plan is specified, a new plan is generated and the build is done using that plan. Since we don't have a provider or any resources defined, the apply command doesn't build anything, but it saves the build state in a "terraform.tfstate" file. In the example below we perform a terraform apply, passing a variable value on the command line using the -var flag. We use the -auto-approve flag so we don't have to manually confirm the action.

terraform apply -var="my_variable=melon" -auto-approve

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

my_variable_output = "melon"

We can apply an existing plan by giving the name of the plan file on the command line.

terraform apply plan.out

Next Steps

Now we know what terraform is and some of the basics of how to use it, it's time to build some things with it. It's worth reading the article about Terraform variables, then try building some components on Oracle Cloud Infrastructure (OCI) following the articles here. You can do that for free using the Oracle Cloud Free Tier.

For more information see:

Hope this helps. Regards Tim...

Back to the Top.