From c2e3b139ddae0c8391c237453340f35a7bf9773d Mon Sep 17 00:00:00 2001 From: Kashyab Murali <22251972+Kashyab19@users.noreply.github.com> Date: Mon, 8 Jul 2024 10:45:40 -0400 Subject: [PATCH] Created VPC + Subnets using TF (#2) - One VPC + 2 Subnets for webapp and db each - Modularized the code based on the resources --- .github/workflows/tf-validation.yml | 28 ++++++++++++++++++++++ .gitignore | 37 +++++++++++++++++++++++++++++ .terraform.lock.hcl | 22 +++++++++++++++++ README.md | 4 +++- main.tf | 14 +++++++++++ outputs.tf | 14 +++++++++++ variables.tf | 35 +++++++++++++++++++++++++++ vpc/main.tf | 31 ++++++++++++++++++++++++ vpc/outputs.tf | 14 +++++++++++ vpc/variables.tf | 29 ++++++++++++++++++++++ 10 files changed, 227 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/tf-validation.yml create mode 100644 .gitignore create mode 100644 .terraform.lock.hcl create mode 100644 main.tf create mode 100644 outputs.tf create mode 100644 variables.tf create mode 100644 vpc/main.tf create mode 100644 vpc/outputs.tf create mode 100644 vpc/variables.tf diff --git a/.github/workflows/tf-validation.yml b/.github/workflows/tf-validation.yml new file mode 100644 index 0000000..8e644a7 --- /dev/null +++ b/.github/workflows/tf-validation.yml @@ -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 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..21e6d3c --- /dev/null +++ b/.gitignore @@ -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 \ No newline at end of file diff --git a/.terraform.lock.hcl b/.terraform.lock.hcl new file mode 100644 index 0000000..ee93cef --- /dev/null +++ b/.terraform.lock.hcl @@ -0,0 +1,22 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/google" { + version = "4.51.0" + constraints = "4.51.0" + hashes = [ + "h1:8lpgWoonXz+Y2kM4h/UZEe6W/WZwaga6bhfwmb11grA=", + "zh:001bf7478e495d497ffd4054453c97ab4dd3e6a24d46496d51d4c8094e95b2b1", + "zh:19db72113552dd295854a99840e85678d421312708e8329a35787fff1baeed8b", + "zh:42c3e629ace225a2cb6cf87b8fabeaf1c56ac8eca6a77b9e3fc489f3cc0a9db5", + "zh:50b930755c4b1f8a01c430d8f688ea79de0b0198c87511baa3a783e360d7e624", + "zh:5acd67f0aafff5ad59e179543cccd1ffd48d69b98af0228506403b8d8193b340", + "zh:70128d57b4b4bf07df941172e6af15c4eda8396af5cc2b0128c906983c7b7fad", + "zh:7905fac0ba2becf0e97edfcd4224e57466b04f960f36a3ec654a0a3c2ffececb", + "zh:79b4cc760305cd77c1ff841f789184f808b8052e8f4faa5cb8d518e4c13beb22", + "zh:c7aebd7d7dd2b29de28e382500d36fae8b4d8a192cf05e41ea29c66f1251acfc", + "zh:d8b4494b13ef5af65d3afedf05bf7565918f1e31ad68ae0df81f5c3b12baf519", + "zh:e6e68ef6881bc3312db50c9fd761f226f34d7834b64f90d96616b7ca6b1daf34", + "zh:f569b65999264a9416862bca5cd2a6177d94ccb0424f3a4ef424428912b9cb3c", + ] +} diff --git a/README.md b/README.md index 351ef3a..ed91c9a 100644 --- a/README.md +++ b/README.md @@ -1 +1,3 @@ -# tf-gcp-infra-summer \ No newline at end of file +# tf-gcp-infra-summer + +Enabled GCP APIs: Compute Engine \ No newline at end of file diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..62afcd7 --- /dev/null +++ b/main.tf @@ -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 +} diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..3b4a863 --- /dev/null +++ b/outputs.tf @@ -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 +} diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..e23686f --- /dev/null +++ b/variables.tf @@ -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 +} diff --git a/vpc/main.tf b/vpc/main.tf new file mode 100644 index 0000000..669fbe8 --- /dev/null +++ b/vpc/main.tf @@ -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] +} diff --git a/vpc/outputs.tf b/vpc/outputs.tf new file mode 100644 index 0000000..292a295 --- /dev/null +++ b/vpc/outputs.tf @@ -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 +} diff --git a/vpc/variables.tf b/vpc/variables.tf new file mode 100644 index 0000000..41c644b --- /dev/null +++ b/vpc/variables.tf @@ -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 +}