Create an SLB instance in a VPC by using Terraform

更新时间:
复制 MD 格式

This topic describes how to use Terraform to create a Server Load Balancer (SLB) instance in a virtual private cloud (VPC).

Note

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:

Procedure

  1. Create a VPC and a vSwitch. In this example, a VPC and a vSwitch are created in the China (Hangzhou) region.

    1. 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
      }
    2. Run terraform apply to create the resources.

    3. Run terraform show to view the created VPC and vSwitch.

  2. Create an SLB instance and add a listener. In this example, a subscription SLB instance and a TCP listener are created.

    1. 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"
      }
    2. 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.
    3. Run terraform show to view the created SLB instance.

Examples

Note

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"
}