CLOUD
Guides

Terraform: Infrastructure Automation as Code

CLOUD|April 19, 2026|3 min read|246 views
Terraform: Infrastructure Automation as Code

Servers + networks + DNS = one code file. Terraform builds and destroys your infrastructure with a command. We explain basics and getting started.

Infrastructure Automation with Terraform

Terraform transforms infrastructure from a manual task into code. Instead of clicking through a control panel to create each server, you write a text file and Terraform creates everything automatically — and you can recreate it at any time with identical configuration.

The Principle: Infrastructure as Code

The old problem:
├── Developer A creates a server one way
├── Developer B creates a server differently
├── No one remembers exact settings
└── Start from scratch every time

With Terraform:
├── One file describes the entire infrastructure
├── git commit = complete change history
├── terraform apply = create a matching environment in minutes
└── Works with any cloud provider

Core Concepts

Providers, Resources, State

# Provider - tells Terraform which cloud to use
terraform {
 required_providers {
 aws = {
 source = "hashicorp/aws"
 version = "~> 5.0"
 }
 }
}

# Resource - the resources you want to create
resource "aws_instance" "web_server" {
 ami = "ami-0c55b159cbfafe1f0"
 instance_type = "t3.medium"
 
 tags = {
 Name = "web-server"
 Environment = "production"
 }
}

Practical Example: Complete Web Environment

# Variables
variable "server_count" {
 description = "Number of app servers"
 type = number
 default = 2
}

# App Servers
resource "aws_instance" "app" {
 count = var.server_count
 ami = "ami-0c55b159cbfafe1f0"
 instance_type = "t3.xlarge"
 
 user_data = <<-EOF
 #!/bin/bash
 apt update
 apt install -y nginx php8.3-fpm
 systemctl start nginx php8.3-fpm
 EOF
 
 tags = {
 Name = "app-${count.index + 1}"
 }
}

Core Commands

# Initialize Terraform (download providers)
terraform init

# Preview changes before applying
terraform plan

# Apply changes
terraform apply

# Remove all resources (dangerous — dev environment only)
terraform destroy

# Show current infrastructure state
terraform show

Managing Variables and Secrets

variable "db_password" {
 description = "Database password"
 type = string
 sensitive = true # Hidden in logs
}
# Pass values via .tfvars file (do not commit to git)
db_password = "SuperSecretPassword123!"
api_key = "your-provider-api-key"

# Or via Environment Variables
export TF_VAR_db_password="SuperSecretPassword123!"
terraform apply

Modules: Reusing Code

# modules/web-server/main.tf
resource "aws_instance" "this" {
 ami = var.ami_id
 instance_type = var.instance_type
 tags = { Name = var.server_name }
}

output "public_ip" {
 value = aws_instance.this.public_ip
}
# main.tf — using the module
module "web_server_1" {
 source = "./modules/web-server"
 server_name = "web-1"
 instance_type = "t3.large"
 ami_id = "ami-0c55b159cbfafe1f0"
}

Conclusion

Terraform turns "how did we create this server?" from an unanswerable question into a clear text file in git. كلاود (cloud.net.sa) provides APIs and resources manageable via Infrastructure as Code tools — contact us for integration details.


Get Started

Ready to build your project? Configure your cloud server with the specs your app needs, or view plans and pricing.

Share