Skip to content

Commit

Permalink
Add example and test
Browse files Browse the repository at this point in the history
  • Loading branch information
q2w committed Oct 24, 2024
1 parent 44cf5c2 commit 5040b24
Show file tree
Hide file tree
Showing 19 changed files with 821 additions and 281 deletions.
16 changes: 16 additions & 0 deletions build/int.cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,22 @@ steps:
- id: user-managed-google-managed-ssl-example-teardown
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'cft test run TestAll/examples/user-managed-google-managed-ssl --stage teardown --verbose']
# separate frontend and backend module for http load balancer
- id: apply lb-http-separate-frontend-and-backend
waitFor:
- init-all
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'cft test run TestSeparateFrontendAndBackend --stage apply --verbose']
- id: verify lb-http-separate-frontend-and-backend
waitFor:
- apply lb-http-separate-frontend-and-backend
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'sleep 720 && cft test run TestSeparateFrontendAndBackend --stage verify --verbose']
- id: teardown lb-http-separate-frontend-and-backend
waitFor:
- verify lb-http-separate-frontend-and-backend
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'cft test run TestSeparateFrontendAndBackend --stage teardown --verbose']
tags:
- 'ci'
- 'integration'
Expand Down
99 changes: 99 additions & 0 deletions examples/lb-http-separate-frontend-and-backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# HTTP Load Balancer Example - Separate modules for creating HTTP load balancer frontend and backend resources

