check and create VPC and subnet
This commit is contained in:
parent
f2a587191b
commit
7195278f2c
1 changed files with 9 additions and 6 deletions
15
GCP-SETUP.md
15
GCP-SETUP.md
|
|
@ -14,20 +14,23 @@ ZONE="us-central1-a"
|
||||||
REGION="us-central1"
|
REGION="us-central1"
|
||||||
VPC_NAME="default"
|
VPC_NAME="default"
|
||||||
SUBNET_NAME="default"
|
SUBNET_NAME="default"
|
||||||
|
SUBNET_RANGE="10.128.0.0/20"
|
||||||
NAT_GATEWAY_NAME="nat-gateway"
|
NAT_GATEWAY_NAME="nat-gateway"
|
||||||
CLOUD_ROUTER_NAME="nat-client"
|
CLOUD_ROUTER_NAME="nat-client"
|
||||||
|
|
||||||
# Set the GCP project's compute region
|
# Set the GCP project's compute region
|
||||||
gcloud config set compute/region $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)")
|
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
|
# Check if the subnet exists, and create it if not
|
||||||
if [[ -z "$vpc_exists" || -z "$subnet_exists" ]]; then
|
subnet_exists=$(gcloud compute networks subnets list --filter="name=$SUBNET_NAME AND region=$REGION" --format="value(name)")
|
||||||
echo "Error: VPC '$VPC_NAME' and/or subnet '$SUBNET_NAME' do not exist in region '$REGION'."
|
if [[ -z "$subnet_exists" ]]; then
|
||||||
exit 1
|
gcloud compute networks subnets create $SUBNET_NAME --network=$VPC_NAME --region=$REGION --range=$SUBNET_RANGE
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Create a firewall rule to allow TCP port 8080 for all instances in the VPC
|
# Create a firewall rule to allow TCP port 8080 for all instances in the VPC
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue