Skip to main content

Command Palette

Search for a command to run...

Terraform variable

Published
•5 min read•View as Markdown
Terraform variable
K

With a rich history spanning decades in Industrial IT, I currently serve as a Technical Consultant at onshore. My career has been defined by my unwavering commitment to DevOps and leadership in database administration projects.

Cloud Expertise: I possess extensive knowledge and hands-on experience in the realms of Microsoft Azure and AWS Cloud. I've played a pivotal role in architecting and optimizing cloud solutions that enable businesses to scale, innovate, and thrive.

Cloud DB and DevOps Advocate: My pursuit of excellence never stops. I'm on a constant quest to acquire deep knowledge in Cloud Database solutions and DevOps tools. From Azure DevOps to Jenkins, Ansible to GitHub, Terraform to Docker and Kubernetes, I'm committed to staying at the forefront of industry trends.

I'm passionate about leveraging technology to drive success in industrial IT. Let's connect and explore how my decades of experience and expertise can contribute to your organization's growth and transformation.

Terraform relies on variables to incorporate external values into its setup, allowing for the construction of infrastructure without directly embedding specific values within the configuration. These variables play a pivotal role in Terraform, particularly when managing instance names and configurations.

To maintain an orderly structure, we consolidate all our variables within a designated file named 'variables.tf'. Here's an illustration:

Task-01

Create a local file using Terraform.

Lets Create a variable.tf file

In this file, we set up two variables. One called filename stores the path where the local file will be created, and the other, content, holds the text that will be written to the local file..

Now in main.tf will call this variable value.

Within this file, we utilize Terraform to generate a resource termed 'local_file' with the identifier "devops." Think of this resource as a guidebook instructing Terraform on specific actions to execute. It directs Terraform to generate a file on your system, dictating both the name and content based on the 'filename' and 'content' variables.

Here's a breakdown of the key components:

  • resource "local_file" "devops": This command instructs Terraform to create a resource of type 'local_file' and names it "devops."

  • filename = var.filename: Here, we set the local file's name to correspond with the value stored in the 'filename' variable.

  • content = var.content: Similarly, this line sets the content of the local file according to the value stored in the 'content' variable.

Execute the script and check result

Upon navigating to the directory, you'll notice the presence of a newly created file named 'var-demo.txt'

Task-02

  • Use terraform to demonstrate usage of List, Set and Object datatypes

  • Put proper screenshots of the outputs

1.Map:

A map is like a labelled collection of values, where each value is identified by a unique string label. It is like dictionary in programming languages i.e having key-value pair.

To understand map we have crafted below variable script.

The declaration 'variable "content_map"' establishes a variable named 'content_map'. In Terraform, this variable functions as a map, serving as a method to organize data with associated labels.

The line 'type = map' explicitly defines the variable's type as a map. Within Terraform, a map represents a structure for storing key-value pairs.

Moreover, 'default = { "content1" = "this is my content " "content2" = "this is cooler content" }' sets a default value for the 'content_map' variable.

This default value constitutes a map containing two key-value pairs. The keys 'content1' and 'content2' correspondingly hold the strings 'this is my content' and 'this is cooler content'.

In this part, the content_map variable is applied within two local_file resources:

  • resource "local_file" "devops": Declares a resource of type local_file with the name "devops."

    • filename = "/home/ubuntu/terraform/terraform-variable/type-map/demo.txt": Specifies the filename for the local file, defining a specific path and filename.

    • content = var.content_map["content1"]: Sets the content of the local file to the value associated with the key "content1" in the content_map variable. In this case, it will be "this is cool content1" based on the default value set earlier.

  • resource "local_file" "devops_var": Declares a second resource of type local_file with the name "devops_var."

    • filename = var.filename: Specifies the filename for this new local file, taking its value from the filename variable.

    • content = var.content_map["content2"]: Sets the content of this local file to the value associated with the key "content2" in the content_map variable. In this case, it will be "this is cooler content2" based on the default value set earlier.

Lets apply the changes.

Verify the new files.

Object:

Object: An object is like a container that can hold different types of values, making it distinct from maps and lists. It's a collection of named attributes, each with its own type.

we have added objects in our varriable.tf file and its looks like below

In this section, we define a Terraform variable named aws_ec2_object as an object with a default value. The object includes properties for configuring an EC2 instance.

  • variable "aws_ec2_object": Declares a variable named aws_ec2_object as an object. This variable represents a structured collection of attributes.

  • type = object({...}): Specifies the type of the variable, defining its structure with named attributes and their respective types.

  • default = {...}: Sets a default value for the aws_ec2_object variable. The default value is an object with properties like "name," "instances," "keys," and "ami," each configured with specific values.

In the main.tf file, we use an output block to display the value of the "ami" property in the Terraform output after applying the configuration.

  • output "aws_ec2_instances": Declares an output named "aws_ec2_instances."

    • value = var.aws_ec2_object.ami: Specifies that the output should display the value of the "ami" property from the aws_ec2_object variable.

Lets execute the script

Upon applying, will see the output displaying the value of the "ami" property in the Terraform output.

Set:

A set is like a bag of unique values, where the order doesn't matter. To declare a set variable, use the set type and specify the type of elements in the set.

add below code in variable.tf

we define a Terraform variable named "security_groups" as a set of strings with a default value.

  • variable "security_groups": Declares a variable named "security_groups" as a set. This variable represents an unordered collection of unique values of the same type.

  • type = set(string): Specifies that the variable is of type set, and the elements within the set are of type string.

  • default = ["sg-93939393", "sg-22222222"]: Sets a default value for the "security_groups" variable. The default value is a set containing two strings representing security group IDs.

Now, you can use this variable in Terraform configuration to reference the set of security group IDs.

Refreshing the Terraform State

Executing 'terraform refresh' is a command within Terraform designed to align your resource states with the configuration file. Upon running 'terraform refresh', it fetches the present status of the resources as defined in your configuration and updates the Terraform state file to precisely mirror that current state

Command :

terraform refresh

Thanks for reading !!

Thanks,

Kishor Chavan

More from this blog

Kishor Chavan

62 posts

Dynamic IT professional more than decade industrial experience.