Terraform - Campus-Castolo/m300 GitHub Wiki
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).
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.
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:
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.
resource "aws_s3_bucket" "example" {
bucket = "my-terraform-s3-bucket"
tags = {
Name = "MyTerraformBucket"
Environment = "Dev"
}
}
This creates an S3 bucket with tags.
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.
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.
- 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.
- Write the configuration (e.g., EC2, RDS, VPC).
- Run
terraform init
(to download AWS provider). - Run
terraform plan
(preview changes). - Run
terraform apply
(create/update resources).
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 |