Working with .tfvars Files
This guide explains how to use the Terraform Editor API to create and manipulate .tfvars
files, which are commonly used to set variable values in Terraform configurations.
Overview
The API provides a special tfvars
block type specifically designed for working with .tfvars
files. This allows you to add, update, set, and delete top-level attributes directly, without the nested block structure that's typical in standard Terraform files.
Key features of the tfvars
block type:
- Attributes are written directly to the root level of the file
- No labels are required (unlike resources or other Terraform blocks)
- Supports all operations:
add
,update
,set
, anddelete
- Handles all Terraform data types: strings, numbers, booleans, lists, maps, and complex objects
Basic Usage
Creating a New .tfvars File
To create a new .tfvars
file with various types of attributes:
{
"code": "",
"edits": {
"add": {
"tfvars": [
{
"add": {
"attributes": {
"string_var": "hello world",
"number_var": 42,
"bool_var": true,
"list_var": ["a", "b", "c"],
"map_var": {
"key1": "value1",
"key2": "value2"
},
"complex_object": {
"nested": {
"value": "nested value"
},
"numbers": [1, 2, 3]
}
}
}
}
]
}
}
}
This produces a .tfvars
file with entries like:
string_var = "hello world"
number_var = 42
bool_var = true
list_var = ["a", "b", "c"]
map_var = {
key1 = "value1"
key2 = "value2"
}
complex_object = {
nested = {
value = "nested value"
}
numbers = [1, 2, 3]
}
Updating Variables
To update existing variables in a .tfvars
file:
{
"edits": {
"update": {
"tfvars": [
{
"update": {
"attributes": {
"string_var": "updated value",
"number_var": 100,
"complex_object": {
"nested": {
"value": "new nested value"
}
}
}
}
}
]
}
}
}
This operation will fail if any of the specified attributes don't exist in the file.
Setting Variables (Create or Update)
To set variables in a .tfvars
file (creating them if they don't exist, or updating them if they do):
{
"edits": {
"set": {
"tfvars": [
{
"set": {
"attributes": {
"existing_var": "new value",
"new_var": "this will be created"
}
}
}
]
}
}
}
This is useful when you're not sure if variables already exist in the file.
Deleting Variables
To remove variables from a .tfvars
file:
{
"edits": {
"delete": {
"tfvars": [
{
"delete": {
"attributes": ["string_var", "complex_object"]
}
}
]
}
}
}
Advanced Usage
Working with Environment-specific Variables
A common use case is maintaining different variable values for different environments:
{
"edits": {
"add": {
"tfvars": [
{
"add": {
"attributes": {
"environment": "staging",
"instance_count": 2,
"instance_type": "t3.small",
"enable_monitoring": true,
"allowed_ips": ["10.0.0.0/24", "10.0.1.0/24"],
"tags": {
"Environment": "staging",
"ManagedBy": "terraform",
"Team": "platform"
}
}
}
}
]
}
}
}
Working with Complex Nested Structures
The API supports deeply nested structures commonly used in Terraform:
{
"edits": {
"add": {
"tfvars": [
{
"add": {
"attributes": {
"vpc_config": {
"cidr_block": "10.0.0.0/16",
"subnets": {
"public": [
"10.0.1.0/24",
"10.0.2.0/24"
],
"private": [
"10.0.3.0/24",
"10.0.4.0/24"
]
},
"nat_gateway": {
"enabled": true,
"single_az": false
}
}
}
}
}
]
}
}
}
Using Interpolation in .tfvars
While variable definitions in .tfvars
files normally contain literal values, the API also supports Terraform interpolation syntax within these values. This is particularly useful when working with variable values that reference other variables or use Terraform functions.
{
"edits": {
"add": {
"tfvars": [
{
"add": {
"attributes": {
"region_specific_amis": {
"${var.aws_region}": "ami-1234abcd",
"${var.backup_region}": "ami-5678efgh"
},
"formatted_name": "${format(\"%s-%s\", var.project_name, var.environment)}",
"conditional_value": "${var.environment == \"prod\" ? \"high\" : \"low\"}"
}
}
}
]
}
}
}
For more details on using interpolation, see the Interpolation Support guide.
Best Practices
When working with .tfvars
files:
- Group related variables - Keep your edits organized by grouping related variables in a single operation
- Use descriptive variable names - Follow Terraform conventions for naming variables
- Consider file structure - For complex projects, consider using multiple
.tfvars
files for different environments or components - Document assumptions - When setting complex objects or nested structures, include comments explaining the expected format
Troubleshooting
Common issues when working with .tfvars
files:
- Invalid syntax in variable values - Ensure your JSON values are properly formatted and escaped
- Missing attributes during update - The
update
operation requires all attributes to exist; useset
if you're not sure - Type mismatches - Ensure you're providing the correct data types for your variables (e.g., numbers instead of strings)
Conclusion
The tfvars
block type provides a convenient way to manage Terraform variable values through the API. By supporting all operations and all Terraform data types, it gives you complete control over your .tfvars
files while maintaining the simple structure needed for variable definitions.