Day 63 - Terraform Variables π§
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 π
Create a new file named
variables.tf
:touch variables.tf
π File created!
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
π
Create a new
main.tf
file if you don't have one already:touch main.tf
π File created!
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 πΊοΈ
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 π
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
π
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 π»
Initialize your Terraform configuration:
terraform init
π Initialization complete!
Apply the Terraform configuration to see the outputs:
terraform apply
Review the execution plan and confirm by typing
yes
. βοΈ
Step 7: Refreshing the State π
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! π
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