fbpx

Multai Load Balancer

This post demonstrates how to create a load balancer using the Spotinst Terraform plugin.

This post assumes that you already have a Spotinst account and went through Spotinst Terraform Installation & Configuration.

 

Let’s first extract the token and deployment ID into variables.
Create a file, variables.tfwith the following contents:

variable "spotinst_token" {
  description = "A Permanent API token"
}
variable "spotinst_deployment_id" {
  description = "The deployment ID for API operations"
}

Save the entire configuration to a file named main.tf:

provider "spotinst" {
  token = "${var.spotinst_token}"
}

resource "spotinst_multai_balancer" "foo" {
  name = "foo"

  connection_timeouts {
    idle     = 60
    draining = 60
  }

  tags {
    env = "prod"
    app = "web"
  }
}

resource "spotinst_multai_listener" "foo" {
  balancer_id = "${spotinst_multai_balancer.foo.id}"
  protocol    = "http"
  port        = 1338

  tags {
    env = "prod"
    app = "web"
  }
}

resource "spotinst_multai_routing_rule" "foo" {
  balancer_id = "${spotinst_multai_balancer.foo.id}"
  listener_id = "${spotinst_multai_listener.foo.id}"
  route       = "PathRegexp(`/`)"

  target_set_ids = [
    "${spotinst_multai_target_set.foo.id}",
  ]

  tags {
    env = "prod"
    app = "web"
  }
}

resource "spotinst_multai_target_set" "foo" {
  balancer_id   = "${spotinst_multai_balancer.foo.id}"
  deployment_id = "${var.spotinst_deployment_id}"
  name          = "foo"
  protocol      = "http"
  port          = 1337
  weight        = 1

  health_check {
    protocol            = "http"
    path                = "/"
    interval            = 30
    timeout             = 10
    healthy_threshold   = 2
    unhealthy_threshold = 2
  }

  tags {
    env = "prod"
    app = "web"
  }
}

resource "spotinst_multai_target" "foo" {
  balancer_id   = "${spotinst_multai_balancer.foo.id}"
  target_set_id = "${spotinst_multai_target_set.foo.id}"
  host          = "172.0.0.10"
  port          = "1337"
  weight        = 1

  tags {
    env = "prod"
    app = "web"
  }
}

Once everything is setup correctly, execute your Terraform file and apply the changes.