Cloud Resume Challenge Part 5 - Automating Backend Deployment with Terraform and GitHub Actions

Cloud Resume Challenge Part 5 - Automating Backend Deployment with Terraform and GitHub Actions

As part of the Cloud Resume Challenge, automating infrastructure deployment is a key skill to showcase. In this step-by-step guide, we will explore how to automate the backend deployment of your Cloud Resume Challenge website using Terraform and GitHub Actions. By leveraging these powerful tools, you can streamline the deployment process, ensure reproducibility, and demonstrate your proficiency in infrastructure automation.

Step 1: Set up the GitHub Actions Workflow

To begin, create a new file named .github/workflows/terraform-deployment.yml in your Cloud Resume Challenge repository. This file will define the GitHub Actions workflow for automating the deployment. Inside the file, add the following workflow configuration code:

name: "Terraform Deployment for Cloud Resume Challenge"

on:
  push:
    branches:
      - main

jobs:
  terraform-plan-and-apply:
    name: "Terraform Init, Plan, and Apply"
    runs-on: ubuntu-latest
    env:
      AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
    defaults:
      run:
        working-directory: infra

Step 2: Check out the Repository Code

To ensure the workflow has access to the repository's code, add the following step to the workflow configuration:

    steps:
      - name: Checkout Repository Code
        uses: actions/checkout@v2

Step 3: Set up Terraform CLI

To use Terraform within the workflow, we need to set up the Terraform CLI. Add the following step to the workflow configuration:

      - name: Setup Terraform
        uses: hashicorp/setup-terraform@v1

Step 4: Initialize the Terraform Working Directory

Before deploying the infrastructure, initialize the Terraform working directory. Add the following step to the workflow configuration:

      - name: Terraform Init
        id: init
        run: terraform init

Step 5: Validate the Terraform Configuration Files

To ensure the correctness of your Terraform configuration files, add the following step to the workflow configuration:

      - name: Terraform Validate
        id: validate
        run: terraform validate -no-color

Step 6: Check the Formatting of Terraform Files

Consistent code formatting is essential for readability and maintainability. Use Terraform to check if your files adhere to the desired format. Add the following step to the workflow configuration:

      - name: Terraform Format
        run: terraform fmt -check

Step 7: Generate and Show the Execution Plan

To review the changes that Terraform will apply to the infrastructure, generate and examine the execution plan. Add the following step to the workflow configuration:

      - name: Terraform Plan
        id: plan
        run: terraform plan -no-color

Step 8: Apply the Changes in the Terraform Plan

After reviewing and approving the execution plan, apply the changes to deploy the infrastructure. Add the following step to the workflow configuration:

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

Step 9: Testing the Workflow

To ensure that the GitHub Actions workflow is functioning correctly, it's essential to test it before relying on it for automated deployments. Follow these steps to test the workflow:

  1. Commit and push your changes: Make a small change to your code or configuration files, such as modifying a comment or adding a new file.

  2. Push the changes to your repository: Use the git push command to push the changes to your GitHub repository.

  3. Monitor the workflow execution: Go to the "Actions" tab in your GitHub repository and navigate to the workflow you set up. You should see a new workflow run triggered by the push event.

  4. Review the workflow logs: Click on the workflow run to view the details. Monitor the logs to ensure that each step is executed without errors. You should see the steps being executed, including the Terraform initialization, validation, formatting, planning, and applying.

  5. Verify the deployment: After the workflow run completes, verify that the changes are successfully deployed to your infrastructure. Access your AWS account and confirm that the desired resources are created or modified according to your Terraform configuration.

Congratulations! You have successfully set up the GitHub Actions workflow to automate the backend deployment of your Cloud Resume Challenge website using Terraform.

With this automation in place, you can confidently iterate on your Cloud Resume Challenge project, focusing on enhancing the user experience, showcasing your skills, and achieving your goals.

Did you find this article valuable?

Support Esteban Moreno by becoming a sponsor. Any amount is appreciated!