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:
An Alibaba Cloud account and an AccessKey pair. Go to the AccessKey pair page of the Alibaba Cloud Management Console to create or view your AccessKey pair
Terraform installed and configured. See Install and configure Terraform in the local PC and Create resources with Terraform
Overview
This example creates an Internet-facing subscription SLB instance named slb_worder with TCP, UDP, and HTTP listeners.
Procedure
-
Create an SLB instance.
-
Create a terraform.tf file in your working directory with the following content:
NoteEach 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" } -
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. -
Run terraform show to verify the SLB instance.
-
-
Add TCP, UDP, and HTTP listeners to the SLB instance.
-
Create a listener.tf file in your working directory with the following content:
NoteTerraform 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 } -
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. -
Run terraform show to verify the listeners.
-