Skip to content

Latest commit

 

History

History
518 lines (370 loc) · 9.78 KB

File metadata and controls

518 lines (370 loc) · 9.78 KB

🔄 Setting Up Version Control for Terraform Projects

1. 🌍 Why Use Version Control?

Using version control for Terraform projects offers several benefits:

  • Collaboration: Multiple team members can work on the same codebase.
  • History: Track changes and revert to previous states if needed.
  • Backup: Safeguard your configuration files.
  • Audit Trail: Maintain a complete history of infrastructure changes.
  • Code Review: Enable peer review through pull requests.
  • CI/CD Integration: Automate testing and deployment.

2. 📦 Choosing a Version Control System

The most commonly used version control system is Git. We'll focus on setting up Git for your Terraform projects.

3. 🏗️ Setting Up Git

Step 1: 🔄 Install Git

  1. For macOS:

    brew install git
  2. For Windows: Download and install Git from the official website.

  3. For Linux:

    sudo apt-get update
    sudo apt-get install git

Step 2: 🛠️ Configure Git

  1. Set your username:

    git config --global user.name "Your Name"
  2. Set your email:

    git config --global user.email "your.email@example.com"
  3. Set default branch name to main:

    git config --global init.defaultBranch main
  4. Enable colored output:

    git config --global color.ui auto
  5. Set line ending handling:

    git config --global core.autocrlf input  # macOS/Linux
    git config --global core.autocrlf true   # Windows

4. 🗂️ Initialize a Git Repository

Step 1: 📁 Create a New Directory

  1. Create a directory for your Terraform project:

    mkdir terraform-project
    cd terraform-project

Step 2: 🔄 Initialize Git

  1. Initialize a new Git repository:

    git init
  2. Verify the repository initialization:

    git status

5. 📝 Create a .gitignore File

Why?

A .gitignore file specifies which files and directories to ignore in your Git repository. This helps avoid committing sensitive information and unnecessary files.

