Terraform variable using TF_VAR environment variables example
How to set Terraform Variables in Windows?
Steps to Use TF_VAR
Environment Variables
- Define Variables in Terraform Configuration
- Set Environment Variables
- Run Terraform Commands
Example
Let’s walk through an example where we use environment variables to set values for Terraform variables.
1. Define Variables in Terraform Configuration
Create a variables.tf
file to define the variables.
variables.tf
:
variable "instance_type" {
description = "The type of instance to use"
type = string
}
variable "instance_count" {
description = "Number of instances to create"
type = number
}
variable "aws_region" {
description = "AWS region to deploy resources"
type = string
}
Create a main.tf
file to use these variables.
main.tf
:
provider "aws" {
region = var.aws_region
}
resource "aws_instance" "example" {
count = var.instance_count
ami = "ami-0abcdef1234567890"
instance_type = var.instance_type
tags = {
Name = "example-instance"
}
}
2. Set Environment Variables
Set the environment variables using the TF_VAR
prefix.
For Linux/macOS:
export TF_VAR_instance_type="t2.medium"
export TF_VAR_instance_count=2
export TF_VAR_aws_region="us-west-2"
For Windows Command Prompt:
set TF_VAR_instance_type=t2.medium
set TF_VAR_instance_count=2
set TF_VAR_aws_region=us-west-2
For Windows PowerShell:
$env:TF_VAR_instance_type="t2.medium"
$env:TF_VAR_instance_count=2
$env:TF_VAR_aws_region="us-west-2"
3. Run Terraform Commands
With the environment variables set, run the Terraform commands as usual.
terraform init –>terraform plan–>terraform apply
Summary
Using TF_VAR
environment variables is a powerful way to manage sensitive information and automate Terraform workflows. By setting environment variables with the TF_VAR
prefix, you can easily pass values to Terraform configurations without hardcoding them in your files.
Terraform variable using command line example
Steps to Use Command Line Variables
- Define Variables in Terraform Configuration
- Pass Variables via Command Line
- Run Terraform Commands
Example
Let’s walk through an example where we use command line arguments to set values for Terraform variables.
1. Define Variables in Terraform Configuration
Create a variables.tf
file to define the variables.
variables.tf
:
variable "instance_type" {
description = "The type of instance to use"
type = string
default = "t2.micro"
}
variable "instance_count" {
description = "Number of instances to create"
type = number
default = 1
}
variable "aws_region" {
description = "AWS region to deploy resources"
type = string
default = "us-east-1"
}
Create a main.tf
file to use these variables.
main.tf
:
provider "aws" {
region = var.aws_region
}
resource "aws_instance" "example" {
count = var.instance_count
ami = "ami-0abcdef1234567890"
instance_type = var.instance_type
tags = {
Name = "example-instance"
}
}
2. Pass Variables via Command Line
When running Terraform commands, you can pass variable values using the -var
flag.
Example Command Line Arguments:
terraform plan -var="instance_type=t2.medium" -var="instance_count=2" -var="aws_region=us-west-2"
Applying the Configuration:
terraform apply -var="instance_type=t2.medium" -var="instance_count=2" -var="aws_region=us-west-2"
Summary
Using the command line to set Terraform variables allows you to override default values on the fly, which is particularly useful for dynamic environments and testing. The -var
flag enables you to pass variable values directly when running Terraform commands.
Terraform Output Variable Tutorials with Example
Terraform output variables are used to display information about your infrastructure after a Terraform run. They can also be used to pass information to other configurations or to external systems. Output variables are defined using the output
block in your Terraform configuration.
Key Points About Output Variables
- Display Information: Output variables can display useful information like resource IDs, IP addresses, or other attributes.
- Pass Values: Output variables can pass values to other Terraform configurations or external systems.
- Dynamic Values: Output values are computed based on the final state of your infrastructure after applying the configuration.
Example of Output Variables
Let’s create an example where we define and use output variables to display information about EC2 instances.
Step-by-Step Guide
- Define Variables and Resources
- Define Output Variables
- Run Terraform Commands
1. Define Variables and Resources
Create a Terraform configuration with some input variables and AWS resources.
variables.tf
:
variable "instance_type" {
description = "The type of instance to use"
type = string
default = "t2.micro"
}
variable "instance_count" {
description = "Number of instances to create"
type = number
default = 1
}
variable "aws_region" {
description = "AWS region to deploy resources"
type = string
default = "us-west-2"
}
main.tf
:
provider "aws" {
region = var.aws_region
}
resource "aws_instance" "example" {
count = var.instance_count
ami = "ami-0abcdef1234567890"
instance_type = var.instance_type
tags = {
Name = "example-instance"
}
}
2. Define Output Variables
Add an outputs.tf
file to define the output variables.
outputs.tf
:
output "instance_ids" {
description = "The IDs of the EC2 instances"
value = aws_instance.example.*.id
}
output "instance_public_ips" {
description = "The public IP addresses of the EC2 instances"
value = aws_instance.example.*.public_ip
}
output "instance_private_ips" {
description = "The private IP addresses of the EC2 instances"
value = aws_instance.example.*.private_ip
}
Run Terraform Commands
terraform init –>terraform plan–>terraform apply
After applying the configuration, Terraform will display the output values:
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:
instance_ids = [
"i-1234567890abcdef0",
"i-0987654321abcdef0",
]
instance_public_ips = [
"54.123.45.67",
"52.123.45.67",
]
instance_private_ips = [
"10.0.1.123",
"10.0.1.124",
]
Summary
- Define Output Variables: Use the
output
block to define output variables. - Display Information: Output variables display information after a Terraform run.
- Pass Values: Output variables can pass values to other configurations or external systems.
- Usage: Output variables can be referenced in scripts, CI/CD pipelines, and other Terraform configurations.
Terraform Local Variable Tutorials with Example
In Terraform, local values (often referred to as “locals”) are used to simplify and manage expressions within a module. Local values are essentially temporary variables that can hold intermediate values, making your Terraform configurations easier to read and maintain. They are defined using the locals
block.
Key Points About Local Values
- Simplify Expressions: Local values help simplify complex expressions and calculations within your Terraform configuration.
- Reuse Values: You can define a local value once and reuse it multiple times in your configuration, reducing redundancy.
- Improve Readability: By using descriptive names for local values, you can make your Terraform configuration more readable and easier to understand.
Defining Local Values
Local values are defined using the locals
block, which contains one or more local value definitions.
Syntax:
locals {
local_name = expression
}
Example of Local Values
Let’s go through an example where we use local values to simplify a Terraform configuration.
Scenario
You have a Terraform configuration to create AWS EC2 instances and you want to simplify the management of tags and instance settings.
Original Configuration Without Local Values:
provider "aws" {
region = "us-west-2"
}
resource "aws_instance" "example" {
ami = "ami-0abcdef1234567890"
instance_type = "t2.micro"
tags = {
Name = "example-instance"
Environment = "dev"
}
}
resource "aws_instance" "example2" {
ami = "ami-0abcdef1234567890"
instance_type = "t2.micro"
tags = {
Name = "example-instance2"
Environment = "dev"
}
}
Simplified Configuration Using Local Values:
main.tf:
provider "aws" {
region = "us-west-2"
}
locals {
instance_ami = "ami-0abcdef1234567890"
instance_type = "t2.micro"
common_tags = {
Environment = "dev"
}
}
resource "aws_instance" "example" {
ami = local.instance_ami
instance_type = local.instance_type
tags = merge(local.common_tags, {
Name = "example-instance"
})
}
resource "aws_instance" "example2" {
ami = local.instance_ami
instance_type = local.instance_type
tags = merge(local.common_tags, {
Name = "example-instance2"
})
}
Explanation
- Local Values:
instance_ami
holds the AMI ID for the instances.instance_type
holds the instance type.common_tags
holds a map of common tags applied to all instances.
- Reuse Values:
- The
ami
andinstance_type
arguments in bothaws_instance
resources use the local values, ensuring consistency. - The
tags
argument uses themerge
function to combinecommon_tags
with instance-specific tags, reducing redundancy.
- The
More Complex Example with Calculations
Local values can also be used for more complex calculations and operations.
locals.tf:
locals {
base_cidr_block = "10.0.0.0/16"
public_subnet_cidr_blocks = [
cidrsubnet(local.base_cidr_block, 8, 0),
cidrsubnet(local.base_cidr_block, 8, 1)
]
private_subnet_cidr_blocks = [
cidrsubnet(local.base_cidr_block, 8, 2),
cidrsubnet(local.base_cidr_block, 8, 3)
]
region = "us-west-2"
}
In Terraform, local values (often referred to as “locals”) are used to simplify and manage expressions within a module. Local values are essentially temporary variables that can hold intermediate values, making your Terraform configurations easier to read and maintain. They are defined using the locals
block.
Key Points About Local Values
- Simplify Expressions: Local values help simplify complex expressions and calculations within your Terraform configuration.
- Reuse Values: You can define a local value once and reuse it multiple times in your configuration, reducing redundancy.
- Improve Readability: By using descriptive names for local values, you can make your Terraform configuration more readable and easier to understand.
Defining Local Values
Local values are defined using the locals
block, which contains one or more local value definitions.
Syntax:
hclCopy codelocals {
local_name = expression
}
Example of Local Values
Let’s go through an example where we use local values to simplify a Terraform configuration.
Scenario
You have a Terraform configuration to create AWS EC2 instances and you want to simplify the management of tags and instance settings.
Original Configuration Without Local Values:
hclCopy codeprovider "aws" {
region = "us-west-2"
}
resource "aws_instance" "example" {
ami = "ami-0abcdef1234567890"
instance_type = "t2.micro"
tags = {
Name = "example-instance"
Environment = "dev"
}
}
resource "aws_instance" "example2" {
ami = "ami-0abcdef1234567890"
instance_type = "t2.micro"
tags = {
Name = "example-instance2"
Environment = "dev"
}
}
Simplified Configuration Using Local Values:
main.tf:
hclCopy codeprovider "aws" {
region = "us-west-2"
}
locals {
instance_ami = "ami-0abcdef1234567890"
instance_type = "t2.micro"
common_tags = {
Environment = "dev"
}
}
resource "aws_instance" "example" {
ami = local.instance_ami
instance_type = local.instance_type
tags = merge(local.common_tags, {
Name = "example-instance"
})
}
resource "aws_instance" "example2" {
ami = local.instance_ami
instance_type = local.instance_type
tags = merge(local.common_tags, {
Name = "example-instance2"
})
}
Explanation
- Local Values:
instance_ami
holds the AMI ID for the instances.instance_type
holds the instance type.common_tags
holds a map of common tags applied to all instances.
- Reuse Values:
- The
ami
andinstance_type
arguments in bothaws_instance
resources use the local values, ensuring consistency. - The
tags
argument uses themerge
function to combinecommon_tags
with instance-specific tags, reducing redundancy.
- The
More Complex Example with Calculations
Local values can also be used for more complex calculations and operations.
locals.tf:
hclCopy codelocals {
base_cidr_block = "10.0.0.0/16"
public_subnet_cidr_blocks = [
cidrsubnet(local.base_cidr_block, 8, 0),
cidrsubnet(local.base_cidr_block, 8, 1)
]
private_subnet_cidr_blocks = [
cidrsubnet(local.base_cidr_block, 8, 2),
cidrsubnet(local.base_cidr_block, 8, 3)
]
region = "us-west-2"
}
main.tf:
provider "aws" {
region = local.region
}
resource "aws_vpc" "main" {
cidr_block = local.base_cidr_block
}
resource "aws_subnet" "public" {
count = length(local.public_subnet_cidr_blocks)
vpc_id = aws_vpc.main.id
cidr_block = element(local.public_subnet_cidr_blocks, count.index)
map_public_ip_on_launch = true
}
resource "aws_subnet" "private" {
count = length(local.private_subnet_cidr_blocks)
vpc_id = aws_vpc.main.id
cidr_block = element(local.private_subnet_cidr_blocks, count.index)
map_public_ip_on_launch = false
}
Explanation
- Local Values:
base_cidr_block
defines the base CIDR block for the VPC.public_subnet_cidr_blocks
calculates the CIDR blocks for public subnets using thecidrsubnet
function.private_subnet_cidr_blocks
calculates the CIDR blocks for private subnets.region
defines the AWS region.
- Use of Local Values:
- The VPC and subnets are created using the local values for CIDR blocks and region, ensuring consistency and reducing the need for hardcoded values.
Summary
- Local Values: Simplify complex expressions and manage reusable values within a module.
- Syntax: Defined using the
locals
block. - Examples: Local values can be used for common configurations, tags, and even complex calculations.
- Benefits: Improve readability, reduce redundancy, and ensure consistency in your Terraform configurations.
Example of terraform terraform.tfvars
The terraform.tfvars
file is used to define the values for the input variables declared in your Terraform configuration. It allows you to specify these values in a separate file, making your main configuration files cleaner and easier to manage. Terraform automatically loads this file to populate the variables when running commands like terraform plan
and terraform apply
.
Example Scenario
Suppose you have a Terraform configuration that defines a simple AWS infrastructure with input variables for instance type, instance count, and AWS region. Here’s how you can structure this configuration and use a terraform.tfvars
file to provide the variable values.
Step-by-Step Example
1. Define Variables in variables.tf
First, define your input variables in a variables.tf
file.
variables.tf
:
variable "instance_type" {
description = "The type of instance to use"
type = string
default = "t2.micro"
}
variable "instance_count" {
description = "Number of instances to create"
type = number
default = 1
}
variable "aws_region" {
description = "AWS region to deploy resources"
type = string
default = "us-west-2"
}
2. Use Variables in Your Main Configuration
Create a main.tf
file to use these variables in your AWS resource definitions.
main.tf
:
provider "aws" {
region = var.aws_region
}
resource "aws_instance" "example" {
count = var.instance_count
ami = "ami-0abcdef1234567890"
instance_type = var.instance_type
tags = {
Name = "example-instance"
}
}
3. Create the terraform.tfvars
File
Create a terraform.tfvars
file to specify the values for the input variables. This file will be automatically loaded by Terraform.
terraform.tfvars
:
instance_type = "t3.medium"
instance_count = 3
aws_region = "us-east-1"
Putting It All Together
The directory structure will look like this:
.
├── main.tf
├── terraform.tfvars
└── variables.tf
Running Terraform with terraform.tfvars
terraform plan
Plan: 3 to add, 0 to change, 0 to destroy.
Explanation
variables.tf
: Defines the input variables with optional default values.main.tf
: Uses the input variables to define AWS resources.terraform.tfvars
: Provides specific values for the input variables, overriding any default values.
By using terraform.tfvars
, you can easily manage and change the configuration values without modifying the main Terraform configuration files. This separation of configuration and values makes your Terraform code more modular and easier to manage, especially when working with multiple environments (e.g., dev, staging, prod) or different configurations.
Advanced Example with Multiple .tfvars
Files
You can also have multiple .tfvars
files for different environments or configurations. For example:
dev.tfvars
:
instance_type = "t2.micro"
instance_count = 2
aws_region = "us-west-1"
prod.tfvars
:
instance_type = "t3.large"
instance_count = 5
aws_region = "us-east-1"
Using Specific .tfvars
Files
To use a specific .tfvars
file, you can pass it using the -var-file
option:
For Development Environment:
terraform apply -var-file="dev.tfvars"
For Production Environment:
terraform apply -var-file="prod.tfvars"
Summary
terraform.tfvars
: A file to define variable values separately from the main configuration.- Automatic Loading: Terraform automatically loads
terraform.tfvars
if present in the working directory. - Multiple Environments: Use multiple
.tfvars
files for different environments and specify them using the-var-file
option.