[![button](http://gstatic.com/cloudssh/images/open-btn.png)](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/terraform-google-lb-http&working_dir=examples/lb-http-separate-frontend-and-backend&page=shell&tutorial=README.md)

This example creates a global HTTP forwarding rule to forward traffic to instance groups in the us-west1 and us-east1 regions. The `google_compute_backend_service` and its dependencies are created as part of `lb-http-backend` module.
The forwarding rules and its dependecies are created as part of `lb-http-frontend` modules.

## Change to the example directory

```
[[ `basename $PWD` != lb-http-separate-frontend-and-backend ]] && cd examples/lb-http-separate-frontend-and-backend
```

## Install Terraform

1. Install Terraform if it is not already installed (visit [terraform.io](https://terraform.io) for other distributions):

## Set up the environment

1. Set the project, replace `YOUR_PROJECT` with your project ID:

```
PROJECT=YOUR_PROJECT
```

```
gcloud config set project ${PROJECT}
```

2. Configure the environment for Terraform:

```
[[ $CLOUD_SHELL ]] || gcloud auth application-default login
export GOOGLE_PROJECT=$(gcloud config get-value project)
```

## Run Terraform

```
terraform init
terraform apply
```

## Test load balancing

1. Open the URL of the load balancer in your browser:

```
echo http://$(terraform output load-balancer-ip)
```

You should see the instance details from the region closest to you.

## Test forwarding to the other region

1. Change the size of the instance group that’s currently serving requests to zero, so that traffic is forwarded to the group in the other region.

For example, if requests are being served from group1 (us-west1), resize group1 to zero. You can do this using the [`gcloud` CLI](https://cloud.google.com/sdk/gcloud/reference/compute/instance-groups/managed/resize) or the [web console](https://cloud.google.com/compute/docs/instance-groups/creating-groups-of-managed-instances#resize_managed_group).

```
gcloud compute instance-groups managed resize lb-http-separate-frontend-and-backend-group1-mig --size=0 --region=us-west1
```
It may take a few minutes for the instance group to be resized.

**Note**: This change will cause your infrastructure to _drift_ from the Terraform state. You can run `terraform apply` at any time to update the infrastructure to match the Terraform state.

2. Open the external load-balancer IP address again, and verify that you see responses from the instance group in the other region.

```
echo http://$(terraform output load-balancer-ip)| sed 's/"//g'
```

## Cleanup

1. Remove all resources created by terraform:

```
terraform destroy
```

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| group1\_region | n/a | `string` | `"us-west1"` | no |
| group2\_region | n/a | `string` | `"us-east1"` | no |
| network\_prefix | n/a | `string` | `"lb-http-separate-frontend-and-backend"` | no |
| project\_id | n/a | `string` | n/a | yes |
| target\_size | n/a | `number` | `2` | no |

## Outputs

| Name | Description |
|------|-------------|
| load-balancer-ip | n/a |
| load-balancer-ipv6 | The IPv6 address of the load-balancer, if enabled; else "undefined" |

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
117 changes: 117 additions & 0 deletions examples/lb-http-separate-frontend-and-backend/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

resource "google_compute_network" "default" {
name = var.network_prefix
auto_create_subnetworks = "false"
}

resource "google_compute_subnetwork" "group1" {
name = "${var.network_prefix}-group1"
ip_cidr_range = "10.126.0.0/20"
network = google_compute_network.default.self_link
region = var.group1_region
private_ip_google_access = true
}

# Router and Cloud NAT are required for installing packages from repos (apache, php etc)
resource "google_compute_router" "group1" {
name = "${var.network_prefix}-gw-group1"
network = google_compute_network.default.self_link
region = var.group1_region
}

module "cloud-nat-group1" {
source = "terraform-google-modules/cloud-nat/google"
version = "~> 5.0"
router = google_compute_router.group1.name
project_id = var.project_id
region = var.group1_region
name = "${var.network_prefix}-cloud-nat-group1"
}

resource "google_compute_subnetwork" "group2" {
name = "${var.network_prefix}-group2"
ip_cidr_range = "10.127.0.0/20"
network = google_compute_network.default.self_link
region = var.group2_region
private_ip_google_access = true
}

# Router and Cloud NAT are required for installing packages from repos (apache, php etc)
resource "google_compute_router" "group2" {
name = "${var.network_prefix}-gw-group2"
network = google_compute_network.default.self_link
region = var.group2_region
}

module "cloud-nat-group2" {
source = "terraform-google-modules/cloud-nat/google"
version = "~> 5.0"
router = google_compute_router.group2.name
project_id = var.project_id
region = var.group2_region
name = "${var.network_prefix}-cloud-nat-group2"
}

module "lb-http-backend" {
source = "terraform-google-modules/lb-http/google//modules/lb-http-backend"
version = "~> 12.0"
project_id = var.project_id
name = "backend-lb"
target_tags = [
"${var.network_prefix}-group1",
module.cloud-nat-group1.router_name,
"${var.network_prefix}-group2",
module.cloud-nat-group2.router_name
]
firewall_networks = [google_compute_network.default.name]
protocol = "HTTP"
port_name = "http"
timeout_sec = 10
enable_cdn = false

health_check = {
request_path = "/"
port = 80
}

log_config = {
enable = true
sample_rate = 1.0
}

groups = [
{
group = module.mig1.instance_group
},
{
group = module.mig2.instance_group
},
]

iap_config = {
enable = false
}
}

module "lb-http-frontend" {
source = "terraform-google-modules/lb-http/google//modules/lb-http-frontend"
version = "~> 12.0"
project_id = var.project_id
name = "frontend-lb"
url_map_input = module.lb-http-backend.backend_service_info
}
93 changes: 93 additions & 0 deletions examples/lb-http-separate-frontend-and-backend/mig.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

provider "google" {
project = var.project_id
}

provider "google-beta" {
project = var.project_id
}

data "template_file" "group-startup-script" {
template = file(format("%s/gceme.sh.tpl", path.module))

vars = {
PROXY_PATH = ""
}
}

module "mig1_template" {
source = "terraform-google-modules/vm/google//modules/instance_template"
version = "~> 12.0"
network = google_compute_network.default.self_link
subnetwork = google_compute_subnetwork.group1.self_link
service_account = {
email = ""
scopes = ["cloud-platform"]
}
name_prefix = "${var.network_prefix}-group1"
startup_script = data.template_file.group-startup-script.rendered
source_image_family = "ubuntu-2004-lts"
source_image_project = "ubuntu-os-cloud"
tags = [
"${var.network_prefix}-group1",
module.cloud-nat-group1.router_name
]
}

module "mig1" {
source = "terraform-google-modules/vm/google//modules/mig"
version = "~> 12.0"
instance_template = module.mig1_template.self_link
region = var.group1_region
hostname = "${var.network_prefix}-group1"
target_size = var.target_size
named_ports = [{
name = "http",
port = 80
}]
}

module "mig2_template" {
source = "terraform-google-modules/vm/google//modules/instance_template"
version = "~> 12.0"
network = google_compute_network.default.self_link
subnetwork = google_compute_subnetwork.group2.self_link
service_account = {
email = ""
scopes = ["cloud-platform"]
}
name_prefix = "${var.network_prefix}-group2"
startup_script = data.template_file.group-startup-script.rendered
tags = [
"${var.network_prefix}-group2",
module.cloud-nat-group2.router_name
]
}

module "mig2" {
source = "terraform-google-modules/vm/google//modules/mig"
version = "~> 12.0"
instance_template = module.mig2_template.self_link
region = var.group2_region
hostname = "${var.network_prefix}-group2"
target_size = var.target_size
named_ports = [{
name = "http",
port = 80
}]
}
24 changes: 24 additions & 0 deletions examples/lb-http-separate-frontend-and-backend/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

output "load-balancer-ip" {
value = module.lb-http-frontend.external_ip
}

output "load-balancer-ipv6" {
value = module.lb-http-frontend.ipv6_enabled ? module.lb-http-frontend.external_ipv6_address : "undefined"
description = "The IPv6 address of the load-balancer, if enabled; else \"undefined\""
}
Loading

0 comments on commit 5040b24

Please sign in to comment.