Running a Local DevOps Environment with Terraform and AWS

Run a Local DevOps Environment with Terraform & AWS - Guide

DevOps practices help teams automate infrastructure deployment, streamline development workflows, and improve software delivery. Terraform, an Infrastructure as Code (IaC) tool, allows developers to define and manage infrastructure in a declarative way. In this guide, we will explore how to set up a local DevOps environment using Terraform and AWS.

What We Will Cover:

✅ Install Terraform and AWS CLI
✅ Write and apply Terraform configurations locally
✅ Automate infrastructure creation


1. Installing Terraform and AWS CLI

1.1 Prerequisites

Before we begin, ensure you have:

  • An AWS account
  • AWS IAM credentials (Access Key and Secret Key)
  • Installed Terraform and AWS CLI on your local machine

1.2 Installing AWS CLI

Download and install AWS CLI:

Windows:

msiexec.exe /i https://awscli.amazonaws.com/AWSCLIV2.msi

macOS:

brew install awscli

Linux:

curl "https://awscli.amazonaws.com/AWSCLIV2-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

Verify the installation:

aws --version

Configure AWS credentials:

aws configure

1.3 Installing Terraform

Windows:

Download Terraform binary from Terraform Official Site and add it to your PATH.

macOS:

brew tap hashicorp/tap
brew install hashicorp/tap/terraform

Linux:

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update && sudo apt-get install terraform

Verify Terraform installation:

terraform --version

2. Writing and Applying Terraform Configurations Locally

2.1 Creating a Terraform Configuration

Create a working directory:

mkdir terraform-aws
cd terraform-aws

Create a Terraform configuration file (main.tf):

provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "example" {
  bucket = "my-terraform-bucket"
  acl    = "private"
}

2.2 Initializing and Applying Terraform

Initialize Terraform:

terraform init

Plan the infrastructure changes:

terraform plan

Apply the changes:

terraform apply -auto-approve

Verify the S3 bucket creation:

aws s3 ls

Destroy the infrastructure when done:

terraform destroy -auto-approve

3. Automating Infrastructure Creation

3.1 Using Variables in Terraform

Define variables in variables.tf:

variable "bucket_name" {
  description = "Name of the S3 bucket"
  type        = string
}

Modify main.tf to use variables:

resource "aws_s3_bucket" "example" {
  bucket = var.bucket_name
  acl    = "private"
}

Provide values in terraform.tfvars:

bucket_name = "my-automated-bucket"

Apply the changes:

terraform apply -auto-approve

3.2 Automating with a CI/CD Pipeline

Use GitHub Actions or GitLab CI/CD to automate infrastructure deployments:

name: Terraform CI/CD
on: push
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v2
      - name: Install Terraform
        uses: hashicorp/setup-terraform@v1
      - name: Terraform Init
        run: terraform init
      - name: Terraform Apply
        run: terraform apply -auto-approve

Conclusion

In this guide, we covered:

✅ Installing Terraform and AWS CLI
✅ Writing and applying Terraform configurations
✅ Automating infrastructure creation

Using Terraform for DevOps automation makes infrastructure management efficient and scalable. 🚀

Sandip Mhaske

I’m a software developer exploring the depths of .NET, AWS, Angular, React, and digital entrepreneurship. Here, I decode complex problems, share insightful solutions, and navigate the evolving landscape of tech and finance.

Post a Comment

Previous Post Next Post