What is Terraform?
Terraform Basics, Setup and Modules
What is Terraform?
Terraform is an open-source infrastructure as code software tool that provides a consistent CLI workflow to manage hundreds of cloud services. Terraform codifies cloud APIs into declarative configuration files.
Terraform is not a programming language but it borrows concepts like looping, modularization, operators, etc from programming languages. Terraform that will allow you to start creating and deploying infrastructure on the cloud.
Terraform Architecture and Components through diagram
Main Terraform Commands:
In a particular directory with resource definition (.tf) files, first:
- terraform init
To see what a change would do, but don’t actually do it:
- terraform plan. (Run
terraform plan
to check whether the execution plan for a configuration matches your expectations before provisioning or changing infrastructure.)
To actually do it:
- terraform apply (Apply changes to hundreds of cloud providers with
terraform apply
to reach the desired state of the configuration.)
To decommission something no longer needed:
- terraform destroy
Terraform Main files:
- main.tf (Main file where we write the code)
- variables.tf. (store all the variables)
- version.tf. (Terraform version is there)
- terraform.tfstate (Terraform records information about what infrastructure it created in a tfstate file)
Variables
Like programming languages, Terraform has variables and values. Variables are names used to hold one or more values. All values have types that specify where that value can be used.
The following are the types available in Terraform :
- string: a sequence of Unicode characters representing some text. For example, text = “Hello”
- number: a numeric value. For example, num= 1 or num = 1.3
- bool: a boolean value, either true or false. For example, b= false
- list or tuple: a sequence of values, like [“a”, “b”]. Elements in a list or tuple are identified by consecutive whole numbers, starting with zero. For example, l = [“a”, “b”]
- map or object: a group of values identified by named labels or key-value pairs. For example, m = {“a” = 1, “b” = 2}
- null: If you set an argument of a resource or module to null, Terraform behaves as though you had completely omitted it. For example, r= null
How To Define Variables in Terraform
It is a good practice to declare variables in a file called variables. tf. Here .tf represents a terraform file.
To declare a variable, use the following syntax:
variable "variable-name" {
type = string
default = "value"
}
Okay, if all variables are in variables. tf file, how are they being utilized in the main logic ( or main. tf )?
The answer: Input Variables
Within the module that declared a variable, its value can be accessed by using the syntax: var.<variable-name>.
resource "cloud_instance" "example" {
name = var.variable-name
}
Note: Input variables are created by a variable
block, but you reference them as attributes on an object named var
.The value assigned to a variable can only be accessed in expressions within the module where it was declared.
Modules
Modules are equivalent to libraries of programming languages. They contain infrastructure code that can be re-used. Every Terraform configuration has at least one module, known as its root module.
Calling Modules
It is a good practice to create child modules or libraries of infrastructure that can be used instead of writing all code from scratch.
Calling a child module means including all code of that module in the calling directory. Code to call a module:
module "alias" {
source = "./appModule"
app-name = "myApp"
}
A module that includes a module
block is the calling module of the child module.
The label “alias” immediately after the module
keyword is a local name, which the calling module can use to refer to this instance of the module.
Between {
and }
are the arguments for the module. The source
argument is mandatory for all modules. It is the past where the child module is located.
When calling a module, it is important to give values for any variables present in the child module. The app-name
is a variable whose value the calling module is defining.
Accessing Output values of Modules
A child module can use output values to expose a subset of its resource attributes to a parent module.
Declaring an Output Value
Each output value exported by a module must be declared using an output
block:
output "app-Name-outputValue" {
value = "myApp"
}
#code of child module
Using the output value in another module:
resource "cloud_instance" "example" {
# ...
instances = module.alias.app-name
}
#code in calling module
Hope it Helps Stay Tuned for more blogs :)