How?

  1. Create a .gitignore file:

    touch .gitignore
  2. Add the following content to your .gitignore file:

    # Local .terraform directories
    **/.terraform/*
    !**/.terraform/modules/**
    !**/.terraform/providers/**
    
    # .tfstate files
    *.tfstate
    *.tfstate.*
    *.tfstate.backup
    *.tfstate.lock.info
    
    # Crash log files
    crash.log
    crash.*.log
    *.crash
    *.crash.*
    
    # Exclude any .tfvars files containing sensitive data
    *.tfvars
    *.tfvars.json
    !example.tfvars
    !example.tfvars.json
    
    # Terraform .plan files
    *.tfplan
    *.tfplan.*
    
    # .terraform.lock.hcl file
    .terraform.lock.hcl
    
    # Ignore any directories or files generated by Terraform
    .terraformrc
    terraform.rc
    .tfplan
    
    # IDE and editor files
    .idea/
    .vscode/
    *.swp
    *.swo
    *~
    .DS_Store
    
    # OS files
    Thumbs.db
    
    # Override files (for local development)
    override.tf
    override.tf.json
    *_override.tf
    *_override.tf.json
    
    # Temporary files
    *.tmp
    *.temp
    .tmp/
    
    # Log files
    *.log
    
    # Environment files
    .env
    .env.local
    .env.*.local
    
    # Backup files
    *.bak
    *.backup

6. 🌐 Adding and Committing Files

Step 1: ➕ Add Files to Git

  1. Add all files to the staging area:

    git add .
  2. Verify files in the staging area:

    git status

Step 2: 📝 Commit Files

  1. Commit the files with a message:

    git commit -m "Initial commit of Terraform project"
  2. Verify the commit:

    git log

7. 🌍 Using a Remote Repository

Step 1: 🔄 Create a Remote Repository

  1. Create a new repository on a platform like GitHub, GitLab, or Bitbucket.
  2. Copy the repository URL.

Step 2: 🔗 Link Remote Repository

  1. Add the remote repository:

    git remote add origin https://github.com/your-username/your-repository.git
  2. Verify the remote repository:

    git remote -v

Step 3: 🚀 Push to Remote Repository

  1. Push the local repository to the remote repository:

    git push -u origin master

8. 🛠️ Working with Branches

Step 1: 🌿 Create a New Branch

  1. Create and switch to a new branch:

    git checkout -b feature-branch

    Or using the newer syntax:

    git switch -c feature-branch
  2. Verify the current branch:

    git branch

Step 2: 🔄 Merge Branches

  1. Switch to the main branch:

    git checkout main

    Or using the newer syntax:

    git switch main
  2. Merge the feature branch:

    git merge feature-branch
  3. Delete the feature branch (optional):

    git branch -d feature-branch

Step 3: 🔄 Using Pull/Merge Requests

For GitHub:

  1. Push branch to remote:
    git push -u origin feature-branch
  2. Create pull request on GitHub website
  3. Request code review from team members
  4. Merge after approval using merge commit, squash, or rebase

For GitLab:

  1. Push branch to remote:
    git push -u origin feature-branch
  2. Create merge request on GitLab website
  3. Configure merge request settings (squash commits, delete source branch)
  4. Merge after approval

Step 4: 🔄 Rebase vs Merge

Merge (preserves history):

git checkout main
git pull origin main
git checkout feature-branch
git rebase main
git checkout main
git merge feature-branch

Sash merge (clean history):

git checkout main
git merge --squash feature-branch
git commit -m "Add feature description"

9. 📦 Using Tags

Step 1: 🔖 Create a Tag

  1. Create a new tag:

    git tag -a v1.0 -m "Version 1.0"
  2. Verify the tag:

    git tag

Step 2: 🚀 Push Tags to Remote

  1. Push the tags to the remote repository:

    git push origin --tags

10. �️ Branch Protection Rules

Step 1: Configure Branch Protection on GitHub

  1. Navigate to repository settings → Branches
  2. Add branch protection rule for main branch:
    • Require pull request reviews before merging
    • Require status checks to pass before merging
    • Require branches to be up to date before merging
    • Limit who can push to matching branches
    • Include administrators

Step 2: Configure Branch Protection on GitLab

  1. Navigate to project settings → Repository → Protected branches
  2. Protect the main branch:
    • Allowed to merge: Maintainers
    • Allowed to push: No one
    • Require approval from code owners

Step 3: Configure Branch Protection on Bitbucket

  1. Navigate to repository settings → Branch restrictions
  2. Add restriction for main branch:
    • Limit merge access to specific users/groups
    • Require pull request approvals

11. 🔧 Pre-commit Hooks for Terraform

Step 1: Install Pre-commit

# macOS/Linux
brew install pre-commit

# Or using pip
pip install pre-commit

Step 2: Create Pre-commit Configuration

Create .pre-commit-config.yaml:

repos:
  - repo: https://github.com/antonbabenko/pre-commit-terraform
    rev: v1.83.0
    hooks:
      - id: terraform_fmt
      - id: terraform_validate
      - id: terraform_tflint
        args:
          - '--args=--module'
      - id: terraform_tfsec
      - id: terraform_checkov
      - id: terraform_docs
      - id: terraform_providers_lock
      - id: terraform_wrapper_module_for_each
      - id: terraform_fmt

  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.4.0
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
      - id: check-yaml
      - id: check-added-large-files

Step 3: Install Hooks

pre-commit install

Step 4: Run Hooks Manually

pre-commit run --all-files

12. 🔄 Git Workflows for Terraform

Step 1: Git Flow Workflow

main (production)
  ↑
  |
develop (staging)
  ↑
  |
feature/* (development)

Commands:

# Create feature branch
git checkout -b feature/add-new-resource

# Merge to develop
git checkout develop
git merge feature/add-new-resource

# Create release from develop
git checkout -b release/v1.0.0

# Merge release to main
git checkout main
git merge release/v1.0.0

Step 2: GitHub Flow Workflow

main (production)
  ↑
  |
feature/* (development)

Commands:

# Create feature branch
git checkout -b feature/add-new-resource

# Push and create PR
git push -u origin feature/add-new-resource

# Merge PR to main

Step 3: Trunk-Based Development

main (all development)

Commands:

# Commit directly to main
git add .
git commit -m "Add new resource"
git push origin main

# Or use feature branches with short-lived PRs
git checkout -b feature/quick-fix
# ... make changes ...
git checkout main
git merge --squash feature/quick-fix

13. �📚 Additional Resources

You are now ready to set up and use version control for your Terraform projects! 🚀

Author by:

Note

Join Our Telegram Community // Follow me for more DevOps & Cloud content.