Skip to content

Instantly share code, notes, and snippets.

@whopiyush
Last active December 6, 2022 06:14
Show Gist options
  • Select an option

  • Save whopiyush/97a47be6bc3280be78c3879957e3b1d8 to your computer and use it in GitHub Desktop.

Select an option

Save whopiyush/97a47be6bc3280be78c3879957e3b1d8 to your computer and use it in GitHub Desktop.
echo "Enter Bucket Name:"
read BUCKET_NAME
echo "Enter Instance Name:"
read INSTANCE_NAME
echo "Enter VPC Name:"
read VPC_NAME
echo "Enter Zone Assigned:"
read ZONE_ASSIGNED
# DMNAME=$(gcloud deployment-manager deployments list --filter=qldm --format="value('name')")
# BUCKET_NAME=$(gcloud deployment-manager deployments describe $DMNAME --format=json | jq -r ".outputs | map(select(.name | contains(\"${VAR1}\") )) | .[].finalValue")
# INSTANCE_NAME=$(gcloud deployment-manager deployments describe $DMNAME --format=json | jq -r ".outputs | map(select(.name | contains(\"${VAR2}\") )) | .[].finalValue")
# VPC_NAME=$(gcloud deployment-manager deployments describe $DMNAME --format=json | jq -r ".outputs | map(select(.name | contains(\"${VAR3}\") )) | .[].finalValue")
echo BUCKET_NAME: $BUCKET_NAME
echo INSTANCE_NAME: $INSTANCE_NAME
echo VPC_NAME: $VPC_NAME
echo ZONE_ASSIGNED: $ZONE_ASSIGNED
# Step 1
touch main.tf
touch variables.tf
mkdir -p modules/instances
touch modules/instances/instances.tf
touch modules/instances/outputs.tf
touch modules/instances/variables.tf
mkdir -p modules/storage
touch modules/storage/storage.tf
touch modules/storage/outputs.tf
touch modules/storage/variables.tf
# Make sure to replace the ${ZONE_ASSIGNED} with the zone assigned to you in the cloud shell.
cat > variables.tf <<EOL
variable "region" {
default = "us-east1"
}
variable "zone" {
default = "${ZONE_ASSIGNED}"
}
variable "project_id" {
default = "${DEVSHELL_PROJECT_ID}"
}
EOL
cat > modules/instances/variables.tf <<EOL
variable "region" {
default = "us-east1"
}
variable "zone" {
default = "${ZONE_ASSIGNED}"
}
variable "project_id" {
default = "${DEVSHELL_PROJECT_ID}"
}
EOL
cat > modules/storage/variables.tf <<EOL
variable "region" {
default = "us-east1"
}
variable "zone" {
default = "${ZONE_ASSIGNED}"
}
variable "project_id" {
default = "${DEVSHELL_PROJECT_ID}"
}
EOL
cat > main.tf << EOL
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "3.55.0"
}
}
}
provider "google" {
project = var.project_id
region = var.region
zone = var.zone
}
module "instances" {
source = "./modules/instances"
}
EOL
terraform init
cat > modules/instances/instances.tf << EOL
resource "google_compute_instance" "tf-instance-1" {
name = "tf-instance-1"
machine_type = "n1-standard-1"
zone = var.zone
metadata_startup_script = <<-EOT
#!/bin/bash
EOT
allow_stopping_for_update = true
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
network = "default"
}
}
resource "google_compute_instance" "tf-instance-2" {
name = "tf-instance-2"
machine_type = "n1-standard-1"
zone = var.zone
metadata_startup_script = <<-EOT
#!/bin/bash
EOT
allow_stopping_for_update = true
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
network = "default"
}
}
EOL
INSTANCE1_ID=$(gcloud compute instances describe tf-instance-1 --zone ${ZONE_ASSIGNED} --format="json(id)" | jq -r .id)
echo $INSTANCE1_ID
INSTANCE2_ID=$(gcloud compute instances describe tf-instance-2 --zone ${ZONE_ASSIGNED} --format="json(id)" | jq -r .id)
echo $INSTANCE2_ID
terraform import module.instances.google_compute_instance.tf-instance-1 $INSTANCE1_ID
terraform import module.instances.google_compute_instance.tf-instance-2 $INSTANCE2_ID
terraform plan
terraform apply -auto-approve
#Step 2
cat > modules/storage/storage.tf << EOL
resource "google_storage_bucket" "storage-bucket" {
name = "${BUCKET_NAME}"
location = "US"
force_destroy = true
uniform_bucket_level_access = true
}
EOL
cat << EOF >> main.tf
module "storage" {
source = "./modules/storage"
}
EOF
terraform init
terraform apply -auto-approve
cat > main.tf << EOL
terraform {
backend "gcs" {
bucket = "${BUCKET_NAME}"
prefix = "terraform/state"
}
required_providers {
google = {
source = "hashicorp/google"
version = "3.55.0"
}
}
}
provider "google" {
project = var.project_id
region = var.region
zone = var.zone
}
module "instances" {
source = "./modules/instances"
}
module "storage" {
source = "./modules/storage"
}
EOL
terraform init
#Step 3
cat > modules/instances/instances.tf << EOL
resource "google_compute_instance" "tf-instance-1" {
name = "tf-instance-1"
machine_type = "n1-standard-2"
zone = var.zone
metadata_startup_script = <<-EOT
#!/bin/bash
EOT
allow_stopping_for_update = true
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
network = "default"
}
}
resource "google_compute_instance" "tf-instance-2" {
name = "tf-instance-2"
machine_type = "n1-standard-2"
zone = var.zone
metadata_startup_script = <<-EOT
#!/bin/bash
EOT
allow_stopping_for_update = true
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
network = "default"
}
}
resource "google_compute_instance" "${INSTANCE_NAME}" {
name = "${INSTANCE_NAME}"
machine_type = "n1-standard-2"
zone = var.zone
allow_stopping_for_update = true
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
network = "default"
}
}
EOL
terraform init
terraform apply -auto-approve
#Step 4
terraform taint module.instances.google_compute_instance.$INSTANCE_NAME
terraform plan
terraform apply -auto-approve
cat > modules/instances/instances.tf << EOL
resource "google_compute_instance" "tf-instance-1" {
name = "tf-instance-1"
machine_type = "n1-standard-2"
zone = var.zone
metadata_startup_script = <<-EOT
#!/bin/bash
EOT
allow_stopping_for_update = true
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
network = "default"
}
}
resource "google_compute_instance" "tf-instance-2" {
name = "tf-instance-2"
machine_type = "n1-standard-2"
zone = var.zone
metadata_startup_script = <<-EOT
#!/bin/bash
EOT
allow_stopping_for_update = true
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
network = "default"
}
}
EOL
terraform apply -auto-approve
# Task 5
cat << EOF >> main.tf
module "vpc" {
source = "terraform-google-modules/network/google"
version = "3.4.0"
project_id = var.project_id
network_name = "${VPC_NAME}"
routing_mode = "GLOBAL"
subnets = [
{
subnet_name = "subnet-01"
subnet_ip = "10.10.10.0/24"
subnet_region = "us-east1"
},
{
subnet_name = "subnet-02"
subnet_ip = "10.10.20.0/24"
subnet_region = "us-east1"
subnet_private_access = "true"
subnet_flow_logs = "true"
description = "This subnet has a description"
}
]
}
EOF
terraform init
terraform apply -auto-approve
cat > modules/instances/instances.tf << EOL
resource "google_compute_instance" "tf-instance-1" {
name = "tf-instance-1"
machine_type = "n1-standard-2"
zone = var.zone
metadata_startup_script = <<-EOT
#!/bin/bash
EOT
allow_stopping_for_update = true
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
network = "${VPC_NAME}"
subnetwork = "subnet-01"
}
}
resource "google_compute_instance" "tf-instance-2" {
name = "tf-instance-2"
machine_type = "n1-standard-2"
zone = var.zone
metadata_startup_script = <<-EOT
#!/bin/bash
EOT
allow_stopping_for_update = true
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
network = "${VPC_NAME}"
subnetwork = "subnet-02"
}
}
EOL
terraform init
terraform apply -auto-approve
# Task 6
cat << EOF >> main.tf
resource "google_compute_firewall" "tf-firewall" {
name = "tf-firewall"
network = "projects/${DEVSHELL_PROJECT_ID}/global/networks/${VPC_NAME}"
allow {
protocol = "tcp"
ports = ["80"]
}
source_tags = ["web"]
source_ranges = ["0.0.0.0/0"]
}
EOF
terraform init
terraform apply -auto-approve
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment