Skip to content

Latest commit

 

History

History
102 lines (77 loc) · 1.96 KB

File metadata and controls

102 lines (77 loc) · 1.96 KB

Terraform Guide

This directory contains Infrastructure as Code (IaC) configurations using Terraform.

Structure

terraform/
├── main.tf              # Main configuration
├── variables.tf         # Variable definitions
├── outputs.tf          # Output definitions
├── providers.tf        # Provider configurations
├── terraform.tfvars.example  # Example variables
└── modules/            # Reusable modules
    ├── aws/
    │   └── vpc/
    └── azure/
        └── resource_group/

Getting Started

  1. Initialize Terraform:

    cd terraform
    terraform init
  2. Copy and configure variables:

    cp terraform.tfvars.example terraform.tfvars
    # Edit terraform.tfvars with your values
  3. Plan your infrastructure:

    terraform plan
  4. Apply changes:

    terraform apply

Environment Management

Use Terraform workspaces for different environments:

# Create workspace
terraform workspace new dev
terraform workspace new staging
terraform workspace new prod

# Switch workspace
terraform workspace select dev

Best Practices

  • Always run terraform plan before apply
  • Use version constraints for providers
  • Store state files remotely (S3, Azure Storage)
  • Use modules for reusable components
  • Tag all resources consistently
  • Never commit .tfvars files with secrets

Module Usage

Modules are located in the modules/ directory. Example usage:

module "vpc" {
  source = "./modules/aws/vpc"
  
  project_name = var.project_name
  environment  = var.environment
  cidr_block   = "10.0.0.0/16"
}

Common Commands

# Format code
terraform fmt

# Validate configuration
terraform validate

# Show current state
terraform show

# List resources
terraform state list

# Import existing resource
terraform import aws_instance.example i-1234567890abcdef0

# Destroy infrastructure
terraform destroy