Skip to content

Set up a GitHub Actions workflow to automatically deploy AWS infrastructure using Terraform

rajiv-njit edited this page Nov 11, 2023 · 8 revisions

let's break down the steps for setting up a GitHub Actions workflow to automatically deploy AWS infrastructure using Terraform. This assumes that you already have a basic understanding of Terraform, an AWS account, and a GitHub repository.

Step 1: Clone Your Repository

First, make sure you have your project on GitHub. If not, create a new repository and clone it to your local machine.

git clone [email protected]:rajiv-njit/Terraform-AWS-GitHubActions-DevOps.git

cd Terraform-AWS-GitHubActions-DevOps

Step 2: Create Terraform Configuration

Write your Terraform configuration files (main.tf, variables.tf, etc.) to define your AWS infrastructure. Ensure your Terraform files are in a directory, for example, terraform/.

Below is a basic template for the main Terraform files: main.tf and variables.tf. You can use these templates as a starting point and customize them according to your specific AWS infrastructure requirements.

main.tf `provider "aws" { region = "us-east-1" # Update with your desired AWS region }

Define your AWS resources below resource "aws_instance" "example_instance" { ami = "ami-0c55b159cbfafe1f0" # Update with your desired AMI ID instance_type = "t2.micro" key_name = "your-key-pair-name" # Update with your key pair name

tags = { Name = "example-instance" } } variable "aws_access_key" { description = "AWS access key" }

variable "aws_secret_key" { description = "AWS secret key" }

Add any additional variables as needed for your infrastructure `

  • In the main.tf file, you'll find a basic AWS provider configuration and an example resource (EC2 instance). Update the region, AMI ID, and key pair name according to your preferences.
  • In the variables.tf file, you have placeholders for AWS access key and secret key. However, it's recommended not to hardcode sensitive information directly in your Terraform files. Instead, you can reference these variables and set their values using environment variables, Terraform variables, or other secure methods.
  • Make sure to customize these templates based on your specific infrastructure needs, and feel free to add more resources or configurations as required.

Step 3: Set Up AWS Credentials

In GitHub Actions, you need to set up AWS credentials securely. You can use GitHub Secrets for this.

  • Go to your GitHub repository.
  • Navigate to "Settings" > "Secrets" > "New repository secret."
  • Name the secret AWS_ACCESS_KEY_ID and add your AWS access key.
  • Name the secret AWS_SECRET_ACCESS_KEY and add your AWS secret key.

How are you ensuring my sensitive information not exposed as this is Public repository? can anyone access Secrets from repository?

Security is a critical concern when dealing with secrets in a public repository. While GitHub Actions allows you to use secrets to protect sensitive information, it's important to note that secrets are not directly exposed in the GitHub repository. They are securely stored and can only be accessed by GitHub Actions workflows during the runtime.

Here are some key points to understand how GitHub Actions handles secrets:

  • Secrets Storage: GitHub securely stores secrets, and they are not accessible directly by users, even if the repository is public.
  • Encrypted Environment: GitHub Actions provides an encrypted environment for running workflows. Secrets are decrypted only when they are needed during the workflow execution.
  • Scoped Access: Secrets are scoped to specific workflows or jobs. They are not exposed globally to all workflows in a repository.
  • Access Control: Users with write or admin access to the repository can create, update, and manage secrets. However, viewers (read-only access) cannot access or modify secrets.

Despite these measures, it's essential to be cautious and follow best practices to minimize the risk:

  • Avoid Hardcoding Secrets: Never hardcode sensitive information directly in your code or configuration files.
  • Use Environment Variables: Reference secrets as environment variables in your GitHub Actions workflow. GitHub Actions automatically masks secrets in the workflow logs to prevent accidental exposure.
  • Review Access Permissions: Regularly review and audit the access permissions for your repository to ensure that only authorized users have access to sensitive information.
  • Monitor Workflow Runs: Keep an eye on the GitHub Actions workflow runs, especially if they include steps that use secrets. This helps you quickly identify any unexpected behavior.

By following these practices, you can leverage GitHub Actions secrets in a public repository while minimizing the risk of exposing sensitive information.

Step 4: Create GitHub Actions Workflow

Create a new file in your repository under the path .github/workflows/terraform.yml. This file will define your GitHub Actions workflow.

Here's a basic example:

name: Terraform Deploy

on: push: branches: - main

jobs: terraform: runs-on: ubuntu-latest

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

  `- name: Set up Terraform`
    `uses: hashicorp/setup-terraform@v1`
    `with:`
      `terraform_version: 1.0.0`

  `- name: Terraform Init`
    `run: terraform init terraform/`

  `- name: Terraform Plan`
    `run: terraform plan -out=tfplan terraform/`

  `- name: Terraform Apply`
    `run: terraform apply -auto-approve tfplan terraform/`

This workflow triggers on pushes to the main branch. It checks out the repository, sets up Terraform, initializes Terraform, plans the deployment, and then applies it.

Step 5: Commit and Push

Commit your changes and push them to your GitHub repository:

git add .github/workflows/terraform.yml git commit -m "Add GitHub Actions workflow for Terraform" git push origin main

Step 6: Monitor GitHub Actions

Go to the "Actions" tab on your GitHub repository to monitor the progress of your workflow. You should see your Terraform workflow running.

That's it! You've set up a basic GitHub Actions workflow to automatically deploy AWS infrastructure using Terraform.

As you become more comfortable, you can customize the workflow to suit your specific needs, integrate additional checks, and handle more advanced scenarios.