What are variables in terraform?
In Terraform, variables are parameters that you can use to input values into your configurations. They allow you to define flexible and dynamic Terraform configurations that can be customized based on different use cases or environments.
Variables are declared using the variable
block in your Terraform configuration files.
Task-01: Create a local file using Terraform
Step 1: Create variable.tf
Create a file named variable.tf
with the following content:
variable "file_content" {
type = string
default = "This is the content of the local file.\n"
}
variable "file_path" {
type = string
default = "/path/to/myfile.txt"
}
This step defines two variables:
file_content
: Contains the content you want to write to the local file.file_path
: Specifies the path where the local file will be created.
Step 2: Create main.tf
Create a file named main.tf
with the following content:
provider "local" {}
resource "local_file" "example" {
content = var.file_content
filename = var.file_path
}
This step defines a local_file
resource that will create a local file with the specified content and path.
Step 3: Initialize and Apply
Run the following commands in the terminal:
terraform init
terraform apply
Terraform will prompt you to confirm the creation of the local file. Type yes
to proceed.
Step 4: Verify
Check the specified path (/path/to/myfile.txt
in this example) to confirm that the local file has been created with the specified content.
Task -02: Data Types in Terraform
In Terraform, data types are used to define the kind of values that can be assigned to variables and used in resource configurations.
Terraform has several built-in data types. Here are some of the key data types in Terraform:
String: Represents a sequence of characters. Strings are used for various purposes, such as defining resource names, tags, and other textual values.
variable "example_string" { type = string default = "Hello, Terraform!" }
Number: Represents numeric values, either integers or floating-point numbers.
variable "example_number" { type = number default = 42 }
Bool: Represents boolean values, either
true
orfalse
.variable "example_bool" { type = bool default = true }
List: Represents an ordered collection of values. Lists are useful when you need to work with multiple items of the same type.
variable "example_list" { type = list(string) default = ["item1", "item2", "item3"] }
Map: Represents a collection of key-value pairs. Maps are useful for associating values with specific keys.
variable "example_map" { type = map(string) default = { key1 = "value1" key2 = "value2" } }
Object: Represents a complex data structure with attributes and nested blocks. Objects are useful for modeling more structured data.
variable "example_object" { type = object({ name = string age = number is_male = bool }) default = { name = "John Doe" age = 30 is_male = true } }
Set: A set is an unordered collection of unique values. It's used for managing distinct elements, and the order doesn't matter. Each element must be unique.
variable "example_set" { type = set(string) default = ["element1", "element2", "element3"] }
Tuple: A tuple is an ordered collection of values where each element can have a different data type. Tuples group related data in a specific order, and you access elements by their index.
variable "example_tuple" { type = tuple([string, number, bool]) default = ["value1", 42, false] }