|
| 1 | +/** |
| 2 | + * Copyright 2022 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +# Terraform Registry: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/network_services_edge_cache_service |
| 18 | +# Google Cloud Documentation |
| 19 | +# 1. https://cloud.google.com/media-cdn/docs/quickstart#create-service |
| 20 | +# 2. https://cloud.google.com/media-cdn/docs/origins#cloud-storage-origins |
| 21 | + |
| 22 | +# [START mediacdn_dynamic_compression_parent_tag] |
| 23 | +resource "random_id" "unique_suffix" { |
| 24 | + byte_length = 8 |
| 25 | +} |
| 26 | + |
| 27 | +resource "google_storage_bucket" "default" { |
| 28 | + name = "my-bucket-${random_id.unique_suffix.hex}" |
| 29 | + location = "US" |
| 30 | + force_destroy = true |
| 31 | + uniform_bucket_level_access = true |
| 32 | +} |
| 33 | + |
| 34 | +resource "google_network_services_edge_cache_origin" "default" { |
| 35 | + name = "cloud-storage-origin" |
| 36 | + origin_address = "gs://my-bucket-${random_id.unique_suffix.hex}" |
| 37 | +} |
| 38 | + |
| 39 | +resource "google_network_services_edge_cache_service" "default" { |
| 40 | + name = "cloud-media-service" |
| 41 | + routing { |
| 42 | + host_rule { |
| 43 | + hosts = ["googlecloudexample.com"] |
| 44 | + path_matcher = "routes" |
| 45 | + } |
| 46 | + path_matcher { |
| 47 | + name = "routes" |
| 48 | + route_rule { |
| 49 | + description = "a default route rule, priority=10 (low)" |
| 50 | + priority = 10 |
| 51 | + match_rule { |
| 52 | + prefix_match = "/" |
| 53 | + } |
| 54 | + origin = google_network_services_edge_cache_origin.default.name |
| 55 | + route_action { |
| 56 | + cdn_policy { |
| 57 | + cache_mode = "CACHE_ALL_STATIC" |
| 58 | + default_ttl = "3600s" |
| 59 | + } |
| 60 | + } |
| 61 | + header_action { |
| 62 | + response_header_to_add { |
| 63 | + header_name = "x-cache-status" |
| 64 | + header_value = "{cdn_cache_status}" |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + # [START mediacdn_dynamic_compression_route] |
| 69 | + route_rule { |
| 70 | + description = "a route rule with dynamic compression, priority=2 (high)" |
| 71 | + priority = 2 |
| 72 | + match_rule { |
| 73 | + path_template_match = "/**.m3u8" # HLS playlists |
| 74 | + } |
| 75 | + match_rule { |
| 76 | + path_template_match = "/**.mpd" # DASH manifests |
| 77 | + } |
| 78 | + origin = google_network_services_edge_cache_origin.default.name |
| 79 | + route_action { |
| 80 | + cdn_policy { |
| 81 | + cache_mode = "FORCE_CACHE_ALL" |
| 82 | + client_ttl = "300s" |
| 83 | + } |
| 84 | + compression_mode = "AUTOMATIC" |
| 85 | + } |
| 86 | + header_action { |
| 87 | + response_header_to_add { |
| 88 | + header_name = "x-cache-status" |
| 89 | + header_value = "{cdn_cache_status}" |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + # [END mediacdn_dynamic_compression_route] |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | +# [END mediacdn_dynamic_compression_parent_tag] |
0 commit comments