All Products
Search
Document Center

Terraform:Manage an SLB instance

Last Updated:Jul 03, 2026

Create a Server Load Balancer (SLB) instance and add TCP, UDP, and HTTP listeners by using Terraform.

Prerequisites

Before you begin, ensure that you have:

Overview

This example creates an Internet-facing subscription SLB instance named slb_worder with TCP, UDP, and HTTP listeners.

Procedure

  1. Create an SLB instance.

    1. Create a terraform.tf file in your working directory with the following content:

      Note

      Each Terraform project requires its own working directory with at least one .tf file.

      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 terraform apply to create the SLB instance. Output similar to the following indicates success:

      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 terraform show to verify the SLB instance.

  2. Add TCP, UDP, and HTTP listeners to the SLB instance.

    1. Create a listener.tf file in your working directory with the following content:

      Note

      Terraform automatically loads all .tf files in the directory, so you can split configurations across multiple files.

      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 terraform apply to add the listeners. Output similar to the following indicates success:

      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 terraform show to verify the listeners.