All Products
Search
Document Center

Terraform:Deploy a web cluster

Last Updated:Jan 10, 2024

This topic describes how to deploy a web cluster by using Terraform.

Prerequisites

Before you begin, make sure that you have completed the following operations:

Background information

Before you deploy a website or application, you must deploy a series of nodes. Server Load Balancer (SLB) distributes requests to each node and automatically scales based on the access quantity or resource usage. This example deploys the entire application in a single zone and only allows access to the Hello World page through port 8080.

Procedure

  1. Create a VPC and a VSwitch.

    1. Create the terraform.tf file, enter the following content, and save the file to the current working directory.

      provider "alicloud" {
        region = "cn-beijing"
      }
      resource "alicloud_vpc" "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"
        zone_id    = "cn-beijing-b"
      }
    2. Run the terraform apply command to create a VPC and a VSwitch.

    3. 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 terraform.tf, 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          = "intranet"
        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 apply command to create a security group.

    3. Run the terraform show command to view the created security group and added security group rule.

  3. Create a Server Load Balancer (SLB) instance and assign a public IP address for it. This example configures a mapping from frontend port 80 to backend port 8080 for the SLB instance and displays a public IP address for subsequent tests.

    1. Create the slb.tf file and add the following content:

      resource "alicloud_slb_load_balancer" "listener" {
        load_balancer_name   = "tf-example"
        internet_charge_type = "PayByTraffic"
        address_type         = "internet"
        instance_charge_type = "PayByCLCU"
      }
      
      resource "alicloud_slb_listener" "http" {
        load_balancer_id          = alicloud_slb_load_balancer.listener.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_load_balancer.listener.address
      }
    2. Run the terraform apply command to create an SLB instance.

    3. Run the terraform show command to view the created SLB instance.

  4. Create Auto Scaling resources.

    This example creates the following resources:

    • Scaling group: specifies the minimum number of instances as 2 and the maximum number of instances as 10. The created SLB instance is attached to the scaling group. The depends_on attribute specifies the deployment sequence because the scaling group depends on SLB listener configurations.

    • Scaling group configuration: specifies the specific configuration of the ECS instance. The initialization configuration (user-data) generates a Hello World page and provides services through port 8080. To simplify operations, this example assigns a public IP address for the ECS instance and configures force_delete=true to subsequently delete the environment.

    • Scaling rule: defines 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_load_balancer.listener.*.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_18_04_64_20G_alibase_20190624.vhd"
        instance_type              = "ecs.n4.large"
        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. Run the terraform apply command to create resources.

      After the resources are created, the public IP address of the SLB instance is displayed.

      Auto Scaling will create an ECS instance after two minutes.

    3. Run the curl http://<slb public ip> command to verify the results.

      If Hello, World is displayed, it indicates that you can use the SLB instance to access the webpage provided by the ECS instance.

  5. Run the terraform destroy command to delete the test environment. After you confirm, the entire 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.