When you deploy a website or an application, you must deploy multiple nodes. The number of nodes can be scaled up or down automatically based on the number of visits or resource usage amount. Server Load Balancer (SLB) can distribute requests to these nodes dynamically. This topic describes how to deploy a web cluster by using Terraform.

Background information

In this example, the entire application is deployed in a single zone and allows access to the Hello World page only through port 8080.

Procedure

  1. Create a VPC and a VSwitch.
    Terraform 0.11 is used in this example.
    Note In Terraform 0.11 and earlier, the example usage of the variable expression is vpc_id = "${alicloud_vpc.vpc.id}". In Terraform 0.12 and later, the example usage of the variable expression is updated to vpc_id = "alicloud_vpc.vpc.id". Use the corresponding variable expression based on your Terraform version.
    1. Create the terraform.tf file, enter the following content, and then save the file to the current working directory.
      resource "alicloud_vpc" "vpc" {
        name       = "tf_test_foo"
        cidr_block = "172.16.0.0/12"
      }
      
      resource "alicloud_vswitch" "vsw" {
        vpc_id            = "${alicloud_vpc.vpc.id}"
        cidr_block        = "172.16.0.0/21"
        availability_zone = "cn-beijing-b"
      }
    2. Run the terraform show command to view the created VPC and VSwitch.
      You can also log on to the VPC console to view the attributes of the VPC and VSwitch.
  2. Create a security group and apply the security group to the created VPC.
    1. In the terraform.tf file, add the following content:
      resource "alicloud_security_group" "default" {
        name = "default"
        vpc_id = "${alicloud_vpc.vpc.id}"
      }
      
      resource "alicloud_security_group_rule" "allow_all_tcp" {
        type              = "ingress"
        ip_protocol       = "tcp"
        nic_type          = "internet"
        policy            = "accept"
        port_range        = "1/65535"
        priority          = 1
        security_group_id = "${alicloud_security_group.default.id}"
        cidr_ip           = "0.0.0.0/0"
      }
    2. Run the terraform show command to view the created security group and added security group rule.
      You can also log on to the ECS console to view the security group and security group rule.
  3. Create an SLB instance and assign a public IP address to it. In this example, a mapping from frontend port 80 to backend port 8080 is configured for the SLB instance and the public IP address of the SLB instance is displayed for subsequent tests.
    1. Create the slb.tf file and add the following content:
      resource "alicloud_slb" "slb" {
        name       = "test-slb-tf"
        vswitch_id = "${alicloud_vswitch.vsw.id}"
        internet = true
      }
      resource "alicloud_slb_listener" "http" {
        load_balancer_id = "${alicloud_slb.slb.id}"
        backend_port = 8080
        frontend_port = 80
        bandwidth = 10
        protocol = "http"
        sticky_session = "on"
        sticky_session_type = "insert"
        cookie = "testslblistenercookie"
        cookie_timeout = 86400
        health_check="on"
        health_check_type = "http"
        health_check_connect_port = 8080
      }
      
      output "slb_public_ip"{
        value = "${alicloud_slb.slb.address}"
      }
    2. Run the terraform show command to view the created SLB instance.
      You can also log on to the SLB console to view the created SLB instance.
  4. Create Auto Scaling resources.
    In this example, the following resources are created:
    • Scaling group: Specify 2 as the minimum number of instances and 10 as the maximum number of instances in the template, and attach the created SLB instance to the scaling group. Because scaling groups depend on SLB listener configurations, you must use the depends_on attribute to specify the deployment sequence in the template.
    • Scaling group configuration: Specify the specific configuration of the ECS instance in the template. The initialization configuration (user-data) generates a Hello World page and provides services over port 8080. To simplify operations, this example assigns a public IP address to the virtual machine and set force_delete to true to subsequently delete the environment.
    • Scaling rule: Define the specific scaling rule.
    1. Create the ess.tf file and add the following content:
      resource "alicloud_ess_scaling_group" "scaling" {
        min_size = 2
        max_size = 10
        scaling_group_name = "tf-scaling"
        vswitch_ids=["${alicloud_vswitch.vsw. *.id}"]
        loadbalancer_ids = ["${alicloud_slb.slb. *.id}"]
        removal_policies   = ["OldestInstance", "NewestInstance"]
        depends_on = ["alicloud_slb_listener.http"]
      }
      
      resource "alicloud_ess_scaling_configuration" "config" {
        scaling_group_id = "${alicloud_ess_scaling_group.scaling.id}"
        image_id = "ubuntu_140405_64_40G_cloudinit_20161115.vhd"
        instance_type = "ecs.n2.small"
        security_group_id = "${alicloud_security_group.default.id}"
        active=true
        enable=true
        user_data = "#! /bin/bash\necho \"Hello, World\" > index.html\nnohup busybox httpd -f -p 8080&"
        internet_max_bandwidth_in=10
        internet_max_bandwidth_out= 10
        internet_charge_type = "PayByTraffic"
        force_delete= true
      
      }
      
      resource "alicloud_ess_scaling_rule" "rule" {
        scaling_group_id = "${alicloud_ess_scaling_group.scaling.id}"
        adjustment_type  = "TotalCapacity"
        adjustment_value = 2
        cooldown = 60
      }
    2. After you confirm that the resources are correct, run the terraform apply command to create the Auto Scaling resources.
      After the resources are created, the public IP address of the SLB instance is displayed.

      After two minutes, Auto Scaling will create an ECS instance.

    3. Enter and run the curl http://<slb public ip> command to verify whether you can access the Hello World page.
      If Hello, World is displayed, you can use the SLB instance to access the web page provided by the ECS instance.
  5. Run the terraform destroy command to delete the test environment. With your confirmation, the entire deployment environment will be deleted.

    You can use Terraform to easily delete environments and deploy new ones. To deploy a new environment, run the terraform apply command.