☑️Day 66: Terraform Variables with Maps, Lists, and Complex Objects🚀

☑️Day 66: Terraform Variables with Maps, Lists, and Complex Objects🚀

🔹Table of Contents :

  • Introduction to Day 66: Advanced Terraform Variables

  • Task 1: Working with Maps in Terraform

  • Task 2: Utilizing Lists in Terraform

  • Task 3: Building Complex Objects

  • Key Commands and Explanations

  • Real-World Benefits of Advanced Variable Types

  • Conclusion


Today’s focus was all about using advanced variable types in Terraform to manage infrastructure configurations more dynamically and flexibly. By diving into maps, lists, and objects, we’ve added depth to our variables.tf and main.tf files for more control over the data we pass into Terraform configurations.


Task 1: Using Maps to Manage Configurations

In our first task, we created a map variable, content_map, which allows us to store multiple values under a single variable, each with a unique key. Here’s how we implemented it:

Step 1: Define the Map Variable

File: variables.tf

variable "content_map" {
  type = map
  default = {
    "content1" = "this is a cool content 1"
    "content2" = "this is a cooler content 2"
  }
}

Step 2: Modify main.tf to Access Map Values

Update main.tf to reference specific keys from content_map.

File: main.tf

resource "local_file" "devops-content1" {
  filename = "/home/ubuntu/terraform-course/terraform-local/content1.txt"
  content  = var.content_map["content1"]
}

resource "local_file" "devops-content2" {
  filename = "/home/ubuntu/terraform-course/terraform-local/content2.txt"
  content  = var.content_map["content2"]
}

Step 3: Apply Changes

Run the following commands to validate, plan, and apply changes:

terraform plan
terraform apply

Explanation:

  • terraform plan: Verifies what changes will be made and shows a preview.

  • terraform apply: Applies the changes to the infrastructure.


Task 2: Creating and Using Lists

Next, we explored lists by creating a file_list variable, which can store multiple file paths. This is useful when you want to manage a sequence of resources in a structured manner.

Step 1: Define the List Variable

File: variables.tf

variable "file_list" {
  type = list
  default = ["/pwd/file_1.txt", "/pwd/file_2.txt"]
}

Step 2: Update main.tf to Use List Elements

Modify main.tf to reference specific items in file_list:

resource "local_file" "devops-file1" {
  filename = var.file_list[0]
  content  = "File 1 content"
}

resource "local_file" "devops-file2" {
  filename = var.file_list[1]
  content  = "File 2 content"
}

Step 3: Run Terraform Commands

terraform plan
terraform apply

Task 3: Using Objects for Complex Configurations

The most complex task involved creating an object variable, aws_ec2_object, to represent attributes needed for provisioning an AWS EC2 instance.

Step 1: Define the Object Variable

File: variables.tf

variable "aws_ec2_object" {
  type = object({
    name      = string
    instances = number
    keys      = list(string)
    ami       = string
  })
  default = {
    name      = "test_ec2_instance"
    instances = 4
    keys      = ["key1.pem", "key2.pem"]
    ami       = "ubuntu-afed34"
  }
}

Step 2: Create an Output for aws_ec2_object

In main.tf, define an output to display the values of aws_ec2_object.

output "aws_ec2_instance" {
  value = var.aws_ec2_object
}

Step 3: Run Terraform Commands to Validate and Apply

terraform plan
terraform apply

Real-World Scenarios

  1. Maps: When you have settings that differ across environments, such as configuration options for dev, staging, and prod, maps make it easy to reference each environment’s specific configurations.

  2. Lists: For provisioning multiple similar resources (like file paths or container names), lists allow efficient looping and indexing without multiple variable declarations.

  3. Objects: Objects are ideal for handling structured data, like EC2 configurations, as they can combine strings, numbers, and lists. This is especially useful when working with modular infrastructures, ensuring all configuration properties are encapsulated in a single variable.


Summary of Commands

  • terraform init: Initializes the Terraform configuration.

  • terraform plan: Shows the intended actions before applying them.

  • terraform apply: Executes the plan to make the defined changes.


Conclusion

Using maps, lists, and objects within Terraform not only provides powerful data structures but also makes configurations significantly cleaner and reusable across projects. This level of variable management helps scale and maintain complex infrastructures effortlessly, making these concepts crucial for DevOps practices.


Stay tuned for more hands-on tasks and in-depth learning!🚀

🚀Thanks for joining me on Day 66! Let’s keep learning and growing together!

Happy Learning! 😊

#90DaysOfDevOps

💡
Follow for more updates on LinkedIn , Github and Twitter(X)