📜 Table of Contents
Terraform IaC Infrastructure as Code
Master Infrastructure as Code with Terraform. Create, modify, and version your infrastructure securely and efficiently across multiple cloud providers.
🏗️ What is Terraform?
Terraform is an open-source tool for Infrastructure as Code (IaC), developed by HashiCorp. It allows users to define and provision infrastructure resources declaratively using the HashiCorp Configuration Language (HCL) or optionally JSON.
🌐 Multi-Cloud
Supports AWS, Azure, GCP, etc.
🔄 Declarative
Describe the desired state
🛠️ Plan & Apply
Safe execution plan
With Terraform, you can manage complex infrastructures across different providers. It separates the planning phase (what needs to be done) from the application phase (actual execution), allowing you to review and approve changes before they are applied.
🧩 Core Concepts
Terraform is based on several key concepts that form the foundation for managing your infrastructure.
🔌 Provider
Providers are plugins that Terraform uses to interact with cloud providers, SaaS providers, and other APIs. Each provider adds a set of resource types and/or data sources that Terraform can manage. Examples include AWS, Azure, Google Cloud, Kubernetes, Docker, etc.
# Example: AWS Provider Configuration
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = "eu-central-1"
}
🧱 Resources
Resources are the most basic elements in Terraform. Each resource describes one or more infrastructure objects, such as virtual machines, networks, or database instances.
# Example: AWS EC2 Instance Resource
resource "aws_instance" "web_server" {
ami = "ami-0c55b31ad2c455052" # Example AMI ID
instance_type = "t2.micro"
tags = {
Name = "HelloWorld"
}
}
🔩 Variables & Outputs
Input variables serve as parameters for a Terraform module, allowing aspects of the configuration to be customized without changing the module's source code. Output values are like return values for a Terraform module.
# Example: Variable Definition
variable "instance_type" {
description = "The type of EC2 instance"
type = string
default = "t2.micro"
}
# Example: Output Definition
output "instance_ip_addr" {
value = aws_instance.web_server.public_ip
}
📦 Modules
Modules are containers for multiple resources used together. A module is a collection of .tf and/or .tf.json files stored in a directory. Modules are the main way to package and reuse Terraform configurations.
💾 State Management
Terraform stores the state of your managed infrastructure and configuration. This state is used to map real resources to your configuration, track metadata, and improve performance for large infrastructures. By default, the state is stored locally in a file called \`terraform.tfstate\`, but for teamwork and better management, it can be stored in a remote backend (e.g., AWS S3, Terraform Cloud).
⚙️ Terraform Workflow
The core workflow of Terraform consists of three main steps:
- Init: Initializes the working directory, downloads provider plugins, and configures the backend. (`terraform init`)
- Plan: Creates an execution plan. Terraform determines which actions are required to achieve the desired state. (`terraform plan`)
- Apply: Applies the changes described in the plan to create, update, or delete the infrastructure. (`terraform apply`)
- Destroy: Destroys all resources managed by Terraform. (`terraform destroy`)
This cycle of writing, planning, and applying is the fundamental process when using Terraform.
🌟 Best Practices
- Use remote state backends for teamwork and security.
- Structure your code with modules for reusability and readability.
- Version your Terraform code with Git or a similar system.
- Use variables for configurable values and avoid hardcoding.
- Format your code consistently with `terraform fmt`.
- Validate your configuration with `terraform validate`.
- Implement a "least privilege" strategy for provider credentials.
- Always plan changes before you apply them and check the plan carefully.
☁️ Terraform Cloud & Enterprise
HashiCorp offers Terraform Cloud as a hosted service that facilitates collaboration, governance, and state management. For larger organizations, there is Terraform Enterprise, a self-hosted version with additional features.
🤝 Collaboration
Shared workspaces, versioning.
🛡️ Governance
Policy as Code (Sentinel), cost control.