Terraform Basics: Getting Started with Infrastructure as Code
Getting Started with Terraform
Infrastructure as Code (IaC) has revolutionized how we manage cloud resources, and Terraform is at the forefront of this revolution. In this first post of our Terraform series, we’ll cover the essentials you need to start your IaC journey.
What is Terraform?
Terraform is an open-source IaC tool that lets you define and provide data center infrastructure using a declarative configuration language. Unlike imperative programming, where you specify how to do something, Terraform’s declarative approach lets you specify what you want, and it figures out how to achieve it.
Installation and Setup
- Install Terraform:
# macOS with Homebrew brew install terraform # Windows with Chocolatey choco install terraform # Verify installation terraform version
- Configure your cloud provider credentials (AWS example):
export AWS_ACCESS_KEY_ID="your_access_key" export AWS_SECRET_ACCESS_KEY="your_secret_key" export AWS_REGION="us-west-2"
Basic Concepts
1. Provider
The provider is the plugin Terraform uses to talk to a specific platform (AWS, Azure, GCP, etc.):
provider "aws" {
region = "us-west-2"
}
2. Resource
Resources are the infrastructure components you want to create:
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "example-instance"
}
}
3. Your First Configuration
Create a file named main.tf
:
# 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"
}
}
# Create a subnet
resource "aws_subnet" "main" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
tags = {
Name = "main"
}
}
Basic Workflow
- Initialize Terraform:
terraform init
- See what changes will be made:
terraform plan
- Apply the changes:
terraform apply
- When you’re done, clean up:
terraform destroy
State
Terraform keeps track of your infrastructure in a state file (terraform.tfstate
). This file maps your configuration to real-world resources. Never edit this file manually!
Key Points to Remember
- Always run
terraform plan
beforeterraform apply
- Keep your state file safe and backed up
- Use version control for your Terraform configurations
- Follow naming conventions for resources
Next Steps
In the next post, we’ll dive into variables, outputs, and data sources to make our configurations more flexible and reusable.
Stay tuned for more in this Terraform series!