diff --git a/mmv1/products/compute/terraform.yaml b/mmv1/products/compute/terraform.yaml index 9d8767aa6b0b..2679f75bc11f 100644 --- a/mmv1/products/compute/terraform.yaml +++ b/mmv1/products/compute/terraform.yaml @@ -939,6 +939,23 @@ overrides: !ruby/object:Overrides::ResourceOverrides post_create: templates/terraform/post_create/labels.erb GlobalForwardingRule: !ruby/object:Overrides::Terraform::ResourceOverride examples: + - !ruby/object:Provider::Terraform::Examples + name: "external_cnd_lb_with_backend_bucket" + primary_resource_id: "default" + vars: + cdn_network: "cdn-network" + cdn_subnet: "cdn-subnet" + cdn_static_ip: "cdn-static-ip" + cdn_forwarding_rule: "cdn-forwarding-rule" + cdn_target_http_proxy: "cdn-target-http-proxy" + cdn_url_map: "cdn-url-map" + image_backend_bucket: "image-backend-bucket" + cdn_backend_storage_bucket: "cdn-backend-storage-bucket" + min_version: beta + ignore_read_extra: + - "port_range" + - "target" + - "ip_address" - !ruby/object:Provider::Terraform::Examples name: "external_ssl_proxy_lb_mig_backend" primary_resource_id: "default" diff --git a/mmv1/templates/terraform/examples/external_cnd_lb_with_backend_bucket.tf.erb b/mmv1/templates/terraform/examples/external_cnd_lb_with_backend_bucket.tf.erb new file mode 100644 index 000000000000..08d0b1a34e38 --- /dev/null +++ b/mmv1/templates/terraform/examples/external_cnd_lb_with_backend_bucket.tf.erb @@ -0,0 +1,137 @@ +# CDN load balancer with Cloud bucket as backend + +# [START cloudloadbalancing_cdn_with_backend_bucket] +# VPC +resource "google_compute_network" "default" { + name = "<%= ctx[:vars]['cdn_network'] %>" + provider = google-beta + auto_create_subnetworks = false +} + +# backend subnet +resource "google_compute_subnetwork" "default" { + name = "<%= ctx[:vars]['cdn_subnet'] %>" + provider = google-beta + ip_cidr_range = "10.0.1.0/24" + region = "us-central1" + network = google_compute_network.default.id +} + +# reserve IP address +resource "google_compute_global_address" "default" { + provider = google-beta + name = "<%= ctx[:vars]['cdn_static_ip'] %>" +} + +# forwarding rule +resource "google_compute_global_forwarding_rule" "<%= ctx[:primary_resource_id] %>" { + name = "<%= ctx[:vars]['cdn_forwarding_rule'] %>" + provider = google-beta + ip_protocol = "TCP" + load_balancing_scheme = "EXTERNAL" + port_range = "80" + target = google_compute_target_http_proxy.default.id + ip_address = google_compute_global_address.default.id +} + +# http proxy +resource "google_compute_target_http_proxy" "default" { + name = "<%= ctx[:vars]['cdn_target_http_proxy'] %>" + provider = google-beta + url_map = google_compute_url_map.default.id +} + +# url map +resource "google_compute_url_map" "default" { + name = "<%= ctx[:vars]['cdn_url_map'] %>" + provider = google-beta + default_service = google_compute_backend_bucket.default.id +} + +# backend bucket with CDN policy with default ttl settings +resource "google_compute_backend_bucket" "default" { + name = "<%= ctx[:vars]['image_backend_bucket'] %>" + description = "Contains beautiful images" + bucket_name = google_storage_bucket.default.name + enable_cdn = true + cdn_policy { + cache_mode = "CACHE_ALL_STATIC" + client_ttl = 3600 + default_ttl = 3600 + max_ttl = 86400 + negative_caching = true + serve_while_stale = 86400 + } +} + +# cdn backend bucket +resource "google_storage_bucket" "default" { + name = "<%= ctx[:vars]['cdn_backend_storage_bucket'] %>" + location = "US" + uniform_bucket_level_access = true + // delete bucket and contents on destroy. + force_destroy = true + // Assign specialty files + website { + main_page_suffix = "index.html" + not_found_page = "404.html" + } +} + +# make bucket public +resource "google_storage_bucket_iam_member" "default" { + bucket = google_storage_bucket.default.name + role = "roles/storage.objectViewer" + member = "allUsers" +} + +resource "google_storage_bucket_object" "index_page" { + name = "index.html" + source = "index.html" + bucket = google_storage_bucket.default.name + depends_on = [local_file.index_page] +} + +resource "google_storage_bucket_object" "error_page" { + name = "404.html" + source = "404.html" + bucket = google_storage_bucket.default.name + depends_on = [local_file.error_page] +} + +# image object for testing, try to access http:///test.jpg +resource "google_storage_bucket_object" "test_image" { + name = "test.jpg" + source = "test.jpg" + content_type = "image/jpeg" + bucket = google_storage_bucket.default.name + depends_on = [null_resource.test_image] +} + +# cdn sample index page +resource "local_file" "index_page" { + filename = "index.html" + content = <<-EOT + +

Congratulations on setting up Google Cloud CDN with Storage backend!

+ + EOT +} + +# cdn default error page +resource "local_file" "error_page" { + filename = "404.html" + content = <<-EOT + +

404 Error: Object you are looking for is no longer available!

+ + EOT +} + +# cdn sample image +resource "null_resource" "test_image" { + provisioner "local-exec" { + command = "wget -O test.jpg https://upload.wikimedia.org/wikipedia/commons/c/c8/Thank_you_001.jpg" + } +} +# [END cloudloadbalancing_cdn_with_backend_bucket]