0% found this document useful (0 votes)
38 views

Variables in Terraform

Terraform variables allow making configurations dynamic by defining variables that can be assigned different values. Variables are defined in a .tfvars file and can have default types like string or number. Conditionals and locals help avoid duplicating values. Loops using count variables can create multiple resources. Data sources fetch external data and import brings existing infrastructure under management. Workspaces isolate environments using separate state files.

Uploaded by

Kumar P
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Variables in Terraform

Terraform variables allow making configurations dynamic by defining variables that can be assigned different values. Variables are defined in a .tfvars file and can have default types like string or number. Conditionals and locals help avoid duplicating values. Loops using count variables can create multiple resources. Data sources fetch external data and import brings existing infrastructure under management. Workspaces isolate environments using separate state files.

Uploaded by

Kumar P
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

VARIABLES IN TERRAFORM

To keep the things dynamic in terraform configuration and not hardcoding the values, we can
make use of variables.
We can define variables in a separate file and refer them to the code.
Approaches of variable Assignment
• Variables can be defined and mentioned in a number of ways.
• Default file is variable.tf (To define variables and default values)
• To specify explicit values, we have another file called terraform.tfvars
• terraform.tfvars is the default file name, if we have a custom file name, we can mention
it with command

Variable data types


We can explicitly define the data type of a variable; this will restrict the variable to accept only
that specific type of value.

variable "region-name" {
default = "us-east-1"
type = string
}
Here region-name variable can only accept string data type and if provided with any other data
type, it will throw an error.
Variable data types supported by terraform
Terraform supports a variety of data types like string, map, number, bool, list tuple.
To read in detail, please follow the below link
1. string
2. map
3. number
4. bool
5. list
6. any
7. tuple

CONDITIONAL EXPRESSIONS
A conditional expression uses the value of a bool expression to select one of two values.
Syntax of Conditional expression:
condition ? true_val : false_val
If the condition is true then the result is true_val. If the condition is false then the result is
false_val.
Let’s assume that there are two resource blocks as part of Terraform configuration.
Depending on the variable value, one of the resource blocks will run.

Locals
A local value assigns a name to an expression, allowing it to be used multiple times within a
module without repeating it.

• Local values can be helpful to avoid repeating the same values or expressions multiple
times in a configuration.
• If overused they can also make a configuration hard to read by future maintainers by
hiding the actual values used
• Use local values only in moderation, in situations where a single value or result is used in
many places and that value is likely to be changed in the future.

TERRAFORM FUNCTIONS
• The Terraform language includes a number of built-in functions that you can use to
transform and combine values.
• The general syntax for function calls is a function name followed by comma-separated
arguments in parentheses:
function (argument1, argument2)
Example:
> max(5, 12, 9)
12
The Terraform language does not support user-defined functions, and so only the functions
built into the language are available for use.
• Numeric

• String
• Collection
• Encoding
• Filesystem
• Date and Time
• Hash and Crypto
• IP Network
• Type Conversion
There is practically a large number of functions that terraform supports, to check how these
work, we can make use of terraform console command and test them out.
Ex
Using loops in terraform

Declaring count variables in a resource create multiple resources in a loop.


variable "subnet_cidrs" {

type = list(string)

default = ["172.21.0.0/24", "172.21.1.0/24", "172.21.2.0/24"]

resource "aws_subnet" "main" {

count = "3"

vpc_id = aws_vpc.main.id

cidr_block = var.subnet_cidrs[count.index]

tags = {

Name = "Subnet-${count.index + 1}"

Terraform Datasource

Using data source we can fetch certain information from aws account dynamically

For example i want of fetch list of azs in current region

Terraform Import
Let's say there is a resource manually created and we want that resource to be managed by

terraform then use “terraform import”

Terraform Workspace

Using terraform workspace we can manage multiple environments with the same configuration

files and using different state files.

Terraform maintains “default” workspace by default.


Vpc Env verification
Subnet Env verification

Env verification on aws s3 bucket:

You might also like