This topic describes how to create a distributed cluster in master/slave mode and deploy files in the cluster by using Terraform.

Prerequisites

Before you begin, ensure that you have completed the following operations:

Procedure

  1. Write the Terraform code. This topic uses an example of running Terraform in Cloud Shell.
    1. Log on to Cloud Shell.
    2. Declare modules in the main.tf file.
      vim main.tf

      Press the i key to enter the edit mode of vim. Add the following content:

      module "app-deployer" {
        source  = "Starnop/app-deployer/alicloud"
        version = "0.0.1"
      
        region            = var.region ! = "" ? var.region : null
        availability_zone = module.vpc.this_availability_zones[0]
        vpc_id            = module.vpc.this_vpc_id
      
        app_name = "example"
        instance_settings = [
          {
            identifier        = "master"
            description       = "master node"
            hostnamePrefix    = "master"
            ecs_password      = "Example123"
            image_id          = null
            image_owners      = null
            image_name_regex  = null
            instance_type     = null
            cpu               = 2
            memory            = 4
            system_disk_size  = 100
            data_disks        = null
            max_bandwidth_out = 100
            security_groups   = null
            vswitch_id        = module.vpc.this_vswitch_ids[0]
            private_ip        = null
            user_data         = null
            temp_files        = ["temp_files/master_template.file"]
            static_files      = ["static_files/master_static.file"]
            entrypoint        = "echo SUCCESS"
            size              = 2
          },
          {
            identifier        = "worker"
            description       = "worker node"
            hostnamePrefix    = "worker"
            ecs_password      = "Example123"
            image_id          = null
            image_owners      = null
            image_name_regex  = null
            instance_type     = null
            cpu               = 4
            memory            = 8
            system_disk_size  = 100
            data_disks        = null
            max_bandwidth_out = 100
            security_groups   = null
            vswitch_id        = module.vpc.this_vswitch_ids[0]
            private_ip        = null
            temp_files        = ["temp_files/worker_template.file"]
            static_files      = ["static_files/worker_static.file"]
            user_data         = null
            entrypoint        = "echo SUCCESS"
            size              = 3
          }
        ]
      }
      
      module "vpc" {
        source = "alibaba/vpc/alicloud"
      
        region       = var.region ! = "" ? var.region : null
        vpc_name     = "my_vpc"
        vswitch_name = "my_vswitch"
      
        vswitch_cidrs = [
          "172.16.1.0/24",
        ]
      }
      							

      Press the Ecs key to exit the edit mode and enter the command mode. On the command line, enter :wq to save and exit vim.

    3. Define constant parameters in the variables.tf file.
      vim variables.tf

      Press the i key to enter the edit mode of vim. Add the following content:

      variable "region" {
        description = "The region ID used to launch this module resources. If not set, it will be sourced from followed by ALICLOUD_REGION environment variable and profile."
        default     = ""
      }

      Press the Ecs key to exit the edit mode and enter the command mode. On the command line, enter :wq to save and exit vim.

    4. Define output parameters in the outputs.tf file.
      vim outputs.tf

      Press the i key to enter the edit mode of vim. Add the following content:

      output "instances" {
        value = {
          for identifier, instance in module.app-deployer.this_instanceList : identifier => instance
        }
      }

      Press the Ecs key to exit the edit mode and enter the command mode. On the command line, enter :wq to save and exit vim.

    5. Add deployment files.
      • static_files/master_static.file
        mkdir static_files && vim static_files/master_static.file

        Press the i key to enter the edit mode of vim. Add the following content:

        master_static

        Press the Ecs key to exit the edit mode and enter the command mode. On the command line, enter :wq to save and exit vim.

      • static_files/worker_static.file
        mkdir static_files && vim static_files/worker_static.file

        Press the i key to enter the edit mode of vim. Add the following content:

        worker_static

        Press the Ecs key to exit the edit mode and enter the command mode. On the command line, enter :wq to save and exit vim.

      • temp_files/master_template.file
        mkdir temp_files && vim temp_files/master_template.file

        Press the i key to enter the edit mode of vim. Add the following content:

        Instances: "${Instances.master_0.private_ip}"

        Press the Ecs key to exit the edit mode and enter the command mode. On the command line, enter :wq to save and exit vim.

      • temp_files/worker_template.file
        mkdir temp_files && vim temp_files/worker_template.file

        Press the i key to enter the edit mode of vim. Add the following content:

        Instances: "${Instances.master_0.public_ip}"

        Press the Ecs key to exit the edit mode and enter the command mode. On the command line, enter :wq to save and exit vim.

  2. Run the terraform init command to initialize Terraform configuration files.
    terraform init

    A similar output is displayed:

    Initializing modules...
    Downloading Starnop/app-deployer/alicloud 0.0.1 for app-deployer...
    - app-deployer in .terraform/modules/app-deployer/Starnop-terraform-alicloud-app-deployer-8434639
    Downloading alibaba/vpc/alicloud 1.4.2 for vpc...
    - vpc in .terraform/modules/vpc/terraform-alicloud-modules-terraform-alicloud-vpc-7e25cee
    
    Initializing the backend...
    
    Initializing provider plugins...
    - Checking for available provider plugins...
    - Downloading plugin for provider "null" (hashicorp/null) 2.1.2...
    - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.68.0...
    - Downloading plugin for provider "local" (hashicorp/local) 1.4.0...
    
    The following providers do not have any version constraints in configuration,
    so the latest version was installed.
    
    To prevent automatic upgrades to new major versions that may contain breaking
    changes, it is recommended to add version = "..." constraints to the
    corresponding provider blocks in configuration, with the constraint strings
    suggested below.
    
    * provider.local: version = "~> 1.4"
    * provider.null: version = "~> 2.1"
    
    Terraform has been successfully initialized!
    
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.
  3. Run the terraform apply command to create resources.
    terraform apply

    A similar output is displayed:

    module.app-deployer.data.alicloud_zones.zones_ds: Refreshing state...
    module.vpc.data.alicloud_zones.default: Refreshing state...
    module.vpc.data.alicloud_vpcs.this: Refreshing state...
    module.vpc.data.alicloud_route_tables.this: Refreshing state...
    module.app-deployer.data.alicloud_images.images["worker"]: Refreshing state...
    module.app-deployer.data.alicloud_instance_types.ecs_type["master"]: Refreshing state...
    module.app-deployer.data.alicloud_instance_types.ecs_type["worker"]: Refreshing state...
    module.app-deployer.data.alicloud_images.images["master"]: Refreshing state...
    
    An execution plan has been generated and is shown below.
    Resource actions are indicated with the following symbols:
    
    Terraform will perform the following actions:
    
    ...
    
    Plan: 23 to add, 0 to change, 0 to destroy.
    
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value: yes
    
    module.vpc.alicloud_vpc.vpc[0]: Creating...
    ...
    
    Apply complete! Resources: 23 added, 0 changed, 0 destroyed.
    
    Outputs:
    
    instances = {
      "master_0" = {
        "availability_zone" = "cn-chengdu-a"
        "credit_specification" = ""
        "data_disks" = []
        "deletion_protection" = false
        "description" = "OWNER: default\nmaster node"
        "dry_run" = false
        "host_name" = "master0"
    ...
      }
      "master_1" = {
        "availability_zone" = "cn-chengdu-a"
        "credit_specification" = ""
        "data_disks" = []
        "deletion_protection" = false
        "description" = "OWNER: default\nmaster node"
        "dry_run" = false
        "host_name" = "master1"
    ...
      }
      "worker_0" = {
        "availability_zone" = "cn-chengdu-a"
        "credit_specification" = ""
        "data_disks" = []
        "deletion_protection" = false
        "description" = "OWNER: default\nworker node"
        "dry_run" = false
        "host_name" = "worker0"
    ...
      }
      "worker_1" = {
        "availability_zone" = "cn-chengdu-a"
        "credit_specification" = ""
        "data_disks" = []
        "deletion_protection" = false
        "description" = "OWNER: default\nworker node"
        "dry_run" = false
        "host_name" = "worker1"
    ...
      }
      "worker_2" = {
        "availability_zone" = "cn-chengdu-a"
        "credit_specification" = ""
        "data_disks" = []
        "deletion_protection" = false
        "description" = "OWNER: default\nworker node"
        "dry_run" = false
        "host_name" = "worker2"
    ...
      }
    }

    After running the code, you can log on to the server to make sure that the deployed files are stored in the /tmp/example directory.