filmov
tv
Understanding Variables in Terraform

Показать описание
In this video, we're diving into a super important concept in Terraform: variables. If you've ever wanted to make your Terraform scripts more dynamic and reusable, this video is for you. I’ll walk you through different aspects of variables in Terraform, including setting default values, adding descriptions, using validation, and managing sensitive data. Let’s get started!
Chapters
*********
00:00 INTRO
00:13 What Are Variables?
01:25 Variable Default Value
01:41 Variable Description
02:03 Variable Validation
04:13 'Sensitive' attribute in Variables
05:07 OUTRO
[SEGMENT 1: What Are Variables?]
Variables in Terraform act like placeholders for data that might change, allowing us to write more generic and flexible scripts. We define variables using the variable keyword, followed by a unique name. Here’s a basic example:
variable "location" {
type = string
}
When running the terraform plan command, Terraform will prompt you to specify the value for the location variable.
[SEGMENT 2: Default Values]
If no value is provided for a variable, Terraform can use a default value. This is helpful for creating more flexible scripts. Here’s how you can define a default value:
variable "location" {
type = string
default = "East US"
}
Now, if you don't specify a location when prompted, it will automatically use "East US" as the default.
[SEGMENT 3: Adding Descriptions]
To make your scripts more readable, include a description for each variable. This helps others understand the purpose of the variable quickly.
variable "location" {
type = string
default = "East US"
description = "The Azure region where resources will be deployed."
}
Adding a description makes it clear what each variable represents.
[SEGMENT 4: Validation]
Starting with Terraform 0.13, you can add a validation block to ensure that a variable meets certain conditions. Here’s an example that validates the VM size:
variable "vmSize" {
type = string
validation {
error_message = "VM size must be either 'Standard_DS1_v2' or 'Standard_DS2_v2'."
}
}
If an invalid value is provided, Terraform will display the specified error message. I demonstrate this in the video by intentionally entering an incorrect value.
[SEGMENT 5: Sensitive Variables]
To protect sensitive information, like passwords, use the sensitive attribute. When set to true, the value won't be shown in the Terraform CLI, making it ideal for secure data.
variable "adminPassword" {
type = string
description = "The admin password for the Azure virtual machine."
sensitive = true
}
When running the terraform plan command, the admin password prompt will not show the characters you type.
Chapters
*********
00:00 INTRO
00:13 What Are Variables?
01:25 Variable Default Value
01:41 Variable Description
02:03 Variable Validation
04:13 'Sensitive' attribute in Variables
05:07 OUTRO
[SEGMENT 1: What Are Variables?]
Variables in Terraform act like placeholders for data that might change, allowing us to write more generic and flexible scripts. We define variables using the variable keyword, followed by a unique name. Here’s a basic example:
variable "location" {
type = string
}
When running the terraform plan command, Terraform will prompt you to specify the value for the location variable.
[SEGMENT 2: Default Values]
If no value is provided for a variable, Terraform can use a default value. This is helpful for creating more flexible scripts. Here’s how you can define a default value:
variable "location" {
type = string
default = "East US"
}
Now, if you don't specify a location when prompted, it will automatically use "East US" as the default.
[SEGMENT 3: Adding Descriptions]
To make your scripts more readable, include a description for each variable. This helps others understand the purpose of the variable quickly.
variable "location" {
type = string
default = "East US"
description = "The Azure region where resources will be deployed."
}
Adding a description makes it clear what each variable represents.
[SEGMENT 4: Validation]
Starting with Terraform 0.13, you can add a validation block to ensure that a variable meets certain conditions. Here’s an example that validates the VM size:
variable "vmSize" {
type = string
validation {
error_message = "VM size must be either 'Standard_DS1_v2' or 'Standard_DS2_v2'."
}
}
If an invalid value is provided, Terraform will display the specified error message. I demonstrate this in the video by intentionally entering an incorrect value.
[SEGMENT 5: Sensitive Variables]
To protect sensitive information, like passwords, use the sensitive attribute. When set to true, the value won't be shown in the Terraform CLI, making it ideal for secure data.
variable "adminPassword" {
type = string
description = "The admin password for the Azure virtual machine."
sensitive = true
}
When running the terraform plan command, the admin password prompt will not show the characters you type.