A production-grade, highly available, and cost-optimized AWS Virtual Private Cloud (VPC) infrastructure built with Terraform and automated using GitHub Actions CI/CD.
This repository demonstrates Infrastructure as Code (IaC), GitOps, and DevSecOps best practices, including:
- Dynamic multi-AZ subnet provisioning
- Public and private subnet separation
- Automated route management
- Configurable NAT Gateway deployment
- AWS EKS-ready subnet tagging
- Remote Terraform state using Amazon S3
- Terraform state locking using DynamoDB
- Static Terraform security scanning using
tfsec - Automated validation, planning, and deployment using GitHub Actions
- Reusable Terraform module architecture
The infrastructure provisions an isolated AWS network across multiple Availability Zones in the us-east-1 region.
| Resource | Configuration |
|---|---|
| VPC CIDR | 10.0.0.0/16 |
| Availability Zones | us-east-1a, us-east-1b |
| Public Subnet 1 | 10.0.1.0/24 |
| Public Subnet 2 | 10.0.2.0/24 |
| Private Subnet 1 | 10.0.10.0/24 |
| Private Subnet 2 | 10.0.20.0/24 |
| Internet Access | Internet Gateway |
| Private Egress | NAT Gateway |
| Remote State | Amazon S3 |
| State Locking | Amazon DynamoDB |
| Security Scanning | tfsec |
| CI/CD | GitHub Actions |
Public subnets are connected to an Internet Gateway (IGW) and can host internet-facing resources such as:
- Application Load Balancers
- Bastion hosts
- Public-facing services
Private subnets do not receive direct inbound internet access.
Outbound internet access is routed through a NAT Gateway, allowing private resources to securely access package repositories, APIs, and external services.
The module supports configurable NAT Gateway deployment strategies:
- Single NAT Gateway for development environments to reduce infrastructure cost.
- One NAT Gateway per Availability Zone for production environments requiring higher availability.
Public and private subnets include standard AWS EKS subnet tags:
kubernetes.io/role/elb
kubernetes.io/role/internal-elb
These tags allow AWS Load Balancer integrations to automatically identify the appropriate subnets.
Terraform code is statically analyzed using tfsec before infrastructure changes are deployed.
The security scan is integrated directly into the GitHub Actions CI/CD pipeline to identify insecure infrastructure configurations early in the development lifecycle.
Internet
│
▼
┌─────────────────┐
│ Internet Gateway│
└────────┬────────┘
│
┌─────────────┴─────────────┐
│ │
▼ ▼
┌─────────────────┐ ┌─────────────────┐
│ Public Subnet A │ │ Public Subnet B │
│ 10.0.1.0/24 │ │ 10.0.2.0/24 │
│ us-east-1a │ │ us-east-1b │
└────────┬────────┘ └─────────────────┘
│
▼
┌─────────────┐
│ NAT Gateway │
└──────┬──────┘
│
┌──────┴─────────────────────────┐
│ │
▼ ▼
┌─────────────────┐ ┌─────────────────┐
│ Private Subnet A│ │ Private Subnet B│
│ 10.0.10.0/24 │ │ 10.0.20.0/24 │
│ us-east-1a │ │ us-east-1b │
└─────────────────┘ └─────────────────┘
Developer
│
│ Push / Pull Request
▼
GitHub Repository
│
▼
GitHub Actions Runner
│
├─────────────────────────────┐
│ │
▼ ▼
Pull Request Push / Merge to main
│ │
├─ terraform fmt ├─ terraform fmt
├─ terraform init ├─ terraform init
├─ terraform validate ├─ terraform validate
├─ tfsec security scan ├─ tfsec security scan
└─ terraform plan ├─ terraform plan
└─ terraform apply
│
▼
AWS Infrastructure
When a Pull Request is opened or updated, GitHub Actions automatically executes:
terraform fmt -check -recursive
terraform init
terraform validate
tfsec .
terraform planThis ensures that infrastructure changes are:
- Properly formatted
- Syntactically valid
- Security scanned
- Reviewable before deployment
After a Pull Request is approved and merged into main, GitHub Actions executes:
terraform init
terraform validate
tfsec .
terraform plan
terraform apply -auto-approveThis automatically provisions or updates the AWS infrastructure.
Terraform state is stored remotely using Amazon S3.
Example backend configuration:
terraform {
backend "s3" {
bucket = "your-terraform-state-bucket"
key = "dev/vpc/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-state-lock"
encrypt = true
}
}Remote state provides:
- Centralized state management
- Team collaboration
- State persistence
- Protection against local state loss
- Encrypted state storage
- State locking during concurrent Terraform operations
DynamoDB locking prevents multiple Terraform executions from modifying the same state simultaneously.
Replace the example S3 bucket and DynamoDB table names with the resources configured for your environment.
The following screenshots verify that the infrastructure was successfully provisioned in AWS.
Visual representation of the created dev-vpc, subnets, route tables, Internet Gateway, and NAT Gateway.
Public and private subnets distributed across us-east-1a and us-east-1b.
Internet Gateway attached to dev-vpc with standardized resource tags.
Cost-optimized NAT Gateway deployed inside a public subnet to provide outbound internet connectivity for private subnets.
aws-vpc-terraform/
│
├── .github/
│ └── workflows/
│ └── terraform.yml
│
├── docs/
│ └── screenshots/
│ ├── vpc-resource-map.png
│ ├── subnets-list.png
│ ├── internet-gateway.png
│ └── nat-gateway.png
│
├── modules/
│ └── vpc/
│ ├── main.tf
│ ├── outputs.tf
│ └── variables.tf
│
├── environments/
│ └── dev/
│ ├── backend.tf
│ ├── main.tf
│ ├── outputs.tf
│ ├── providers.tf
│ ├── terraform.tfvars
│ └── variables.tf
│
├── .gitignore
└── README.md
.github/workflows/
Contains the GitHub Actions workflow responsible for Terraform validation, security scanning, planning, and deployment.
modules/vpc/
Contains the reusable VPC Terraform module.
environments/dev/
Contains development environment configuration and module invocation.
docs/screenshots/
Contains AWS Console screenshots used for infrastructure verification.
Before using this project, install and configure:
- Terraform
>= 1.5.0 - AWS CLI v2
- Git
tfsec- An AWS account with permissions to create networking resources
- An Amazon S3 bucket for Terraform remote state
- A DynamoDB table for Terraform state locking
For local Terraform execution, configure AWS credentials:
aws configureYou will be prompted for:
AWS Access Key ID
AWS Secret Access Key
Default region name
Default output format
Example region:
us-east-1
Verify authentication:
aws sts get-caller-identityFor CI/CD deployment, configure the following GitHub Repository Secrets:
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
Navigate to:
GitHub Repository
→ Settings
→ Secrets and variables
→ Actions
→ New repository secret
Add the required AWS credentials.
For production environments, GitHub Actions with AWS IAM OpenID Connect (OIDC) is preferred over long-lived AWS access keys.
git clone https://github.com/ChaninduImanjith/aws-vpc-terraform.git
cd aws-vpc-terraformcd environments/devterraform initterraform fmt -recursiveCheck formatting without modifying files:
terraform fmt -check -recursiveterraform validatetfsec .terraform planReview all proposed changes before applying them.
terraform applyOr:
terraform apply -auto-approveFor normal GitOps workflows, deployment should occur through GitHub Actions after merging changes into main.
git checkout -b feature/network-updateModify the required Terraform configuration.
terraform fmt -recursive
terraform validate
tfsec .
terraform plangit add .
git commit -m "Update network configuration"git push origin feature/network-updateCreate a Pull Request from:
feature/network-update
↓
main
GitHub Actions will automatically run:
terraform fmt
terraform init
terraform validate
tfsec security scan
terraform plan
After reviewing:
- Terraform code
- tfsec security results
- Terraform execution plan
- GitHub Actions status
merge the Pull Request.
The deployment workflow will then execute Terraform against AWS.
To remove the provisioned infrastructure manually:
cd environments/dev
terraform destroyOr:
terraform destroy -auto-approveAlways review destroy plans carefully before approving them, especially in shared or production environments.
| Name | Description | Type | Default | Required |
|---|---|---|---|---|
vpc_cidr |
Base CIDR block for the VPC | string |
"10.0.0.0/16" |
Yes |
public_subnet_cidrs |
CIDR blocks for public subnets | list(string) |
["10.0.1.0/24", "10.0.2.0/24"] |
Yes |
private_subnet_cidrs |
CIDR blocks for private subnets | list(string) |
["10.0.10.0/24", "10.0.20.0/24"] |
Yes |
enable_single_nat_gateway |
Use a single NAT Gateway for cost optimization | bool |
true |
No |
| Name | Description |
|---|---|
vpc_id |
ID of the created AWS VPC |
public_subnet_ids |
List of public subnet IDs |
private_subnet_ids |
List of private subnet IDs |
nat_gateway_ips |
Public Elastic IP addresses assigned to NAT Gateways |
Example:
terraform outputPossible output:
vpc_id = "vpc-xxxxxxxxxxxxxxxxx"
public_subnet_ids = [
"subnet-xxxxxxxxxxxxxxxxx",
"subnet-yyyyyyyyyyyyyyyyy"
]
private_subnet_ids = [
"subnet-aaaaaaaaaaaaaaaaa",
"subnet-bbbbbbbbbbbbbbbbb"
]
- Multi-AZ AWS VPC architecture
- Reusable Terraform module design
- Public and private subnet isolation
- Dynamic subnet provisioning
- Internet Gateway configuration
- NAT Gateway private subnet egress
- Single NAT Gateway cost-optimization mode
- Multi-NAT production deployment support
- Automatic route table creation
- Automatic route table associations
- Elastic IP management
- AWS EKS-ready subnet tagging
- Consistent AWS resource tagging
- Environment-specific Terraform configuration
- Amazon S3 remote Terraform state
- DynamoDB Terraform state locking
- Static Terraform security scanning using
tfsec - DevSecOps security checks inside CI/CD
- GitHub Actions CI/CD automation
- Pull Request Terraform validation
- Automated Terraform planning
- Automated infrastructure deployment
- GitOps-style infrastructure workflow
Security validation is integrated directly into the infrastructure delivery workflow using tfsec.
tfsec performs static analysis on Terraform configuration and can detect issues such as:
- Publicly exposed infrastructure
- Overly permissive network rules
- Missing encryption
- Insecure AWS service configurations
- Weak security defaults
- Misconfigured cloud resources
Run the scan locally:
tfsec .The same security scan also runs automatically inside GitHub Actions.
If the security scan fails, the CI/CD workflow can stop before insecure infrastructure changes are deployed.
Private subnets do not expose resources directly to the public internet.
Outbound traffic follows:
Private Resource
│
▼
Private Route Table
│
▼
NAT Gateway
│
▼
Internet Gateway
│
▼
Internet
Terraform state is stored remotely in Amazon S3 with encryption enabled:
encrypt = trueProduction environments should additionally consider:
- S3 bucket versioning
- S3 public access blocking
- Restricted IAM access
- Server-side encryption using AWS KMS
- CloudTrail logging
- State backup and recovery policies
DynamoDB state locking protects against concurrent Terraform executions that could otherwise corrupt infrastructure state.
GitHub Actions credentials should follow the principle of least privilege.
For production environments, prefer short-lived AWS credentials through GitHub Actions OIDC federation instead of storing long-lived IAM access keys.
A single NAT Gateway reduces development cost but introduces an availability dependency.
For production workloads, deploying one NAT Gateway per Availability Zone provides better fault isolation.
Recommended local development workflow:
terraform fmt -recursive
terraform init
terraform validate
tfsec .
terraform planRecommended deployment workflow:
Developer
│
▼
Feature Branch
│
▼
Pull Request
│
▼
Terraform Formatting
│
▼
Terraform Validation
│
▼
tfsec Security Scan
│
▼
Terraform Plan
│
▼
Code Review
│
▼
Merge to main
│
▼
Terraform Apply
│
▼
AWS Infrastructure
AWS verification screenshots should be stored in:
docs/screenshots/
Expected files:
vpc-resource-map.png
subnets-list.png
internet-gateway.png
nat-gateway.png
If the filenames are changed, update the corresponding image paths in this README.
Potential improvements for production environments include:
- GitHub Actions authentication using AWS OIDC
- Separate
dev,staging, andprodenvironments - GitHub Environment approval gates for production
- TFLint integration
- Checkov security scanning
- Amazon VPC Flow Logs
- VPC Endpoints for private AWS service access
- AWS KMS encryption for remote state
- Network ACL customization
- AWS Transit Gateway integration
- Automated Terraform documentation
- Infrastructure testing
- Automated security reporting
GitHub:
https://github.com/ChaninduImanjith/aws-vpc-terraform
Clone:
git clone https://github.com/ChaninduImanjith/aws-vpc-terraform.gitThis project is licensed under the MIT License.
See the LICENSE file for details.



