Skip to content

Commit

Permalink
Created VPC + Subnets using TF (#2)
Browse files Browse the repository at this point in the history
- One VPC + 2 Subnets for webapp and db each
- Modularized the code based on the resources
  • Loading branch information
Kashyab19 authored Jul 8, 2024
1 parent ac87453 commit c2e3b13
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 1 deletion.
28 changes: 28 additions & 0 deletions .github/workflows/tf-validation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: "Terraform GCP Deploy"

on:
push:
branches:
- main
- feature/assignment-03

jobs:
validate:
name: "Validate Terraform Templates"
runs-on: ubuntu-latest

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

- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: "1.7.3"

- name: Terraform Initialization
run: terraform init
# Initializes Terraform, a necessary step before validation

- name: Terraform Validate
run: terraform validate
37 changes: 37 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Local .terraform directories
**/.terraform/*

# .tfstate files
*.tfstate
*.tfstate.*

# Crash log files
crash.log
crash.*.log

# Exclude all .tfvars files, which are likely to contain sensitive data, such as
# password, private keys, and other secrets. These should not be part of version
# control as they are data points which are potentially sensitive and subject
# to change depending on the environment.
*.tfvars
*.tfvars.json

# Ignore override files as they are usually used to override resources locally and so
# are not checked in
override.tf
override.tf.json
*_override.tf
*_override.tf.json

# Ignore transient lock info files created by terraform apply
.terraform.tfstate.lock.info

# Include override files you do wish to add to version control using negated pattern
# !example_override.tf

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*

# Ignore CLI configuration files
.terraformrc
terraform.rc
22 changes: 22 additions & 0 deletions .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# tf-gcp-infra-summer
# tf-gcp-infra-summer

Enabled GCP APIs: Compute Engine
14 changes: 14 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
provider "google" {
project = var.project_id
region = var.region
}

module "vpc" {
source = "./vpc"
vpc_name = var.vpc_name
region = var.region
webapp_subnet_cidr = var.webapp_subnet_cidr
db_subnet_cidr = var.db_subnet_cidr
subnet1 = var.subnet1
subnet2 = var.subnet2
}
14 changes: 14 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
output "vpc_self_link" {
description = "The self-link of the VPC"
value = module.vpc.vpc_self_link
}

output "webapp_subnet_self_link" {
description = "The self-link of the webapp subnet"
value = module.vpc.webapp_subnet_self_link
}

output "db_subnet_self_link" {
description = "The self-link of the db subnet"
value = module.vpc.db_subnet_self_link
}
35 changes: 35 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
variable "project_id" {
description = "The ID of the project in which to create the VPC"
type = string
}

variable "region" {
description = "The region in which to create the VPC and subnets"
type = string
default = "us-central1"
}

variable "vpc_name" {
description = "The name of the VPC"
type = string
}

variable "webapp_subnet_cidr" {
description = "The CIDR range of the webapp subnet"
type = string
}

variable "db_subnet_cidr" {
description = "The CIDR range of the db subnet"
type = string
}

variable "subnet1" {
description = "The name of my subnet"
type = string
}

variable "subnet2" {
description = "The name of my subnet 2"
type = string
}
31 changes: 31 additions & 0 deletions vpc/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
resource "google_compute_network" "vpc" {
name = var.vpc_name
auto_create_subnetworks = false
routing_mode = "REGIONAL"
}

resource "google_compute_subnetwork" "webapp" {
name = var.subnet1
ip_cidr_range = var.webapp_subnet_cidr
region = var.region
network = google_compute_network.vpc.self_link
}

resource "google_compute_subnetwork" "db" {
name = var.subnet2
ip_cidr_range = var.db_subnet_cidr
region = var.region
network = google_compute_network.vpc.self_link
}

resource "google_compute_route" "webapp_default_route" {
name = "webapp-default-route"
network = google_compute_network.vpc.self_link
dest_range = "0.0.0.0/0"
next_hop_gateway = "default-internet-gateway"
priority = 1000

tags = ["webapp"]

depends_on = [google_compute_subnetwork.webapp]
}
14 changes: 14 additions & 0 deletions vpc/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
output "vpc_self_link" {
description = "The self-link of the VPC"
value = google_compute_network.vpc.self_link
}

output "webapp_subnet_self_link" {
description = "The self-link of the webapp subnet"
value = google_compute_subnetwork.webapp.self_link
}

output "db_subnet_self_link" {
description = "The self-link of the db subnet"
value = google_compute_subnetwork.db.self_link
}
29 changes: 29 additions & 0 deletions vpc/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
variable "vpc_name" {
description = "The name of the VPC"
type = string
}

variable "webapp_subnet_cidr" {
description = "The CIDR range of the webapp subnet"
type = string
}

variable "db_subnet_cidr" {
description = "The CIDR range of the db subnet"
type = string
}

variable "region" {
description = "The region in which to create the VPC and subnets"
type = string
}

variable "subnet1" {
description = "The name of my subnet"
type = string
}

variable "subnet2" {
description = "The name of my subnet 2"
type = string
}

0 comments on commit c2e3b13

Please sign in to comment.