Terraform Detailed Notes
Terraform - Detailed Notes
1. Introduction
Terraform is an open-source infrastructure as code (IaC) tool created by HashiCorp that enables users to
define and provision infrastructure using a high-level configuration language known as HCL (HashiCorp
Configuration Language). It allows you to manage cloud, on-premises, and SaaS infrastructure in a
consistent way.
2. Why Use Terraform?
- Platform agnostic: works with AWS, Azure, GCP, etc.
- Infrastructure versioning and reproducibility
- Dependency management and graph-based execution
3. Key Concepts
- Providers: Interface to cloud platforms (e.g., aws, azurerm, google)
- Resources: Individual components (e.g., aws_instance, aws_s3_bucket)
- Variables: Inputs for dynamic configuration
- Outputs: Exported values post-deployment
- Modules: Reusable configurations
- State: Tracks current infrastructure
4. Terraform Workflow
- Write: Define resources in .tf files
- Init: terraform init
Terraform Detailed Notes
- Plan: terraform plan
- Apply: terraform apply
- Destroy: terraform destroy
5. Sample Code
provider "aws" {
region = "us-west-2"
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
6. Remote State Example
terraform {
backend "s3" {
bucket = "my-tfstate"
key = "dev/terraform.tfstate"
region = "us-west-2"
7. Best Practices
Terraform Detailed Notes
- Use remote backend with locking
- Use variables and modules for reusability
- Maintain state securely
- Validate code with terraform validate and fmt