Terraform - Campus-Castolo/m300 GitHub Wiki


🔹 What is Terraform?

Terraform is an Infrastructure as Code (IaC) tool developed by HashiCorp. It allows you to define, manage, and provision cloud infrastructure using configuration files written in a simple, human-readable language called HCL (HashiCorp Configuration Language).


🔹 What is Terraform Used For?

Terraform is used to:

Provision Infrastructure: You can automatically create and manage servers, databases, networks, storage, etc.

Automate Changes: Easily update and scale infrastructure without manually adjusting resources.

Track Infrastructure State: Terraform maintains a state file that tracks all deployed resources so it knows what’s running.

Multi-Cloud Support: Terraform works with AWS, Azure, GCP, and even on-premises services.

Reusable Infrastructure: You can create modules (reusable infrastructure components) to maintain consistency across projects.


🔹 How Can Terraform Be Used with AWS?

Terraform has a provider specifically for AWS (provider "aws"), making it easy to define AWS resources in your configuration files.

Some common AWS use cases with Terraform:

1️⃣ Provisioning an EC2 Instance

provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "web_server" { ami = "ami-0c55b159cbfafe1f0" # Example AMI instance_type = "t2.micro"

tags = { Name = "WebServer" } }

This simple script creates an EC2 instance in us-east-1.


2️⃣ Setting Up an S3 Bucket

resource "aws_s3_bucket" "example" {
  bucket = "my-terraform-s3-bucket"

tags = { Name = "MyTerraformBucket" Environment = "Dev" } }

This creates an S3 bucket with tags.


3️⃣ Deploying a VPC with Subnets

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"

tags = { Name = "MainVPC" } }

resource "aws_subnet" "subnet_a" { vpc_id = aws_vpc.main.id cidr_block = "10.0.1.0/24" }

This defines a VPC with a subnet, ready for further configuration.


4️⃣ Automating Full Application Infrastructure

You can combine EC2, RDS, Load Balancers, Security Groups, and even IAM Roles into a single Terraform configuration, making it easy to deploy an entire app infrastructure.


🔹 Key Benefits in AWS

  • Automate Everything: No need to click in the AWS console.
  • Version Control: Store infrastructure code in Git.
  • Consistency: Reuse configurations across dev/staging/production.
  • Audibility: Track changes to infrastructure easily.
  • Multi-Region & Multi-Account: Easily deploy in different AWS accounts or regions.

🔹 Example Workflow

  1. Write the configuration (e.g., EC2, RDS, VPC).
  2. Run terraform init (to download AWS provider).
  3. Run terraform plan (preview changes).
  4. Run terraform apply (create/update resources).

🔹 In Short

Feature Description
Tool Type Infrastructure as Code (IaC)
Language HashiCorp Configuration Language (HCL)
Cloud Support Multi-cloud (AWS, Azure, GCP, etc.)
State Management Tracks deployed resources in a state file

last revised 04.03.2025: renamed page
⚠️ **GitHub.com Fallback** ⚠️