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

Prerequisites

Before you begin, ensure 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" "instance" {
        name                 = "slb_worder"
        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.instance: Creating...
      alicloud_slb.instance: Creation complete after 7s [id=lb-bp1edqadye9xvpb3fvo7u]
      
      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.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.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.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.tcp: Creation complete after 5s (ID: lb-bp1mq8qmsxl96pw61ruja:22)
      alicloud_slb_listener.http: Creation complete after 5s (ID: lb-bp1mq8qmsxl96pw61ruja:80)
      alicloud_slb_listener.udp: Creation complete after 5s (ID: lb-bp1mq8qmsxl96pw61ruja:2001)
      
      Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
    3. Run the terraform show command to view the added listeners.