Efficient Deployment of AWS Infrastructure using Terraform with GitHub Actions: A Step-by-Step Guide

·

3 min read

Efficient Deployment of AWS Infrastructure using Terraform with GitHub Actions: A Step-by-Step Guide

Introduction

When orchestrating infrastructure deployment, it's important to use tools that ensure precision and security. In this article, we'll examine a methodical approach to deploying AWS infrastructure, particularly EC2 instances across multiple Availability Zones (AZs), using Terraform, and GitHub Actions.

GitHub Actions is a CI/CD tool that automates workflows based on repository events such as pull requests, issues or commits.

Terraform, on the other hand, is an Infrastructure as Code(IaC) tool that enables the provisioning of cloud infrastructure using a high-level configuration syntax. It allows for version-controlled, repeatable, and automated setup of resources like AWS EC2 instances.

Step 1: Setting up Your AWS Account and IAM

The first step is to ensure you have signed up for an AWS account. Create an IAM user and assign necessary permissions for managing EC2, S3, and IAM resources. For this project, we will use an AWS S3 bucket to store our Terraform state. Securely store your access key and secret key, as they will be used in GitHub Actions.

Step 2: Configuring Terraform for AWS

Install Terraform and set up your Terraform configuration in the root directory. Below is an example of code contained in the file:

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

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

The above code defines the AWS provider and the required EC2 instance specifications. Replace the ami and region with your desired values.

Step 3: Storing Terraform State in S3

Using AWS S3 for Terraform state storage assists in storing your state remotely. Here's an example of how you configure it:

terraform {
  backend "s3" {
    bucket = "my-terraform-state-bucket"
    key    = "state"
    region = "us-east-1"
  }
}

Create an S3 bucket (in this example, "my-terraform-state-bucket") and ensure it’s private and secure.

Step 4: Setting up GitHub Actions

Create a GitHub repository and add your Terraform files. Now, set up GitHub Actions by creating a .github/workflows/main.yml file:

name: 'Terraform'

on:
  push:
    branches:
    - main

jobs:
  terraform:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v3

    - name: Set up AWS credentials
      uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: us-east-1

    - name: Terraform Init
      uses: hashicorp/setup-terraform@v2

    - name: Terraform Plan
      run: terraform plan

    - name: Terraform Apply
      run: terraform apply -auto-approve

The workflow initializes Terraform, plans the deployment, and applies it to the main branch. Your AWS credentials should never be exposed in code; they should be securely stored in GitHub Secrets, which can be found in the repository settings.

Step 5: Deploying Across Multiple Availability Zones

To ensure high availability, modify your Terraform configuration to deploy two EC2 instances across two different AZs:

resource "aws_instance" "example" {
  count         = 2
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  availability_zone = count.index == 0 ? "us-east-1a" : "us-east-1b"

Conclusion

The above guide outlines the steps for deploying a scalable AWS infrastructure using Terraform with GitHub Actions. To manage your AWS costs effectively, always remember to terminate resources when not in use. By following these steps, you can build a robust, automated infrastructure deployment pipeline that aligns with best practices in modern cloud environments.