Day 63 - Terraform Variables πŸ”§

Nilkanth MistryNilkanth Mistry
3 min read

Hello, DevOps enthusiasts! Welcome to Day 63 of the #90DaysOfDevOpsChallenge. Today, we'll explore variables in Terraform. We'll see how they help manage configurations effectively. Let's dive in and get hands-on with some practical examples on AWS! 🌟

Understanding Terraform Variables 🧩

Variables in Terraform are essential for storing values such as instance names, configurations, and more. They help make your code modular and reusable.

Install Terraform:

sudo apt-get update
sudo apt-get install -y gnupg software-properties-common curl
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update
sudo apt-get install terraform
terraform -version

Step 1: Creating Variables in Terraform πŸ“œ

  1. Create a new file named variables.tf:

     touch variables.tf
    

    πŸ“ File created!

  2. Add the following variable definitions in variables.tf:

     variable "filename" {
       default = "/home/ubuntu/terraform-tutorials/terraform-variables/demo-var.txt"
     }
    
     variable "content" {
       default = "This is coming from a variable which was updated"
     }
    

    ✏️ Variables defined!

Step 2: Using Variables in main.tf 🌐

  1. Create a new main.tf file if you don't have one already:

     touch main.tf
    

    πŸ“„ File created!

  2. Add the following code in main.tf to use the variables:

     resource "local_file" "devops" {
       filename = var.filename
       content  = var.content
     }
    

    πŸ“œ Variables used in resource!

Data Types in Terraform πŸ“š

Terraform supports various data types like Map, List, Set, and Object. Let's see some examples.

Step 3: Using Map Data Type πŸ—ΊοΈ

  1. Add a map variable to variables.tf:

     variable "file_contents" {
       type = map(string)
       default = {
         "statement1" = "this is cool"
         "statement2" = "this is cooler"
       }
     }
    

    πŸ—ΊοΈ Map variable added!

Step 4: Using List, Set, and Object Data Types πŸ“‹

  1. Add list, set, and object variables to variables.tf:

     variable "my_list" {
       type    = list(string)
       default = ["one", "two", "three"]
     }
    
     variable "my_set" {
       type    = set(string)
       default = ["apple", "banana", "cherry"]
     }
    
     variable "my_object" {
       type = object({
         name   = string
         age    = number
         active = bool
       })
       default = {
         name   = "John Doe"
         age    = 30
         active = true
       }
     }
    

    πŸ“‹ Variables for list, set, and object added!

Step 5: Using these Variables in main.tf πŸ“‘

  1. Access and print these variables in main.tf:

     output "list_output" {
       value = var.my_list
     }
    
     output "set_output" {
       value = var.my_set
     }
    
     output "object_output" {
       value = var.my_object
     }
    

    πŸ”„ Variables accessed and outputs defined!

Step 6: Initialize and Apply the Configuration πŸ’»

  1. Initialize your Terraform configuration:

     terraform init
    

    🏁 Initialization complete!

  2. Apply the Terraform configuration to see the outputs:

     terraform apply
    

    Review the execution plan and confirm by typing yes. βœ”οΈ

Step 7: Refreshing the State πŸ”„

  1. Refresh the Terraform state:

    terraform refresh
    

    This reloads the variables and updates the state file. πŸ”„ State refreshed!

Summary πŸ“š

Today, we've covered how to:

  • Define and use variables in Terraform.

  • Utilize different data types like map, list, set, and object.

  • Initialize and apply Terraform configurations.

  • Refresh Terraform state.

Keep practicing and happy learning! πŸš€


Feel free to reach out if you have any questions or run into any issues. See you tomorrow for another exciting day of the #90DaysOfDevOpsChallenge! 🌟

0
Subscribe to my newsletter

Read articles from Nilkanth Mistry directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nilkanth Mistry
Nilkanth Mistry

Embark on a 90-day DevOps journey with me as we tackle challenges, unravel complexities, and conquer the world of seamless software delivery. Join my Hashnode blog series where we'll explore hands-on DevOps scenarios, troubleshooting real-world issues, and mastering the art of efficient deployment. Let's embrace the challenges and elevate our DevOps expertise together! #DevOpsChallenges #HandsOnLearning #ContinuousImprovement