All Products
Search
Document Center

Terraform:Manage an SLB instance

Last Updated:Jan 18, 2024

This topic describes how to create a Server Load Balancer (SLB) instance and add a listener in the SLB instance.

Prerequisites

Before you begin, make sure that you have completed the following operations:

Background information

This topic uses an Internet-facing subscription SLB instance named slb_worder that is configured with TCP, UDP, and HTTP listeners as an example.

Procedure

  1. Create an SLB instance.

    1. Create the terraform.tf file, enter the following content, and save the file to the current working directory.

      Note

      You must create an independent working directory for each Terraform project. You must have at least one .tf file before you initialize the configuration files.

      resource "alicloud_slb_load_balancer" "instance" {
        load_balancer_name   = "slb_worder"
        load_balancer_spec   = "slb.s3.small"
        internet_charge_type = "PayByTraffic"
        address_type         = "internet"
      }
    2. Run the terraform apply command to create the SLB instance. If log entries similar to the following ones are displayed, the SLB instance is created.

      alicloud_slb_load_balancer.instance: Creating...
      alicloud_slb_load_balancer.instance: Still creating... [10s elapsed]
      alicloud_slb_load_balancer.instance: Creation complete after 14s [id=lb-gw89kh14ra0htarr1ub8n]
      ......
      Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
    3. Run the terraform show command to view the created SLB instance.

  2. Add listeners in the SLB instance. In this example, an HTTP listener, a UDP listener, and a TCP listener are added.

    1. Create the listener.tf file, enter the following content, and save the file to the current working directory.

      Note

      Terraform will automatically load all of the .tf files in the directory. You can write configuration information to different files as needed.

      resource "alicloud_slb_listener" "tcp" {
        load_balancer_id          = alicloud_slb_load_balancer.instance.id
        backend_port              = "22"
        frontend_port             = "22"
        protocol                  = "tcp"
        bandwidth                 = "10"
        health_check_type         = "tcp"
        persistence_timeout       = 3600
        healthy_threshold         = 8
        unhealthy_threshold       = 8
        health_check_timeout      = 8
        health_check_interval     = 5
        health_check_http_code    = "http_2xx"
        health_check_connect_port = 20
        health_check_uri          = "/console"
        established_timeout       = 600
      }
      
      resource "alicloud_slb_listener" "udp" {
        load_balancer_id          = alicloud_slb_load_balancer.instance.id
        backend_port              = 2001
        frontend_port             = 2001
        protocol                  = "udp"
        bandwidth                 = 10
        persistence_timeout       = 3600
        healthy_threshold         = 8
        unhealthy_threshold       = 8
        health_check_timeout      = 8
        health_check_interval     = 4
        health_check_connect_port = 20
      }
      
      resource "alicloud_slb_listener" "http" {
        load_balancer_id          = alicloud_slb_load_balancer.instance.id
        backend_port              = 80
        frontend_port             = 80
        protocol                  = "http"
        sticky_session            = "on"
        sticky_session_type       = "insert"
        cookie                    = "testslblistenercookie"
        cookie_timeout            = 86400
        health_check              = "on"
        health_check_uri          = "/cons"
        health_check_connect_port = 20
        healthy_threshold         = 8
        unhealthy_threshold       = 8
        health_check_timeout      = 8
        health_check_interval     = 5
        health_check_http_code    = "http_2xx,http_3xx"
        bandwidth                 = 10
        request_timeout           = 80
        idle_timeout              = 30
      }
    2. Run the terraform apply command to add the listeners. If log entries similar to the following ones are displayed, the listeners are added.

      alicloud_slb_listener.udp: Creating...
      alicloud_slb_listener.http: Creating...
      alicloud_slb_listener.tcp: Creating...
      alicloud_slb_listener.http: Creation complete after 5s [id=lb-gw89kh14ra0htarr1ub8n:http:80]
      ......
      Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
    3. Run the terraform show command to view the added listeners.