Skip to content

Instantly share code, notes, and snippets.

@akiatoji
Last active March 1, 2024 06:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save akiatoji/f393493db57e9dff9b681e2ea111a917 to your computer and use it in GitHub Desktop.
Save akiatoji/f393493db57e9dff9b681e2ea111a917 to your computer and use it in GitHub Desktop.

In GCP Console, create VPC networks named vpn-1 and vpn-2 in us-east1 and europe-west1

In Cloud Shell:

Create VPN gateways

gcloud compute target-vpn-gateways \
create vpn-1 \
--network vpn-network-1  \
--region us-east1

gcloud compute target-vpn-gateways \
create vpn-2 \
--network vpn-network-2  \
--region europe-west1

List VPN gateways

gcloud compute target-vpn-gateways list

Reserve static IPs

gcloud compute addresses create --region us-east1 vpn-1-static-ip
gcloud compute addresses create --region europe-west1 vpn-2-static-ip

Make note of addresses

gcloud compute addresses list

Create forwarding rules for static IP -> VPN GW

For ESP, UDP500 and UDP4500 protocols

gcloud compute \
forwarding-rules create vpn-1-esp \
--region us-east1  \
--ip-protocol ESP  \
--address $STATIC_IP_VPN_1 \
--target-vpn-gateway vpn-1

gcloud compute \
forwarding-rules create vpn-2-esp \
--region europe-west1  \
--ip-protocol ESP  \
--address $STATIC_IP_VPN_2 \
--target-vpn-gateway vpn-2

gcloud compute \
forwarding-rules create vpn-1-udp500  \
--region us-east1 \
--ip-protocol UDP \
--ports 500 \
--address $STATIC_IP_VPN_1 \
--target-vpn-gateway vpn-1

gcloud compute \
forwarding-rules create vpn-2-udp500  \
--region europe-west1 \
--ip-protocol UDP \
--ports 500 \
--address $STATIC_IP_VPN_2 \
--target-vpn-gateway vpn-2

gcloud compute \
forwarding-rules create vpn-1-udp4500  \
--region us-east1 \
--ip-protocol UDP --ports 4500 \
--address $STATIC_IP_VPN_1 \
--target-vpn-gateway vpn-1

gcloud compute \
forwarding-rules create vpn-2-udp4500  \
--region europe-west1 \
--ip-protocol UDP --ports 4500 \
--address $STATIC_IP_VPN_2 \
--target-vpn-gateway vpn-2

Verify above in VPC Network -> External IP addreses

Create Tunnels

Network 1 to network 2

gcloud compute \
vpn-tunnels create tunnel1to2  \
--peer-address $STATIC_IP_VPN_2 \
--region us-east1 \
--ike-version 2 \
--shared-secret gcprocks \
--target-vpn-gateway vpn-1 \
--local-traffic-selector 0.0.0.0/0 \
--remote-traffic-selector 0.0.0.0/0

Network 2 to network 1

gcloud compute \
vpn-tunnels create tunnel2to1 \
--peer-address $STATIC_IP_VPN_1 \
--region europe-west1 \
--ike-version 2 \
--shared-secret gcprocks \
--target-vpn-gateway vpn-2 \
--local-traffic-selector 0.0.0.0/0 \
--remote-traffic-selector 0.0.0.0/0

Verify

gcloud compute vpn-tunnels list

Create Static Routes

VPN's are now peered, but there are no routes between the two yet. Run below to add

gcloud compute  \
routes create route1to2  \
--network vpn-network-1 \
--next-hop-vpn-tunnel tunnel1to2 \
--next-hop-vpn-tunnel-region us-east1 \
--destination-range 10.1.3.0/24

gcloud compute  \
routes create route2to1  \
--network vpn-network-2 \
--next-hop-vpn-tunnel tunnel2to1 \
--next-hop-vpn-tunnel-region europe-west1 \
--destination-range 10.5.4.0/24

Now you should be able to ping internal addresses in each VPN-network from another.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment