Louis Sayers
Louis Sayers

Reputation: 2219

GCP - How do you create a URL Redirect path rule with gcloud url-maps?

I want to create a GCP Load Balancer path redirect rule programatically using the gcloud tool.

As a test, I created one manually through the GCP Console web interface.

For my manually created rule, gcloud compute url-maps describe my-url-map returns something that looks like:

creationTimestamp: '2021-02-23T20:26:04.825-08:00'
defaultService: https://www.googleapis.com/compute/v1/projects/my-project/global/backendServices/my-backend-service
fingerprint: abcdefg=
hostRules:
- hosts:
  - blah.my-site.com
  pathMatcher: path-matcher-1
id: '12345678'
kind: compute#urlMap
name: my-url-map
pathMatchers:
- defaultService: https://www.googleapis.com/compute/v1/projects/my-project/global/backendServices/my-backend-service
  name: path-matcher-1
  pathRules:
  - paths:
    - /my-redirect-to-root
    urlRedirect:
      httpsRedirect: false
      pathRedirect: /
      redirectResponseCode: MOVED_PERMANENTLY_DEFAULT
      stripQuery: false
selfLink: https://www.googleapis.com/compute/v1/projects/my-project/global/urlMaps/my-url-map

What I would like to do is to recreate the urlRedirect rule above (redirecting from /my-redirect-to-root to /), but using the gcloud tool.

Looking through the gcloud docs I can't seem to find anything referring to redirects. Is it that this is not possible to do via the gcloud tool? and if not, is there any other solution for creating these redirect rules programatically?

I'm basically trying to get around another GCP issue to do with GCS URLs for static websites by using Load Balancer redirects for each folder in our static site (~400 folders).

Upvotes: 1

Views: 2240

Answers (3)

rustyx
rustyx

Reputation: 85246

gcloud CLI doesn't yet support redirect URL maps. But the urlMaps API does, so you can create it using the url-maps import CLI command like this:

NAME=my-redir
MY_STATIC_IPV4=111.222.33.44
MY_STATIC_IPV6=2600:1111:2222:3333::1

cat <<EOM >map.yaml
defaultUrlRedirect:
  httpsRedirect: true
  redirectResponseCode: MOVED_PERMANENTLY_DEFAULT
kind: compute#urlMap
name: $NAME
EOM

gcloud compute url-maps import $NAME --source=map.yaml --global

rm map.yaml

gcloud compute target-http-proxies create $NAME --url-map=$NAME --global

gcloud compute forwarding-rules create $NAME --target-http-proxy=$NAME --global --ports=80 --address=$MY_STATIC_IPV4

gcloud compute forwarding-rules create $NAME-ipv6 --target-http-proxy=$NAME --global --ports=80 --address=$MY_STATIC_IPV6

Upvotes: 0

DarkDexter25
DarkDexter25

Reputation: 1

Anyone looking to create URL maps programatically with redirect rules using gcloud SDK. The direct feature has not yet been added.

Current workaround is to create a temp-backend service to add in the URL map and then using a config JSON to include a redirect rule,

gcloud compute url-maps import URL_MAP \
  --source redirect_config.json \
  --global

We can clean up the temp backend later,

gcloud compute backend-services delete temp-backend --global

Upvotes: 0

Sergiusz
Sergiusz

Reputation: 1235

Currently Cloud SDK does not support creating url maps with redirects.
If you think that functionality should be available, you can create a Feature Request at Public Issue Tracker to have this option added in future.

For now, you can use API which allows creating url maps with redirects.

Upvotes: 1

Related Questions