Learn different techniques to easily print and view Terraform variable values during your infrastructure deployments.
When working with Terraform, understanding how to inspect and manage variables is crucial for debugging and ensuring your infrastructure is deployed correctly. This guide provides various techniques to view and work with Terraform variables, including sensitive data, while emphasizing best practices for security.
Basic Variable Inspection:
terraform init
, use terraform console
.var.<variable_name>
to see its value.
terraform console
> var.example_variable
Outputting Values:
output "example_output" {
value = var.example_variable
}
terraform output
to retrieve the value.
terraform output example_output
Debugging During Plan/Apply:
TF_LOG=debug
environment variable to increase log verbosity, which might offer clues.variable "example_variable" {
type = string
validation {
condition = length(var.example_variable) > 0
error_message = "Variable 'example_variable' cannot be empty."
}
}
Sensitive Values:
terraform output -json
and inspect the JSON output.terraform apply
.Remember: Avoid printing sensitive information directly to the console or logs. Use outputs and appropriate security practices to handle such data.
This Terraform code deploys an Azure storage account within a resource group. It defines variables for resource names and location, with default values for convenience. The code includes validation for the resource group name and marks the storage account name as sensitive. Outputs provide access to the deployed resource information, including the resource group location and the storage account name.
# Configure the Azure Provider
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.0"
}
}
}
# Configure Variables
variable "resource_group_name" {
type = string
description = "Name of the resource group"
validation {
condition = length(var.resource_group_name) > 0
error_message = "Variable 'resource_group_name' cannot be empty."
}
}
variable "location" {
type = string
description = "Azure region to deploy resources"
default = "westus2"
}
variable "storage_account_name" {
type = string
description = "Name of the storage account"
sensitive = true
}
# Create a Resource Group
resource "azurerm_resource_group" "example" {
name = var.resource_group_name
location = var.location
}
# Create a Storage Account
resource "azurerm_storage_account" "example" {
name = var.storage_account_name
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
account_tier = "Standard"
account_replication_type = "LRS"
}
# Define Outputs
output "resource_group_location" {
value = azurerm_resource_group.example.location
}
output "storage_account_name" {
value = azurerm_storage_account.example.name
sensitive = true
}
Explanation:
Basic Variable Inspection:
terraform init
, you can use terraform console
and var.<variable_name>
to inspect values. For example:
terraform console
> var.location
"westus2"
Outputting Values:
resource_group_location
and storage_account_name
.terraform output resource_group_location
terraform output storage_account_name
storage_account_name
is marked as sensitive and will be masked in the standard output.Debugging During Plan/Apply:
resource_group_name
variable has a validation rule to ensure it's not empty.TF_LOG=debug
to get more information during plan/apply.Sensitive Values:
storage_account_name
variable and output are marked as sensitive.terraform output -json
and inspect the JSON output.Remember: This is a basic example. You can adapt it to your specific needs and cloud provider. Always prioritize security and avoid exposing sensitive information directly in logs or console outputs.
General:
${}
to embed variable values within strings.1. Basic Variable Inspection:
terraform output <variable_name>
if the variable is also defined as an output.2. Outputting Values:
3. Debugging During Plan/Apply:
4. Sensitive Values:
Additional Tips:
This table summarizes various methods for inspecting and debugging Terraform variables:
| Method | Description
By mastering these techniques, you can effectively manage, inspect, and debug Terraform variables, ensuring smooth and secure infrastructure deployments. Remember to prioritize security by protecting sensitive values and following best practices for handling such data. This comprehensive guide equips you with the knowledge to confidently work with Terraform variables, paving the way for robust and secure infrastructure automation.