GCP Load balancer Redirects

In this post you’ll learn how to setup path redirects for a GCP Global Load balancer using a simple example that can be easily applied to your use case.

Cheat Sheet:

The following commands illustrate the basic steps to pull (export from GCP), edit, and push (import to GCP) your load balancer config.

MY_GLOBAL_LB_NAME=<your-lb-name>
# Step 1: pull the yaml and save it as lb-map-config.yaml
gcloud compute url-maps export $MY_GLOBAL_LB_NAME --destination=lb-map-config.yaml --global

# Step 2: Edit the yaml ... (see example below)

# Step 3: push the yaml back to GCP
gcloud compute url-maps import $MY_GLOBAL_LB_NAME --global --source=lb-map-config.yaml

Example with redirects:

defaultService: https://www.googleapis.com/compute/v1/projects/...
hostRules:
- hosts:
   - example.com
  pathMatcher: path-matcher-2
kind: compute#urlMap
name: my-global-lb
pathMatchers:
- defaultService: https://www.googleapis.com/compute/v1/projects/...
  name: path-matcher-2
  pathRules:
  - paths:
    - /hi/*
    service: https://www.googleapis.com/compute/v1/projects/...
#######################
### REDIRECTS BEGIN ###
#######################
  - paths:
    - /hello/*
    - /hello
    urlRedirect:
      pathRedirect: "/hi/"
#####################
### REDIRECTS END ###
#####################
selfLink: https://www.googleapis.com/compute/v1/projects/...

References:

The following links will give you some background information.

Acknowledgements:

I’d like to acknowledge Joey and Joe for tag teaming with me for the underlying sleuthing.

Scroll to Top