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 outputs for sharing data between modules and stacks. Covers output types, sensitive outputs, remote state data sources, and cross-stack...
Outputs expose values from your Terraform configuration — print them in the terminal, pass them between modules, or share them across projects via remote state data sources. Mark sensitive outputs with sensitive = true.
output "vpc_id" {
value = aws_vpc.main.id
description = "ID of the VPC"
}
output "public_ip" {
value = aws_instance.web.public_ip
description = "Public IP of the web server"
}
output "db_endpoint" {
value = aws_db_instance.main.endpoint
description = "RDS endpoint"
sensitive = true
}# View outputs
terraform output
terraform output vpc_id
terraform output -json
terraform output -raw public_ip # No quotes, useful in scriptsoutput "vpc_id" {
value = aws_vpc.main.id
}
output "private_subnet_ids" {
value = aws_subnet.private[*].id
}
output "public_subnet_ids" {
value = aws_subnet.public[*].id
}module "networking" {
source = "./modules/networking"
vpc_cidr = "10.0.0.0/16"
}
module "compute" {
source = "./modules/compute"
vpc_id = module.networking.vpc_id # Use module output
subnet_ids = module.networking.private_subnet_ids # Use module output
}
# Expose module outputs at root level
output "vpc_id" {
value = module.networking.vpc_id
}# Project B reads outputs from Project A's state
data "terraform_remote_state" "networking" {
backend = "s3"
config = {
bucket = "my-terraform-state"
key = "prod/networking/terraform.tfstate"
region = "us-east-1"
}
}
resource "aws_instance" "web" {
subnet_id = data.terraform_remote_state.networking.outputs.private_subnet_ids[0]
}output "db_password" {
value = random_password.db.result
sensitive = true # Hidden in terminal, visible in state file
}
# Access sensitive outputs explicitly
# terraform output -json db_password
# terraform output -raw db_password# String
output "region" { value = var.region }
# List
output "subnet_ids" { value = aws_subnet.private[*].id }
# Map
output "endpoints" {
value = {
web = aws_instance.web.public_ip
api = aws_instance.api.private_ip
db = aws_db_instance.main.endpoint
}
}
# Computed
output "connection_string" {
value = "postgresql://${var.db_user}:${random_password.db.result}@${aws_db_instance.main.endpoint}/mydb"
sensitive = true
}description to every output — documents what the value is forsensitive — prevents accidental loggingvpc_id not the_vpc_identifierOutputs are how Terraform modules communicate. Use them to share IDs, endpoints, and config between modules, expose important values in the terminal, and connect projects via remote state. Always add descriptions and mark sensitive values.
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.
Understand the Terraform required_providers block for version pinning. Covers source addresses, version constraints, lock files, and multi-provider configs.
Master Terraform string functions with practical examples. Covers format, join, split, replace, regex, trim, lower, upper, and template rendering.