Multai & Terraform Getting Started – Create Multai Load Balancer

How to install Multai plugin for Terraform

In this post, we will demonstrate how to create a load balancer using Multai Terraform plugin.

This post assumes that you already have a Multai account.

1. Download the binary file terraform-provider-multai.

Please download the proper binary file for your operating system and architecture and put it somewhere on your filesystem:

2. Configure Terraform to be able to find the binary file:

If you are on a Unix-like system, create a file named .terraformrc in your home directory:

~/.terraformrc

If you are on a Windows system, create a file named terraform.rc in the %APPDATA% directory:

%APPDATA%/terraform.rc

3. Edit the file and add the following content:

providers {
  multai = "/path/to/terraform-provider-multai"
}

4. Create a new Multai Load Balancer.

Let’s first extract our token, project ID and deployment ID into a few variables. Create a file, variables.tf with the following contents.

variable "multai_token" {
  description = "The API token for API operations"
}

variable "multai_project_id" {
  description = "The project ID for API operations"
}
variable "multai_deployment_id" {
  description = "The deployment ID for API operations"
}

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

provider "multai" {
  token = "${var.multai_token}"
}

resource "multai_balancer" "lb-tf" {
  project_id = "${var.multai_project_id}"
  name       = "lb-tf"

  conn_timeouts {
    idle     = 10
    draining = 10
  }

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

resource "multai_balancer_listener" "ls-tf" {
  project_id  = "${var.multai_project_id}"
  balancer_id = "${multai_balancer.lb-tf.id}"
  protocol    = "http"
  port        = 5050

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

resource "multai_balancer_routing_rule" "rr-tf" {
  project_id  = "${var.multai_project_id}"
  balancer_id = "${multai_balancer.lb-tf.id}"
  listener_id = "${multai_balancer_listener.ls-tf.id}"
  route       = "Path(`/foo`)"

  target_set_ids = [
    "${multai_balancer_target_set.ts-tf.id}",
  ]

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

resource "multai_balancer_target_set" "ts-tf" {
  project_id    = "${var.multai_project_id}"
  deployment_id = "${var.multai_deployment_id}"
  balancer_id   = "${multai_balancer.lb-tf.id}"
  name          = "azure"
  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 "multai_balancer_target" "t1-tf" {
  project_id    = "${var.multai_project_id}"
  balancer_id   = "${multai_balancer.lb-tf.id}"
  target_set_id = "${multai_balancer_target_set.ts-tf.id}"
  host          = "172.0.0.10"
  weight        = 1

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

resource "multai_balancer_target" "t2" {
  project_id    = "${var.multai_project_id}"
  balancer_id   = "${multai_balancer.lb-tf.id}"
  target_set_id = "${multai_balancer_target_set.ts-tf.id}"
  host          = "172.0.0.11"
  port          = 1338
  weight        = 1

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

Once you have everything setup correctly, you can execute your Terraform file and apply the changes. It should trigger an API call to Multai, and create a Load Balancer.

If you have any question or a comment, please feel free to reach us at support@spotinst.com

Best,
The Spotinst Team.