Skip to main content

Membuat VPC dan VM di GCP dengan Perintah Gcloud

Membuat VPC Network

VPC adalah isolated network untuk setiap customer, GCP VPC Network bisa across region, didalam nya bisa membuat subnetwork untuk masing-masing region, didalam nya ada firewall yang bisa dipasang untuk mengatur/membatasi traffic ke resources yang ada didalam VPC seperti instance.


create-network.sh


#!/bin/bash

# Set your GCP project ID

project_id="your-project"


# Set the VPC network name

vpc_name="your-global-vpc-network"


# Function to create a subnet

create_subnet() {

  local subnet_name=$1

  local ip_range=$2

  local region=$3


 gcloud compute networks subnets create $subnet_name \

    --network $vpc_name \

    --range $ip_range \

    --region $region

}


# Set the project

gcloud config set project $project_id


# Create the VPC network

echo "y" | gcloud compute networks create $vpc_name --subnet-mode custom --project=$project_id


# Vars

subnet_name="your-subnetwork1"

ip_range="10.1.1.0/24"

region="asia-southeast1"  # Singapore region


# Create subnets without prompting

echo "y" | create_subnet $subnet_name $ip_range $region


Sesuain nama project, Ip ranges, subnet sesuai dengan kebutuhan.



Membuat VM Instance


gcloud compute instances create ubuntu-desktop \

    --zone=asia-southeast1-a \

    --machine-type=n1-highmem-2 \

    --image=projects/ubuntu-os-pro-cloud/global/images/ubuntu-pro-2004-focal-v20210720 \

    --network=your-global-vpc-network \

    --subnet=your-subnetwork


Bisa sesuaikan juga nama instance, network diatas.


Comments