From 9274cb9ae0daa7f1e6e771af6e7e6aa6f1ee0b02 Mon Sep 17 00:00:00 2001 From: Robert Wilkins III <1147229+genome21@users.noreply.github.com> Date: Tue, 18 Apr 2023 04:29:42 +0000 Subject: [PATCH] check and create VPC and subnet --- GCP-SETUP.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/GCP-SETUP.md b/GCP-SETUP.md index 1f4eb6b4e..de0ec836d 100644 --- a/GCP-SETUP.md +++ b/GCP-SETUP.md @@ -14,20 +14,23 @@ ZONE="us-central1-a" REGION="us-central1" VPC_NAME="default" SUBNET_NAME="default" +SUBNET_RANGE="10.128.0.0/20" NAT_GATEWAY_NAME="nat-gateway" CLOUD_ROUTER_NAME="nat-client" # Set the GCP project's compute region gcloud config set compute/region $REGION -# Verify the VPC and subnet exist +# Check if the VPC exists, and create it if not vpc_exists=$(gcloud compute networks list --filter="name=$VPC_NAME" --format="value(name)") -subnet_exists=$(gcloud compute networks subnets list --filter="name=$SUBNET_NAME AND region=$REGION" --format="value(name)") +if [[ -z "$vpc_exists" ]]; then + gcloud compute networks create $VPC_NAME --subnet-mode=custom +fi -# Exit with an error message if the VPC or subnet does not exist -if [[ -z "$vpc_exists" || -z "$subnet_exists" ]]; then - echo "Error: VPC '$VPC_NAME' and/or subnet '$SUBNET_NAME' do not exist in region '$REGION'." - exit 1 +# Check if the subnet exists, and create it if not +subnet_exists=$(gcloud compute networks subnets list --filter="name=$SUBNET_NAME AND region=$REGION" --format="value(name)") +if [[ -z "$subnet_exists" ]]; then + gcloud compute networks subnets create $SUBNET_NAME --network=$VPC_NAME --region=$REGION --range=$SUBNET_RANGE fi # Create a firewall rule to allow TCP port 8080 for all instances in the VPC