VPC peering in GCP - Commands & Configuration
Start learning this pattern below
Jump into concepts and practice - no test required
resources:
- name: my-vpc-peering-connection
type: compute.v1.networkPeering
properties:
network: projects/my-project/global/networks/my-vpc
peerNetwork: projects/peer-project/global/networks/peer-vpc
name: my-vpc-to-peer-vpc
autoCreateRoutes: true
This YAML file defines a VPC peering connection between two networks.
network: The source VPC network in your project.
peerNetwork: The VPC network in the other project you want to connect to.
name: A name for this peering connection.
autoCreateRoutes: Automatically creates routes so traffic can flow between the networks.
gcloud compute networks peerings create my-vpc-to-peer-vpc --network=my-vpc --peer-project=peer-project --peer-network=peer-vpc --auto-create-routes
--network - Specifies the name of your VPC network.--peer-project - Specifies the project ID of the peer network.--peer-network - Specifies the name of the peer VPC network.--auto-create-routes - Automatically creates routes for network communication.gcloud compute networks peerings list --network=my-vpc
--network - Specifies the VPC network to list peerings for.gcloud compute networks peerings describe my-vpc-to-peer-vpc --network=my-vpc
--network - Specifies your VPC network.If you remember nothing else from this pattern, remember: VPC peering connects two private networks so they can communicate directly and securely without using the public internet.
Practice
What is the main purpose of VPC peering in Google Cloud?
Solution
Step 1: Understand VPC peering concept
VPC peering connects two private networks directly, avoiding the public internet.Step 2: Compare options with concept
Only To connect two private networks securely without using the internet describes secure private network connection without internet.Final Answer:
To connect two private networks securely without using the internet -> Option BQuick Check:
VPC peering = secure private network connection [OK]
- Confusing VPC peering with firewall rules
- Thinking VPC peering provides internet access
- Assuming VPC peering is for backups
Which of the following is the correct command to create a VPC peering connection from net-a to net-b in Google Cloud CLI?
gcloud compute networks peerings create PEERING_NAME --network=NETWORK --peer-network=PEER_NETWORK
Solution
Step 1: Identify correct command syntax
The command requires a peering name, the local network, and the peer network.Step 2: Match parameters to networks
gcloud compute networks peerings create peer-ab --network=net-a --peer-network=net-b correctly uses a peering name and assigns net-a as local network and net-b as peer network.Final Answer:
gcloud compute networks peerings create peer-ab --network=net-a --peer-network=net-b -> Option AQuick Check:
Correct CLI syntax = gcloud compute networks peerings create peer-ab --network=net-a --peer-network=net-b [OK]
- Swapping --network and --peer-network values
- Using network names as peering name
- Omitting required flags
Given two VPC networks net-a and net-b peered together, which of the following statements about routing is true?
1. Each network must create routes to the other's IP ranges.
2. Routes are automatically shared by default.
3. Peering allows communication only if firewall rules permit.
4. Peering replaces the need for VPN connections.
Solution
Step 1: Analyze routing and firewall requirements
VPC peering automatically shares subnet routes by default. Firewall rules still control traffic.Step 2: Evaluate statements
Statement 1 is false (no manual route creation needed). Statements 2 and 3 are true. Statement 4 is not accurate (peering and VPN serve different purposes).Final Answer:
Only statement 2 and 3 are true -> Option AQuick Check:
Routes auto + firewall needed [OK]
- Thinking routes must be manually created
- Ignoring firewall rules in peering
- Thinking peering always replaces VPN
You created a VPC peering between net-a and net-b, but instances in net-a cannot reach instances in net-b. What is the most likely cause?
Solution
Step 1: Check common connectivity issues in VPC peering
Firewall rules must allow traffic between peered networks; blocking rules prevent communication.Step 2: Evaluate other options
Wrong peering name or one-sided peering would prevent peering creation. Overlapping IP ranges prevent peering setup itself.Final Answer:
Firewall rules innet-bblock incoming traffic fromnet-a-> Option DQuick Check:
Firewall blocking = connectivity failure [OK]
- Ignoring firewall rules as cause
- Assuming peering auto-fixes IP conflicts
- Thinking peering is one-sided
You have two VPC networks, net-a with CIDR 10.0.0.0/16 and net-b with CIDR 10.0.0.0/16. You want to peer them to share resources privately. What is the best approach?
Solution
Step 1: Understand CIDR overlap restrictions in VPC peering
VPC peering requires non-overlapping IP ranges to route traffic correctly.Step 2: Choose solution for overlapping CIDRs
Changing one network's CIDR to a non-overlapping range allows peering. VPN or shared VPC are alternatives but not direct peering solutions.Final Answer:
Change one network's CIDR to a non-overlapping range before peering -> Option CQuick Check:
Non-overlapping CIDRs required for peering [OK]
- Trying to peer overlapping CIDRs directly
- Confusing VPN with peering
- Ignoring shared VPC as different concept
