How to Use Terraform with GitHub Actions - CICD Pipeline Guide
Set up Terraform CI-CD with GitHub Actions. Covers plan on PR, apply on merge, state locking, secrets management, and environment protection.
Terraform
Master Terraform variables with practical examples. Learn input, output, local, and environment variables for flexible infrastructure as code configurations.
Terraform variables are the foundation of reusable, maintainable infrastructure code. Whether you're managing a single environment or orchestrating multi-cloud deployments, understanding variables is essential.
This comprehensive guide covers every type of Terraform variable with real-world examples you can use immediately.
Input variables let you parameterize your Terraform configurations. They make your code reusable across environments.
variable "region" {
description = "AWS region for resources"
type = string
default = "us-east-1"
}
variable "instance_type" {
description = "EC2 instance type"
type = string
default = "t3.micro"
validation {
condition = contains(["t3.micro", "t3.small", "t3.medium"], var.instance_type)
error_message = "Instance type must be t3.micro, t3.small, or t3.medium."
}
}Terraform supports several variable types:
variable "tags" {
type = map(string)
default = {
Environment = "dev"
Project = "terraform-demo"
}
}
variable "availability_zones" {
type = list(string)
default = ["us-east-1a", "us-east-1b", "us-east-1c"]
}
variable "server_config" {
type = object({
name = string
instance_type = string
disk_size = number
monitoring = bool
})
}There are multiple ways to set variable values:
terraform apply -var="region=us-west-2"terraform.tfvars or *.auto.tfvarsexport TF_VAR_region=us-west-2region = "us-west-2"
instance_type = "t3.small"
tags = {
Environment = "production"
Team = "platform"
}terraform apply -var-file="environments/prod.tfvars"Output variables expose values from your Terraform configuration. They're essential for module composition and debugging.
output "instance_id" {
description = "ID of the EC2 instance"
value = aws_instance.web.id
}
output "public_ip" {
description = "Public IP address"
value = aws_instance.web.public_ip
}
output "database_password" {
description = "Database password"
value = aws_db_instance.main.password
sensitive = true
}Access outputs after apply:
terraform output instance_id
terraform output -jsonLocal variables simplify complex expressions and avoid repetition:
locals {
common_tags = {
Project = var.project_name
Environment = var.environment
ManagedBy = "terraform"
Owner = var.team
}
name_prefix = "${var.project_name}-${var.environment}"
is_production = var.environment == "production"
}
resource "aws_instance" "web" {
ami = var.ami_id
instance_type = local.is_production ? "t3.large" : "t3.micro"
tags = merge(local.common_tags, {
Name = "${local.name_prefix}-web"
})
}Add custom validation rules to catch errors early:
variable "environment" {
type = string
validation {
condition = contains(["dev", "staging", "production"], var.environment)
error_message = "Environment must be dev, staging, or production."
}
}
variable "cidr_block" {
type = string
validation {
condition = can(cidrhost(var.cidr_block, 0))
error_message = "Must be a valid CIDR block."
}
}Mark variables as sensitive to prevent them from appearing in logs:
variable "db_password" {
type = string
sensitive = true
}sensitive = trueTake your Terraform skills further with structured learning:
Terraform variables are powerful tools for creating flexible, reusable infrastructure code. By mastering input, output, local, and sensitive variables, you can build configurations that scale across teams and environments.
Start applying these patterns in your next Terraform project and see how much cleaner your code becomes.
Set up Terraform CI-CD with GitHub Actions. Covers plan on PR, apply on merge, state locking, secrets management, and environment protection.
Master Terraform workspaces for managing dev, staging, and production environments. Covers workspace commands, state isolation, and CI/CD integration.
Configure Terraform backends for remote state. Complete guide for S3 + DynamoDB, Azure Blob, GCS, Terraform Cloud, and Consul with encryption and locking.
Understand the Terraform state file, its purpose, and best practices for managing it. Learn remote state, locking, and recovery techniques for teams.