This project demonstrates how to create a VPC in AWS using Terraform and automate the deployment using GitHub Actions.
- AWS account
- GitHub account
- Terraform CLI
- AWS CLI
- GitHub CLI
- Set up your environment
- Create and apply Terraform configuration
- Automate with GitHub Actions
Install Terraform:
- Visit the Terraform download page.
- Download and install Terraform for your operating system.
Install AWS CLI:
- Follow the AWS CLI installation guide.
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:
- Follow the GitHub CLI installation guide.
Authenticate GitHub CLI:
gh auth login
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"
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.
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
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.
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.