Skip to content

Syed-portfolio/GitHub-Actions-Terraform-AWS-VPC-Project

Repository files navigation

GitHub Actions Terraform AWS VPC Project

Overview

This project demonstrates how to create a VPC in AWS using Terraform and automate the deployment using GitHub Actions.

Prerequisites

  • AWS account
  • GitHub account
  • Terraform CLI
  • AWS CLI
  • GitHub CLI

Steps

  1. Set up your environment
  2. Create and apply Terraform configuration
  3. Automate with GitHub Actions

Steps

1. Set Up Your Environment

Install Terraform:

Install AWS CLI:

Configure AWS CLI:

aws configure
AWS Access Key ID [None]: <Your_AWS_Access_Key_ID>
AWS Secret Access Key [None]: <Your_AWS_Secret_Key>
Default region name [None]: us-east-2
Default output format [None]: json

Install GitHub CLI:

Authenticate GitHub CLI:

gh auth login

2. Create a Terraform Configuration

Initialize a New Terraform Project:

mkdir terraform-aws-vpc
cd terraform-aws-vpc

Create main.tf:

provider "aws" {
  region = "us-east-2"
  access_key = var.aws_access_key
  secret_key = var.aws_secret_key
}

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "subnet" {
  vpc_id            = aws_vpc.main.id
  cidr_block        = "10.0.1.0/24"
  availability_zone = "us-east-2a"
}

output "vpc_id" {
  value = aws_vpc.main.id
}

output "subnet_id" {
  value = aws_subnet.subnet.id
}

Create variables.tf:

variable "aws_access_key" {
  description = "The AWS access key"
}

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

Create terraform.tfvars:

aws_access_key = "Your_AWS_Access_Key_ID"
aws_secret_key = "Your_AWS_Secret_Key"

3. Initialize and Apply the Configuration

Initialize Terraform:

terraform init

Plan the Infrastructure Changes:

terraform plan

Apply the Configuration:

terraform apply
  • Review the plan and confirm by typing yes when prompted.

4. Automate with GitHub Actions

Create the GitHub Actions directories:

mkdir -p .github/workflows

Create terraform.yml:

name: 'Terraform AWS VPC'
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

      - name: Terraform Init
        run: terraform init
        working-directory: terraform-aws-vpc

      - name: Terraform Plan
        run: terraform plan
        working-directory: terraform-aws-vpc

      - name: Terraform Apply
        run: terraform apply -auto-approve
        working-directory: terraform-aws-vpc

Commit and Push Your Workflow:

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

5. Monitor the Workflow

Go to Your Repository on GitHub:

  • Navigate to the Actions tab to monitor the workflow's progress.
  • Ensure that the workflow runs and completes successfully.

6. Verify Terraform Deployment

Check AWS Management Console:

  • Log in to your AWS Management Console and navigate to the VPC section to verify that the VPC and subnet are created as expected.

image

Resources

About

A repository for the SN-Platform project

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages