Demo Hands on creating complete infrastructure stack using gcloud CLI
Step-by-step gcloud CLI walkthrough to provision a GCP infrastructure stack including service account and IAM, VPC, subnet, firewall rules, a private Compute Engine VM, and cleanup.
Hello and welcome back.In this walkthrough we’ll provision a simple, production-like infrastructure stack in Google Cloud using only the gcloud CLI from Cloud Shell. The steps cover service account creation and IAM bindings, networking (VPC, subnet, firewall), and launching a Compute Engine VM that uses the service account. All commands shown are executed in Cloud Shell — replace the sample project kodekloud-gcp-training with your target project ID where needed.
We will:
Create a service account.
Grant the service account necessary IAM roles.
Create a VPC, a subnet, and firewall rules.
Launch a Compute Engine instance using the service account (no external IP).
Verify resources and clean them up when finished.
You don’t need to memorize every gcloud flag. Learn the command structure and reference the gcloud CLI docs: https://cloud.google.com/sdk/gcloud/reference. Use this guide as a step-by-step reference for common infra tasks.
Note: If your project has policy bindings with conditions, gcloud may prompt to choose a condition (e.g., choose 2 for None unless you need a specific condition).
Example creation output includes the network name and mode. Instances on the new network require firewall rules to be reachable — we’ll add those next.Verify the network:
Allow SSH (22), HTTP (80), and HTTPS (443) ingress from anywhere to instances on demo-vpc. Create one rule per port for clarity and minimal privileges.Allow SSH:
Create a VM named demo-vm in zone us-central1-a that uses the demo-sa service account and is attached to demo-subnet without an external IP (private-only instance):
WARNING: You have selected a disk size of under [200GB]. This may result in poor I/O performance. For more information, see: https://developers.google.com/compute/docs/disks#performance.Created [https://www.googleapis.com/compute/v1/projects/kodekloud-gcp-training/zones/us-central1-a/instances/demo-vm].NAME: demo-vmZONE: us-central1-aMACHINE_TYPE: e2-microINTERNAL_IP: 10.0.1.2EXTERNAL_IP:STATUS: RUNNING
Verify instance details (note the zone format us-central1-a):
You can also confirm using the Compute Engine page in the GCP Console.
Cleanup is important to avoid unexpected charges. Delete resources in reverse order of creation and confirm prompts. If you plan to keep resources, consider applying labels and budgets to manage costs.
This guide demonstrated how to provision a basic infrastructure stack using only gcloud CLI commands: creating a service account, assigning IAM roles, creating a custom VPC/subnet, adding firewall rules, and launching a private Compute Engine VM using the service account. For production environments, automate and parameterize these steps using scripts or infrastructure-as-code tools such as Terraform.Further reading and references: