Create an SLB instance in a VPC by using Terraform
This topic describes how to use Terraform to create a Server Load Balancer (SLB) instance in a virtual private cloud (VPC).
The sample code in this topic is ready to run. You can run the code directly. Run Now
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
Procedure
Create a VPC and a vSwitch. In this example, a VPC and a vSwitch are created in the China (Hangzhou) region.
Create a file named terraform.tf, add the following content to the file, and then save the file in the current directory.
variable "region" { default = "cn-hangzhou" } provider "alicloud" { region = var.region } data "alicloud_zones" "default" { available_resource_creation = "VSwitch" } resource "alicloud_vpc" "main" { vpc_name = "alicloud" # The CIDR block of the VPC cidr_block = "10.1.0.0/21" } resource "alicloud_vswitch" "main" { vpc_id = alicloud_vpc.main.id # The CIDR block of the vSwitch cidr_block = "10.1.0.0/24" # The zone zone_id = data.alicloud_zones.default.zones[0].id }Run terraform apply to create the resources.
Run terraform show to view the created VPC and vSwitch.
Create an SLB instance and add a listener. In this example, a subscription SLB instance and a TCP listener are created.
Add the following content to the terraform.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 = "intranet" vswitch_id = alicloud_vswitch.main.id } resource "alicloud_slb_listener" "listener" { load_balancer_id = alicloud_slb_load_balancer.instance.id backend_port = "2111" frontend_port = "21" protocol = "tcp" bandwidth = "5" }Run terraform apply. The following output indicates that the resources have been successfully created.
alicloud_slb_load_balancer.instance: Creating... alicloud_slb_load_balancer.instance: Still creating... [10s elapsed] alicloud_slb_load_balancer.instance: Creation complete after 10s [id=lb-bp13b3e2m9l8wjwh3y8px] alicloud_slb_listener.listener: Creating... alicloud_slb_listener.listener: Creation complete after 3s [id=lb-bp13b3e2m9l8wjwh3y8px:tcp:21] ...... Apply complete! Resources: 2 added, 0 changed, 0 destroyed.Run terraform show to view the created SLB instance.
Examples
The sample code in this topic is ready to run. You can run the code directly. Run Now
variable "region" {
default = "cn-hangzhou"
}
provider "alicloud" {
region = var.region
}
data "alicloud_zones" "default" {
available_resource_creation = "VSwitch"
}
resource "alicloud_vpc" "main" {
vpc_name = "alicloud"
# The CIDR block of the VPC
cidr_block = "10.1.0.0/21"
}
resource "alicloud_vswitch" "main" {
vpc_id = alicloud_vpc.main.id
# The CIDR block of the vSwitch
cidr_block = "10.1.0.0/24"
# The zone
zone_id = data.alicloud_zones.default.zones[0].id
}
resource "alicloud_slb_load_balancer" "instance" {
load_balancer_name = "slb_worder"
load_balancer_spec = "slb.s3.small"
internet_charge_type = "PayByTraffic"
address_type = "internet"
vswitch_id = alicloud_vswitch.main.id
}
resource "alicloud_slb_listener" "listener" {
load_balancer_id = alicloud_slb_load_balancer.instance.id
backend_port = "2111"
frontend_port = "21"
protocol = "tcp"
bandwidth = "5"
}