Day 2: Mastering Terraform Variables - A Comprehensive Guide
Shrikar Lonkar
DevOps Engineer@HeroVired ??Kubernetes, Docker, Terraform, Ansible ????? | CI/CD with Jenkins ?? | Git & GitHub ???? | AWS Cloud Explorer ??
Day 2: Mastering Terraform Variables - A Simplified Guide
Welcome back to our Terraform learning journey! Today, we'll unravel the power of Terraform variables. Let's dive right in and make our infrastructure more dynamic and flexible.
Recap of Day 1.1: Terraform and Docker Harmony
In our previous adventure, we witnessed the seamless integration of Docker into Terraform, orchestrating containers with precision. Now, armed with this knowledge, let's move forward to Day 2, where we'll delve into the realm of Terraform variables.
Understanding the Challenge
Imagine you've created a Terraform configuration to generate a local file named devops.txt. The catch? If you want to change the filename, you're tempted to modify the main.tf file directly. However, this goes against coding best practices. The solution? Harness the power of Terraform variables!
Introducing Terraform Variables
Variables in Terraform act as containers for values, allowing us to create dynamic and reusable infrastructure. But before we jump into the code, let's organize our project structure:
resource local_file "devops_var" {
filename = "/study/terraform/Day2/devops.txt"
content = "this is Day2 of terraform"
}
Creating Variables
Let's start by creating a variables.tf file:
variables.tf
variable "filename" {
default = "/study/terraform/Day2/devops_variables.txt"
}
variable "content" {
default = "this is autogenerated from Terraform variables"
}
In this file, we define two variables: filename and content. The default attribute initializes their values.
Using Variables in main.tf
Now, let's leverage these variables in our main.tf:
main.tf
resource "local_file" "devops_var" {
filename = var.filename
content = var.content
}
In the resource block, we use the var. syntax to reference our variables. This separation allows us to modify configurations without directly changing the main.tf file.
Understanding the Difference
Now, let's run terraform init, terraform plan, and terraform apply. Our new local file, devops_variables.txt, will be created with the specified content.
Difference between Resources:
resource "local_file" "devops_var" {
filename = "/study/terraform/Day2/devops.txt"
content = "this is Day2 of Terraform"
}
resource "local_file" "devops_var" {
filename = var.filename
content = var.content
}
By utilizing variables, we've made our configuration more adaptable without directly modifying the main.tf file.
Primitive Variable Types
String:
Strings in Terraform are your go-to choice for handling any text-based values. Whether it's a region, a username, or a simple label, strings have you covered. Here's a quick dive into using strings:
variable "aws_region" {
type = string
default = "ap-south-1"
}
领英推荐
In this example, aws_region is a string variable holding the AWS region "us-west-2". You can customize it with any text-based value fitting your infrastructure needs.
Number:
Numbers, as the name suggests, deal with numeric values, both integers and floating-point numbers. They're handy when specifying quantities or numerical configurations. Let's jump into an illustration:
variable "instance_count" {
type = number
default = 4
}
Here, instance_count is a number variable set to the default value of 3. It could represent the number of instances you want to deploy. Terraform handles it as a numerical entity, allowing for precise quantity definitions.
Bool:
The Bool type is capable of holding either true or false values. For example, if we define the logging variables as a type of bool with a value defaulting to true, the logging function will be enabled. You can change the default value from true to false to disable it.
variable "logging" {
type = bool
default = true
}
Exploring Complex Data Types
Terraform supports various complex data types. Let's briefly touch on a few:
Map:
variable "content_map" {
type = map
default = {
content1 = "this is cool content1"
content2 = "this is great content"
}
}
List:
List variables are used to sequentially pass multiple elements to Terraform configuration
variable "file_list" {
type = list
default = ["/study/terraform/Day2/file_1.txt", "/study/terraform/Day2/file_2.txt"] }
Object:
The object variable allows you to create variables that contain structured data and named attributes. This is similar to a map but has fixed names and types for the attributes.
variable "aws_ec2_object" {
type = object({
name = string
instances = number
keys = list(string)
ami = string })
default = {
name = "test_ec2_instances"
instances = 2
keys = ["keys1.pem", "keys2.pem"]
ami = "ami-11664"
} }
variable "security_groups" {
type = set(string)
default = ["sg-12345678", "sg-abcdefgh"]
}
Tuple:
Unlike sets, tuples in Terraform maintain order. They are an ordered collection of elements, making them handy when sequence matters. Let's explore a tuple in action:
variable "IP_addresses" {
type = tuple([string, string])
default = ["192.168.20.10", "192.168.10.22"]
}
Conclusion
In this immersive exploration of Terraform variables, you've delved into the intricacies of complex data types and harnessed the power of dynamic configurations. By adopting the best practice of segregating variables into a dedicated file, you've laid a robust foundation for your Terraform projects.
Stay tuned for more thrilling Terraform adventures as we continue to unravel its capabilities and make infrastructure as code an exhilarating journey. Happy coding, and may your Terraform endeavors be nothing short of magical! ???? #TerraformMagic #InfrastructureAsCode #Day2 #VariablesUnleashed
Assistant Consultant at Tata Consultancy Services | 3x AWS Certified | 1x Google Cloud Certified | Technology Mentor | Everyday Learner
1 年Great work Shrikar Lonkar ?? Likewise youtube video can be find under my youtube channel - https://youtu.be/EHmYsYbVukU?si=fqRexqFaDr-_wFFq ??