Code Monkey home page Code Monkey logo

terraform-google-load-balancer's Introduction

Load Balancer Terraform Module (Google)

Wrapper around the complexity of configuring a global load balancer on Google's Cloud Platform. It manages all the resources required to direct traffic to the provided sets of buckets or instance groups.

Usage

Please note: Due to the way the dependency graph is currently being evaluated in Terraform, the removal of buckets or services from the load balancer must be done in two steps. Please see Removing a service or bucket for more information on how to do this.

Additionally, removing a load balancer is a two-step process too (and will generate an error). This is shown in Removing a load balancer.

Creating a load balancer

resource google_compute_global_address my_address {
  name = "load-balancer"
}

resource google_compute_instance_group my_group {
  name = "my-group"
  zone = "europe-west4-a"
}

resource google_storage_bucket my_bucket1 {
  name = "my-bucket1"
}

resource google_storage_bucket my_bucket2 {
  name = "my-bucket2"
}

resource google_compute_health_check my_healthcheck {
  name = "my-health-check"
  
  http_health_check {
    
  }
}

module load_balancer {
  source = "garbetjie/load-balancer/google"
  
  name = "load-balancer" 
  default_backend = ["service", 0]  // or ["bucket", 1] for the bucket defined for "2.static.example.org".
  address = google_compute_global_address.my_address.address
  
  buckets = [
    { hosts = ["1.static.example.org"], bucket = google_storage_bucket.my_bucket1.name },
    { hosts = ["2.static.example.org"], bucket = google_storage_bucket.my_bucket2.name },
    { hosts = ["3.static.example.org"], bucket = google_storage_bucket.my_bucket1.name },
  ]
  
  services = [
    { hosts = ["api.example.org", "example.org"], groups = [google_compute_instance_group.my_group.self_link], health_check = google_compute_health_check.my_healthcheck },
    { hosts = ["2.api.example.org", "2.example.org"], groups = [google_compute_instance_group.my_group.self_link], health_check = google_compute_health_check.my_healthcheck },
    { hosts = ["3.api.example.org", "3.example.org"], groups = [google_compute_instance_group.my_group.self_link], health_check = google_compute_health_check.my_healthcheck },
  ]
}

Removing a service or bucket

Firstly, the buckets or services to be removed need to be moved to the end of their configuration, and the hosts to which they're assigned need to be emptied.

Using the same example as in creating a load balancer (shown above), it would become (notice how the 2.static.example.org and ["2.api.example.org", "2.example.org"] hosts have been moved to the end, and emptied):

Step 1: Empty the hosts properties.

module load_balancer {
  source = "garbetjie/load-balancer/google"
  
  name = "load-balancer" 
  default_backend = ["service", 0]
  address = google_compute_global_address.my_address.address
  
  buckets = [
    { hosts = ["1.static.example.org"], bucket = google_storage_bucket.my_bucket1.name },
    { hosts = ["3.static.example.org"], bucket = google_storage_bucket.my_bucket1.name },
    { hosts = [], bucket = google_storage_bucket.my_bucket2.name },
  ]
  
  services = [
    { hosts = ["api.example.org", "example.org"], groups = [google_compute_instance_group.my_group.self_link], health_check = google_compute_health_check.my_healthcheck },
    { hosts = ["3.api.example.org", "3.example.org"], groups = [google_compute_instance_group.my_group.self_link], health_check = google_compute_health_check.my_healthcheck },
    { hosts = [], groups = [google_compute_instance_group.my_group.self_link], health_check = google_compute_health_check.my_healthcheck },
  ]
}

Step 2: Remove the service/bucket definitions.

Once this change has been applied, you can then remove the services or buckets:

module load_balancer {
  source = "garbetjie/load-balancer/google"
  
  name = "load-balancer" 
  default_backend = ["service", 0]
  address = google_compute_global_address.my_address.address
  
  buckets = [
    { hosts = ["1.static.example.org"], bucket = google_storage_bucket.my_bucket1.name },
    { hosts = ["3.static.example.org"], bucket = google_storage_bucket.my_bucket1.name },
  ]
  
  services = [
    { hosts = ["api.example.org", "example.org"], groups = [google_compute_instance_group.my_group.self_link], health_check = google_compute_health_check.my_healthcheck },
    { hosts = ["3.api.example.org", "3.example.org"], groups = [google_compute_instance_group.my_group.self_link], health_check = google_compute_health_check.my_healthcheck },
  ]
}

Removing a load balancer

When removing the load balancer entirely, the same process needs to be followed as when removing a bucket or service - all hosts for the buckets and services need to be set to empty lists.

When this change has been applied, the load balancer can be removed.

Inputs

Name Description Type Default Required
name Name of the load balancer (as displayed in the Google Cloud Console) string n/a Yes
default_backend Backend type and index to select as the default backend. Must be a tuple with the first index being one of bucket or service, and the second the index of the bucket or service designated as the default. tuple(string, number) n/a Yes
address IP address to assign to the load balancer. string n/a Yes
services List of objects used to define the backend services for the load balancer. list(object) [] No
services.hosts HTTP hosts for which traffic should be sent to these groups. list(string) [] No
services.groups selfLinks of the instance groups to which traffic should be sent. list(string) n/a Yes
services.health_check selfLink of a health check to use to determine this service's health. string n/a Yes
buckets List of objects used to define the backend buckets for the load balancer. list(object) [] No
buckets.hosts HTTP hosts for which traffic should be sent to this bucket. list(string) [] No
buckets.bucket The name of the bucket to direct traffic to. string n/a Yes

Outputs

None

terraform-google-load-balancer's People

Contributors

garbetjie avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.