From 97e995b906faa46b2ad7ff1c5a831e31b30cce21 Mon Sep 17 00:00:00 2001 From: Gregory Date: Wed, 12 Apr 2023 16:23:57 -0700 Subject: [PATCH] init repo --- main.tf | 34 +++++++++++++++++++++++++ modules/compute/main.tf | 48 +++++++++++++++++++++++++++++++++++ modules/compute/outputs.tf | 7 +++++ modules/compute/variables.tf | 7 +++++ modules/firewall/main.tf | 31 ++++++++++++++++++++++ modules/firewall/outputs.tf | 0 modules/firewall/variables.tf | 1 + modules/vpc/outputs.tf | 7 +++++ modules/vpc/variables.tf | 15 +++++++++++ outputs.tf | 9 +++++++ terraform.tfvars | 9 +++++++ variables.tf | 8 ++++++ 12 files changed, 176 insertions(+) create mode 100644 main.tf create mode 100644 modules/compute/main.tf create mode 100644 modules/compute/outputs.tf create mode 100644 modules/compute/variables.tf create mode 100644 modules/firewall/main.tf create mode 100644 modules/firewall/outputs.tf create mode 100644 modules/firewall/variables.tf create mode 100644 modules/vpc/outputs.tf create mode 100644 modules/vpc/variables.tf create mode 100644 outputs.tf create mode 100644 terraform.tfvars create mode 100644 variables.tf diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..28ee58d --- /dev/null +++ b/main.tf @@ -0,0 +1,34 @@ +provider "google" { + credentials = file(var.credentials_file) + project = var.project_id + region = var.region +} + +module "firewall" { + source = "./modules/firewall" + network_name = var.network_name + depends_on = [ + module.vpc + ] +} + +module "compute" { + source = "./modules/compute" + project_id = var.project_id + region = var.region + zone = var.zone + instance_type = var.instance_type + image_name = var.image_name + network_name = var.network_name + subnets_cidr_list = var.subnets_cidr_list + depends_on = [ + module.vpc + ] +} + +module "vpc" { + source = "./modules/vpc" + network_name = var.network_name + subnets_cidr_list = var.subnets_cidr_list + region = var.region + } \ No newline at end of file diff --git a/modules/compute/main.tf b/modules/compute/main.tf new file mode 100644 index 0000000..9233a97 --- /dev/null +++ b/modules/compute/main.tf @@ -0,0 +1,48 @@ +# Create the first VM in the first zone +resource "google_compute_instance" "vm1" { + name = "vm1" + machine_type = var.instance_type + zone = var.zone[0] + tags = ["allow-ssh","allow-internal"] + boot_disk { + initialize_params { + image = var.image_name + } + } + network_interface { + network = var.network_name + subnetwork = "subnet-1" + access_config { + // Use ephemeral IP address + nat_ip = null + } + } + + metadata_startup_script = "apt-get update && apt-get install -y rsync" + +} + +# Create the second VM in the second zone +resource "google_compute_instance" "vm2" { + name = "vm2" + machine_type = var.instance_type + zone = var.zone[1] + tags = ["allow-ssh","allow-internal"] + boot_disk { + initialize_params { + image = var.image_name + } + } + network_interface { + network = var.network_name + subnetwork = "subnet-2" + access_config { + // Use ephemeral IP address + nat_ip = null + } + } + + metadata_startup_script = "apt-get update && apt-get install -y rsync" + +} + diff --git a/modules/compute/outputs.tf b/modules/compute/outputs.tf new file mode 100644 index 0000000..d7bc248 --- /dev/null +++ b/modules/compute/outputs.tf @@ -0,0 +1,7 @@ +output "vm1_ip" { + value = google_compute_instance.vm1.network_interface[*].access_config[*].nat_ip +} + +output "vm2_ip" { + value = google_compute_instance.vm2.network_interface[*].access_config[*].nat_ip +} \ No newline at end of file diff --git a/modules/compute/variables.tf b/modules/compute/variables.tf new file mode 100644 index 0000000..a7e71b7 --- /dev/null +++ b/modules/compute/variables.tf @@ -0,0 +1,7 @@ +variable "image_name" {} +variable "instance_type" {} +variable "zone" {} +variable "project_id" {} +variable "region" {} +variable "network_name" {} +variable "subnets_cidr_list" {} diff --git a/modules/firewall/main.tf b/modules/firewall/main.tf new file mode 100644 index 0000000..929ac0b --- /dev/null +++ b/modules/firewall/main.tf @@ -0,0 +1,31 @@ +resource "google_compute_firewall" "allow_internal" { + name = "allow-internal" + network = var.network_name + source_ranges = ["0.0.0.0/0"] + allow { + protocol = "icmp" + } + allow { + protocol = "tcp" + ports = ["0-65535"] + } + allow { + protocol = "udp" + ports = ["0-65535"] + } + target_tags = ["allow-internal"] + +} + +resource "google_compute_firewall" "allow-ssh" { + name = "allow-ssh" + network = var.network_name + source_ranges = ["0.0.0.0/0"] + allow { + protocol = "tcp" + ports = ["22"] + } + target_tags = ["allow-ssh"] + + +} \ No newline at end of file diff --git a/modules/firewall/outputs.tf b/modules/firewall/outputs.tf new file mode 100644 index 0000000..e69de29 diff --git a/modules/firewall/variables.tf b/modules/firewall/variables.tf new file mode 100644 index 0000000..aa6291a --- /dev/null +++ b/modules/firewall/variables.tf @@ -0,0 +1 @@ +variable "network_name" {} \ No newline at end of file diff --git a/modules/vpc/outputs.tf b/modules/vpc/outputs.tf new file mode 100644 index 0000000..7621961 --- /dev/null +++ b/modules/vpc/outputs.tf @@ -0,0 +1,7 @@ +output "vpc_network_name" { + value = google_compute_network.vpc_network.name +} + +output "vpc_subnets" { + value = google_compute_subnetwork.vpc_subnets.*.self_link +} \ No newline at end of file diff --git a/modules/vpc/variables.tf b/modules/vpc/variables.tf new file mode 100644 index 0000000..fa292de --- /dev/null +++ b/modules/vpc/variables.tf @@ -0,0 +1,15 @@ +variable "network_name" { + type = string + description = "Name of the VPC network to create" +} + +variable "subnets_cidr_list" { + type = list(string) + default = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24", "10.0.4.0/24"] + description = "List of CIDR blocks for the subnets to create within the VPC network" +} + +variable "region" { + type = string + description = "Region where the VPC subnets will be created" +} diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..40575b5 --- /dev/null +++ b/outputs.tf @@ -0,0 +1,9 @@ +output "vpc_subnets_out" { + value = module.vpc.vpc_subnets +} +output "vm1_public_ip" { + value = module.compute.vm1_ip +} +output "vm2_public_ip" { + value = module.compute.vm2_ip +} \ No newline at end of file diff --git a/terraform.tfvars b/terraform.tfvars new file mode 100644 index 0000000..77012b8 --- /dev/null +++ b/terraform.tfvars @@ -0,0 +1,9 @@ +credentials_file = "credentials.json" +project_id = "linux-copy-demo" +region = "us-central1" +zone = ["us-central1-a", "us-central1-b", "us-central1-c"] +image_name = "debian-cloud/debian-11" +instance_type = "n1-standard-1" +network_name = "my-vpc" + +subnets_cidr_list= ["10.0.1.0/24","10.0.2.0/24","10.0.3.0/24","10.0.4.0/24"] \ No newline at end of file diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..454bccd --- /dev/null +++ b/variables.tf @@ -0,0 +1,8 @@ +variable "credentials_file" {} +variable "project_id" {} +variable "region" {} +variable "image_name" {} +variable "instance_type" {} +variable "zone" {type=list} +variable "network_name" {} +variable "subnets_cidr_list" { type=list}