Skip to content

Commit

Permalink
Add CDN load balancer with Cloud Storage buckets as backend (#5575) (#…
Browse files Browse the repository at this point in the history
…4007)

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Jan 25, 2022
1 parent 769015f commit d0a6b6b
Show file tree
Hide file tree
Showing 3 changed files with 312 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .changelog/5575.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
```release-note:none
```
165 changes: 165 additions & 0 deletions google-beta/resource_compute_global_forwarding_rule_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,171 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccComputeGlobalForwardingRule_externalCndLbWithBackendBucketExample(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": randString(t, 10),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProvidersOiCS,
CheckDestroy: testAccCheckComputeGlobalForwardingRuleDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeGlobalForwardingRule_externalCndLbWithBackendBucketExample(context),
},
{
ResourceName: "google_compute_global_forwarding_rule.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"network", "port_range", "target", "ip_address"},
},
},
})
}

func testAccComputeGlobalForwardingRule_externalCndLbWithBackendBucketExample(context map[string]interface{}) string {
return Nprintf(`
# CDN load balancer with Cloud bucket as backend
# VPC
resource "google_compute_network" "default" {
name = "tf-test-cdn-network%{random_suffix}"
provider = google-beta
auto_create_subnetworks = false
}
# backend subnet
resource "google_compute_subnetwork" "default" {
name = "tf-test-cdn-subnet%{random_suffix}"
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 = "tf-test-cdn-static-ip%{random_suffix}"
}
# forwarding rule
resource "google_compute_global_forwarding_rule" "default" {
name = "tf-test-cdn-forwarding-rule%{random_suffix}"
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 = "tf-test-cdn-target-http-proxy%{random_suffix}"
provider = google-beta
url_map = google_compute_url_map.default.id
}
# url map
resource "google_compute_url_map" "default" {
name = "tf-test-cdn-url-map%{random_suffix}"
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 = "tf-test-image-backend-bucket%{random_suffix}"
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 = "tf-test-cdn-backend-storage-bucket%{random_suffix}"
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://<your_lb_ip_address>/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
<html><body>
<h1>Congratulations on setting up Google Cloud CDN with Storage backend!</h1>
</body></html>
EOT
}
# cdn default error page
resource "local_file" "error_page" {
filename = "404.html"
content = <<-EOT
<html><body>
<h1>404 Error: Object you are looking for is no longer available!</h1>
</body></html>
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"
}
}
`, context)
}

func TestAccComputeGlobalForwardingRule_externalTcpProxyLbMigBackendExample(t *testing.T) {
t.Parallel()

Expand Down
145 changes: 145 additions & 0 deletions website/docs/r/compute_global_forwarding_rule.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,151 @@ https://cloud.google.com/compute/docs/load-balancing/http/



<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=external_cnd_lb_with_backend_bucket&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## Example Usage - External Cnd Lb With Backend Bucket


```hcl
# CDN load balancer with Cloud bucket as backend
# VPC
resource "google_compute_network" "default" {
name = "cdn-network"
provider = google-beta
auto_create_subnetworks = false
}
# backend subnet
resource "google_compute_subnetwork" "default" {
name = "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 = "cdn-static-ip"
}
# forwarding rule
resource "google_compute_global_forwarding_rule" "default" {
name = "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 = "cdn-target-http-proxy"
provider = google-beta
url_map = google_compute_url_map.default.id
}
# url map
resource "google_compute_url_map" "default" {
name = "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 = "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 = "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://<your_lb_ip_address>/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
<html><body>
<h1>Congratulations on setting up Google Cloud CDN with Storage backend!</h1>
</body></html>
EOT
}
# cdn default error page
resource "local_file" "error_page" {
filename = "404.html"
content = <<-EOT
<html><body>
<h1>404 Error: Object you are looking for is no longer available!</h1>
</body></html>
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"
}
}
```
## Example Usage - External Ssl Proxy Lb Mig Backend


Expand Down

0 comments on commit d0a6b6b

Please sign in to comment.