DevOps for Beginners: Automating Infrastructure Provisioning with Terraform and AWS

In the world of DevOps, infrastructure as code (IaC) is a fundamental concept. It allows you to manage and provision your infrastructure using code, enabling automation and scalability. In this guide, we’ll walk through a beginner-friendly DevOps project that leverages Terraform to provision cloud infrastructure on AWS.

By the end of this tutorial, you’ll know how to:

  • Set up Terraform

  • Use Terraform to provision an EC2 instance on AWS

  • Automate infrastructure deployment using CI/CD with GitHub Actions

Let’s get started!

Prerequisites:

Before diving into the project, ensure you have the following:

Step 1: Set Up Terraform

Objective: Install Terraform and set up a basic configuration file.

  1. Install Terraform:

  2. Follow the official installation guide to install Terraform on your machine.

  3. Create a New Project Directory:

  4. Create a directory for your project and navigate into it:

mkdir terraform-aws-project
cd terraform-aws-project
  1. Configure Your AWS Credentials:

  2. Install the AWS CLI if you haven’t already and configure your credentials:

aws configure
  • You’ll need your AWS access key, secret key, and region (e.g., us-east-1).

  • Write Your First Terraform Configuration File:

  • Create a file called main.tf and add the following code to provision an EC2 instance:

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

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"  # Replace with a valid AMI ID for your region
  instance_type = "t2.micro"
}
  • This file defines an EC2 instance with the specified AMI and instance type.

Step 2: Initialize and Apply Terraform

Objective: Use Terraform to provision the EC2 instance on AWS.

  1. Initialize Terraform:

  2. Run the following command to initialize Terraform. This will download the necessary provider plugins: terraform init

  3. Apply the Terraform Configuration:

  4. To provision the infrastructure, run: terraform apply

  5. Terraform will show you an execution plan and ask for confirmation. Type yes to proceed.

  6. After a few moments, Terraform will provision the EC2 instance on AWS.

  7. Verify the EC2 Instance:

  8. Log in to the AWS Management Console and navigate to the EC2 dashboard. You should see the instance running.

Step 3: Automate Infrastructure Deployment with GitHub Actions

Objective: Use GitHub Actions to automate the deployment of your infrastructure using Terraform.

Create a GitHub Repository:

  • Create a new repository on GitHub and clone it to your local machine.

  • Add your Terraform configuration files (main.tf) to the repository.

  • Commit and push your code to GitHub:

git add .
git commit -m "Initial Terraform configuration"
git push origin main
  1. Set Up GitHub Actions:

  2. In your repository, create a .github/workflows directory.

  3. Inside that directory, create a file named terraform.yml with the following content:

name: Terraform

on:
  push:
    branches:
      - main

jobs:
  terraform:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Set up Terraform
      uses: hashicorp/setup-terraform@v1

    - name: Initialize Terraform
      run: terraform init

    - name: Apply Terraform
      run: terraform apply -auto-approve
      env:
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
  • This GitHub Actions workflow will trigger on every push to the main branch, initialize Terraform, and apply the configuration.

  • Store AWS Credentials in GitHub Secrets:

  • Go to your repository settings on GitHub, and under “Secrets and variables”, add two new secrets: AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. These should be the credentials for your AWS account.

  • Test the Workflow:

  • Push your changes to the main branch. GitHub Actions should automatically run the workflow, provisioning the infrastructure on AWS.

  • You can monitor the progress of the workflow in the “Actions” tab of your repository.

Step 4: Destroy Infrastructure

Objective: Clean up the AWS resources by destroying the infrastructure when no longer needed.

  1. Destroy the Resources:

  2. In your local environment, run the following command to destroy the resources provisioned by Terraform: terraform destroy

  3. This will remove the EC2 instance and any associated resources.

  4. Automate the Teardown:

  5. You can extend your GitHub Actions workflow to include a destroy job that runs when you need to tear down the infrastructure.

Conclusion

Congratulations! You’ve completed your first DevOps project using Terraform and AWS. You’ve learned how to set up and manage infrastructure as code, automate deployments with GitHub Actions, and efficiently provision and destroy cloud resources.

This project introduces you to the core principles of DevOps: automation, version control, and continuous delivery. As you grow in your DevOps journey, you’ll explore more complex infrastructures, integrate other tools like Kubernetes, and dive deeper into monitoring and security.

Feel free to share your experience and ask questions in the comments. Happy coding!