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.
Command: output | Terraform | HashiCorp Developer | The terraform output command is used to extract the value of an output variable from the state file.
How to debug print terraform variable values when the plan step fails | Iām reviving an old terraform script where a variable caused the plan step to fail. How to debug print such terraform variable values during it run? $ terraform console > var.name "demo" $ make apply terraform apply ā Error: "name" may only contain alphanumeric characters, dots, dashes and underscores ā ā with module.linux.azurerm_linux_virtual_machine.mytfvm, ā on linux/main.tf line 80, in resource "azurerm_linux_virtual_machine" "mytfvm": ā 80: name = "${var.name}"...
Output Values - Configuration Language | Terraform | HashiCorp ... | Output values make information about your infrastructure available on the command line, and can expose information for other Terraform configurations to use.
How to Use Terraform Variables: Examples & Best Practices | Terraform variables types and how to use them. Learn how to use local, input, output, and environment variables. See how to mark Terraform variables as sensitive.
How to show sensitive values - Terraform - HashiCorp Discuss | I can see how sensitive values need to be protected in CI/CD, but at my terminal I would like to see them. In some cases, there is a workaround, eg for outputs that are marked sensitive you can use json output. BUT this trick does not work for terraform apply: sensitive values are masked, and if the value is say a json blob (created from a template), the whole blob is masked. There is then no way for me to know what actually caused the change in that json/value. Is there a way to tell terraform...
Terraform Output Values : Complete Guide & Examples | What are Terraform outputs? See the different options for their configuration and how Terraform handles and exports output values between modules.
Terraform Output Command : Examples, Tips and Best Practices ... | A practical in-depth guide on Terraform outputs with detailed examples, advanced use cases, and more.