Automating AWS Service Deployments using GitHub Actions

Automating AWS Service Deployments using GitHub Actions

Introduction:-

AWS EC2 instances are widely used for running virtual machines in the cloud. #Terraform is a popular #infrastructure-as-code (IaC) tool that allows you to define and manage your infrastructure using code. GitHub Actions is a powerful automation tool that enables you to create workflows to automatically build, test, and deploy code changes in your #GitHub repositories. In this blog post, we will explore how to deploy #AWS EC2 instances using Terraform and automate the process using GitHub Actions.

Prerequisites :-

Before we begin, make sure you have the following prerequisites in place:

  1. AWS account: You will need an AWS account with appropriate permissions to create EC2 instances.

  2. GitHub account: You will need a GitHub account to create a GitHub repository and set up GitHub Actions.

  3. Terraform installed: You will need Terraform installed on your local machine or the environment where you plan to run the Terraform commands.

Step-1 : Create a GitHub repository

  1. Log in to your GitHub account and navigate to the main page.

  2. Create a Repository with a default name.

Step-2 : Configure the GitHub repository

  1. create directory on your desktop and add(copy) your Terraform source code to that directory.

  2. Open a terminal on your local machine and navigate to your source code directory.

  3. Configure the remote origin for the github repository which u have created in step-1.

Step-3 :- Push the code to GitHub

  1. Navigate to the folder where you have configured your GitHub repository using the terminal.

  2. Run the following commands to push the code to the GitHub repository:

git add .
git commit -m "Initial commit from mahiratechnology"
git push origin master

This will push the code to the GitHub repository that you created in step 1. Make sure the repository is set to private to ensure the security of your Terraform code and AWS credentials.

Step 4: Configure GitHub Actions workflow

  1. Go to the GitHub repository where you pushed the code and navigate to the “Actions” tab.

  2. Click on the “Set up a workflow yourself” or “New workflow” button.

  3. Enter a name for the workflow, such as “Deploy EC2 with Terraform”.

  4. In the editor, paste the following code for the workflow:

name: Deploy EC2 with Terraform

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest

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

    - name: Set up Terraform
      run: |
        VERSION=$(curl --silent https://checkpoint-api.hashicorp.com/v1/check/terraform | jq -r .current_version)
        DOWNLOAD_URL="https://releases.hashicorp.com/terraform/${VERSION}/terraform_${VERSION}_linux_amd64.zip"
        curl -o terraform.zip ${DOWNLOAD_URL}
        unzip terraform.zip
        sudo mv terraform /usr/local/bin/
        rm terraform.zip

    - name: Configure AWS credentials
      run: |
        echo "$AWS_ACCESS_KEY_ID" > aws_access_key_id
        echo "$AWS_SECRET_ACCESS_KEY" > aws_secret_access_key
        aws configure set aws_access_key_id $(cat aws_access_key_id)
        aws configure set aws_secret_access_key $(cat aws_secret_access_key)
        aws configure set default.region ${{ secrets.AWS_REGION }}
        rm aws_access_key_id aws_secret_access_key

    - name: Run Terraform commands
      env:
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
      run: |
        terraform init
        terraform validate
        terraform plan -out=tfplan
        terraform apply -auto-approve tfplan

This GitHub Actions workflow is triggered whenever a push event occurs on the “main” branch of your repository. It sets up Terraform, configures AWS credentials, and runs the necessary Terraform commands to deploy EC2 instances in AWS.

Note: Make sure to replace the placeholders like ${{secrets.AWS_REGION }}, ${{secrets.AWS_ACCESS_KEY_ID }}, and ${{secrets.AWS_SECRET_ACCESS_KEY }} with the actual values for your AWS region and credentials, which you will configure in the subsequent steps.

Step 5: Configure AWS credentials

  1. Login to your aws management and create a user with administarator access.

  2. create access and secret access keys for the user and download those credentials.

Step 6: Configure GitHub repository secrets

  1. Go back to the GitHub repository where you created the workflow and navigate to the “Settings” tab.

  2. In the left-hand menu, click on “Secrets”.

  3. Click on the “New repository secret” button.

  4. Enter a name for the secret, such as “AWS_ACCESS_KEY_ID”, and enter the corresponding value obtained from step 5.

  5. Click on the “Add secret” button.

  6. Repeat the process to add another secret for “AWS_SECRET_ACCESS_KEY” with its corresponding value obtained from step 5.

Step 7: Update the GitHub Actions workflow

  1. Go back to the GitHub Actions workflow that you created in step 4 and click on the “Edit” button.

  2. Update the “env” section with the correct AWS region, if necessary.

  3. Update the “steps” section with the Terraform commands you want to run, such as “terraform init”, “terraform plan”, and “terraform apply”.

  4. Make sure to set the working directory to the location where your Terraform code is stored in the repository.

Step 8: Trigger the GitHub Actions workflow

  1. Commit and push any changes to your Terraform code in the GitHub repository.

  2. Go to the “Actions” tab in the GitHub repository and you should see the workflow you created.

  3. Click on the “Run workflow” button and select the appropriate inputs, such as the “tfpath” input for the Terraform file path, if you defined it in the workflow.

  4. Click on the “Run workflow” button to trigger the workflow.

Step 9: Monitor the GitHub Actions workflow

  1. Once the workflow is triggered, you can monitor its progress in the “Actions” tab.

  2. The workflow will run the Terraform commands as defined in the workflow and deploy the EC2 instances in AWS.

  3. If there are any errors or issues, you can check the logs and troubleshoot accordingly.

  4. Once the workflow completes successfully, you should be able to see the deployed EC2 instances in your AWS account.

Conclusion:-

In this blog post, we explored how to deploy #AWS EC2 instances using #Terraform and automate the process using GitHub Actions. By following the steps outlined above, you can set up a seamless and automated workflow for deploying EC2 instances with Terraform in your AWS environment. This allows for efficient and repeatable infrastructure deployments, making it easier to manage your cloud resources. Happy automating!