Tutorial 1: What is Infrastructure as Code with Terraform?
Learning Objectives
- Understand the concept of Infrastructure as Code (IaC)
- Learn why IaC is important for modern infrastructure management
- Understand how Terraform implements IaC principles
- Compare traditional infrastructure management vs IaC approach
What is Infrastructure as Code?
Infrastructure as Code (IaC) is the practice of managing and provisioning computing infrastructure through machine-readable definition files, rather than through physical hardware configuration or interactive configuration tools.
Key Principles of IaC
- Declarative Configuration: Define what you want, not how to achieve it
- Version Control: Infrastructure definitions stored in version control systems
- Automation: Automated provisioning and management of infrastructure
- Consistency: Identical environments across development, staging, and production
- Repeatability: Ability to recreate infrastructure reliably
Traditional Infrastructure Management Problems
Manual Configuration Issues
- Human Error: Manual processes are prone to mistakes
- Inconsistency: Environments drift over time
- Documentation: Hard to maintain accurate documentation
- Scalability: Manual processes don't scale
- Auditing: Difficult to track changes and compliance
Example Scenario
Imagine manually setting up a web application infrastructure:
- Log into cloud console
- Create VPC and subnets
- Configure security groups
- Launch EC2 instances
- Set up load balancer
- Configure database
- Set up monitoring
Problems:
- Takes hours or days
- Easy to miss steps
- Hard to replicate
- No audit trail
- Manual rollback if issues occur
How Terraform Solves These Problems
Terraform's Approach
Terraform uses a declarative language (HCL - HashiCorp Configuration Language) to describe infrastructure in code files.
Benefits of Terraform IaC
- Predictable: Same configuration produces same infrastructure
- Collaborative: Teams can review infrastructure changes
- Versionable: Track infrastructure changes over time
- Testable: Test infrastructure changes before applying
- Documented: Code serves as living documentation
Simple Terraform Example
# Configure the AWS Provider
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
# Configure the AWS Provider
provider "aws" {
region = "us-west-2"
}
# Create a VPC
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "main-vpc"
}
}
# Create an EC2 instance
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1d0"
instance_type = "t2.micro"
tags = {
Name = "web-server"
}
}
IaC Benefits in Practice
Development Workflow
- Plan: See what changes will be made before applying
- Apply: Execute the planned changes
- Review: Code review process for infrastructure changes
- Rollback: Easy rollback to previous configurations
Team Collaboration
- Code Reviews: Infrastructure changes go through peer review
- Documentation: Code documents the infrastructure
- Knowledge Sharing: Team members understand infrastructure through code
- Onboarding: New team members can understand infrastructure quickly
Compliance and Auditing
- Change Tracking: Every change is tracked in version control
- Approval Process: Changes can require approval before deployment
- Compliance: Ensure infrastructure meets compliance requirements
- Rollback: Quick rollback to compliant configurations
Key Terraform Concepts
Resources
Basic building blocks of Terraform configuration. Examples:
aws_instance
- EC2 instanceaws_vpc
- Virtual Private Cloudgoogle_compute_instance
- Google Cloud VM
Providers
Plugins that enable Terraform to interact with cloud platforms and services:
- AWS Provider
- Azure Provider
- Google Cloud Provider
- Kubernetes Provider
State
Terraform maintains state of your infrastructure in a state file:
- Tracks resource mappings
- Enables change detection
- Supports team collaboration
Configuration Files
.tf
files contain resource definitionsterraform.tfvars
contains variable valuesterraform.tfstate
contains current state
Getting Started Checklist
Prerequisites
- Cloud account (AWS, Azure, or GCP)
- Terraform installed on local machine
- Text editor or IDE
- Basic understanding of cloud services
First Steps
- Install Terraform
- Configure cloud provider credentials
- Create first configuration file
- Run
terraform init
- Run
terraform plan
- Run
terraform apply
Common Use Cases
Development Environments
- Spin up identical development environments
- Test infrastructure changes safely
- Clean up resources automatically
Production Infrastructure
- Manage production infrastructure consistently
- Track all changes through version control
- Implement approval workflows
Multi-Cloud Deployments
- Manage resources across multiple cloud providers
- Consistent configuration syntax
- Unified workflow
Best Practices Introduction
Organization
- Use meaningful resource names
- Group related resources logically
- Use consistent naming conventions
Version Control
- Store all configuration in version control
- Use branching strategies for changes
- Tag releases for rollback capability
Security
- Never hardcode sensitive values
- Use variables for configuration
- Implement least privilege access
Next Steps
After understanding IaC concepts, the next tutorial will cover installing and configuring Terraform on your local machine.
Key Takeaways
- Infrastructure as Code eliminates manual configuration errors
- Terraform provides declarative approach to infrastructure management
- Code-based infrastructure enables collaboration and version control
- IaC is essential for modern, scalable infrastructure management