Terraform
By: Aryan Gadhavi | DevOps Enthusiast
CONTENTS
1. Introduction to Terraform
2. Benefits
3. Terraform Key Concepts
4. Terraform vs Iac Tools
5. Advanced Features
6. Real-World Use Cases
7. Project
What is Terraform?
Infrastructure as Code Tool
1 Terraform automates infrastructure provisioning using
declarative configuration files.
Multi-Cloud Support
2 Compatible with AWS, Azure, GCP, and hybrid cloud
environments for consistent deployments.
Open Source By HashiCorp
3 A free, extensible tool designed for modern DevOps
workflows and infrastructure management.
State Management
4 Tracks resource changes, ensuring efficient updates and
preventing drift in infrastructure configurations.
Modular and Scalable
5 Enables reusable modules to standardize infrastructure while
scaling resources effortlessly across environments.
Infrastructure as a Code
Definition
1 Infrastructure as Code uses code to provision and manage
infrastructure, ensuring automation and consistency.
Automation
2 Automates infrastructure tasks, reducing manual work and errors
for faster, reliable deployments.
Consistency
3 Ensures identical environments, preventing configuration
drift between development, staging, and production setups.
Version Control
4 Tracks infrastructure changes with tools like Git,
enabling collaboration and easy rollbacks.
Scalability
5 Quickly adjusts infrastructure to meet fluctuating demands,
ensuring seamless resource scaling.
01 Multi Cloud Support
Works seamlessly across AWS, Azure,
GCP, and on-premises.
02 Declarative Configuration
Define desired infrastructure without
worrying about procedural steps.
BENEFITS
03 Infrastructure Versioning
Track, manage, and revert infrastructure
changes like code.
04 Reusable Modules
Standardize and simplify resource
creation across projects and teams.
Terraform Key Concepts
Plugins State
Plugins to interact with Tracks infrastructure status,
cloud services and APIs. enabling updates and avoiding
configuration drift.
Resources Previews
Fundamental building Previews changes before
blocks representing applying them to avoid
infrastructure unexpected modifications.
components like servers
or databases.
Modules Terraform Registry
Reusable configurations Centralized repository for
that simplify and community and
standardize infrastructure HashiCorp-provided
setups. modules.
Terraform vs other Iac Tools
State Management
Efficiently tracks infrastructure state,
offering advantages over non-stateful
tools like Ansible.
Cloud Agnostic Declarative Syntax
Terraform supports a wide range of cloud providers, Terraform declarative configuration simplifies resource
while others like CloudFormation are limited to a management, unlike imperative tools like Chef and
specific cloud platform. Puppet, which require detailed step-by-step instructions.
Dependency Graph Modular Reusability
Terraform automatically resolves Terraform Modular design encourages reusable
dependencies, making resource configurations across multiple projects, providing
management more efficient, unlike scalability and flexibility beyond what other tools
other tools that require manual typically offer.
configuration for resource order. Extensive Providers
Terraform supports a wide array of providers, offering
more flexibility than cloud-specific tools like AWS
CloudFormation or Azure Resource Manager.
Terraform Commands
terraform init terraform output
Initializes configuration, Displays values from
downloads provider
03 04 the state file outputs.
plugins.
terraform plan terraform validate
02 05 Checks configuration for
Previews changes without
applying them. syntax or logic errors.
terraform apply terraform destroy
01 06
Creates or updates resources Removes all managed
in infrastructure. infrastructure resources.
Terraform Workflow
1. Write Configuration
6.Destroy (terraform
Define infrastructure as destroy)
code using HCL, Safely deprovision and
specifying resources and clean up all resources
their desired state. when they are no longer
needed.
2. Initialize (terraform
init) 5.State Management
Prepare the working Track and manage
directory by downloading resource states using
necessary providers and Terraform state file for
setting up the environment. synchronization and
3.Plan (terraform plan) 4.Apply (terraform apply)
idempotency.
Preview the changes Terraform will Execute the plan to provision, update,
make to achieve the desired or delete infrastructure resources as
infrastructure state. defined.
Advanced features
Workspaces Modules
Isolate environments like
development and production
1 6 Reuse standardized
within a single configuration for infrastructure components,
better management. ensuring consistency and easier
management across projects.
Remote Backend Data Sources
Store state remotely (e.g., AWS 2 5 Fetch external data from APIs
S3) for collaboration and version
control in teams. or cloud providers, enabling
flexible infrastructure
configurations.
Dynamic Blocks
Create complex configurations
3 4 Meta Arguments
Control resource behavior using
dynamically, reducing repetitive arguments like count, for_each,
code in infrastructure setup. depends_on, and lifecycle
State locking
Definition
State locking prevents simultaneous modifications
to the Terraform state file, ensuring operations are
consistent and avoiding conflicts during resource
updates.
Importance
It ensures multiple users or processes do not
corrupt the state file by making changes
simultaneously during Terraform operations.
Implementation
Remote backends like AWS S3 with DynamoDB or
Terraform Cloud support automatic state locking for
secure collaboration.
Real World Use Case?
Multi-Cloud Infrastructure Disaster Recovery
Management and High Availability
Replicate infrastructure across
Seamlessly manage and provision
resources across AWS, Azure, GCP, 1. 2. multiple regions or cloud
providers to ensure business
and other cloud platforms, ensuring
continuity, fault tolerance, and
consistent infrastructure
minimize downtime during
management.
failures.
Network Infrastructure
CI/CD Pipeline Automation Automation
Efficiently automate network
Automate the provisioning of
development, testing, and
4. 3. setups, including VPCs, subnets,
and security groups, ensuring
production environments,
secure, scalable, and consistent
ensuring streamlined CI/CD
networking in cloud
workflows for faster
environments.
deployments.
Project: Automated Nginx Web Server
Deployment on AWS using Terraform AWS Provider- Key Pair
Configuration Resource
1 Sets AWS region
and credentials
4 Manages public
key for secure SSH
instance access.
for access.
Security Group
Configuration
EC2 Instance
5 Allows inbound
SSH traffic from
all IP addresses.
Resource
2 Provisions an
Ubuntu EC2 instance
with Nginx.
Tagging
Instances
6 Adds descriptive
tags for easy
instance
identification.
User Data Output Public
Script IP
3 Automates Nginx
installation and
startup on
7 Displays the EC2
instance public IP
after provisioning.
instance boot.