All Products
Search
Document Center

Enterprise Distributed Application Service:Use Terraform to create an EDAS ECS cluster and deploy an application

Last Updated:Mar 13, 2026

Use Terraform to create an Elastic Compute Service (ECS) cluster in Enterprise Distributed Application Service (EDAS), deploy a Java application, and bind a Classic Load Balancer (CLB) instance -- all defined as infrastructure-as-code.

Note

Run the complete example from this tutorial directly in Terraform Explorer.

What you will build

By the end of this tutorial, your Terraform configuration provisions the following resources in sequence:

  1. Networking and compute -- A Virtual Private Cloud (VPC), vSwitch, security group, and ECS instance.

  2. EDAS cluster -- An ECS cluster with the ECS instance attached.

  3. Application -- An EDAS application and deploy group.

  4. Deployment -- The application scaled out and deployed with a demo JAR package.

  5. Load balancing -- A CLB instance bound to the application.

Prerequisites

Before you begin, make sure that you have:

Required IAM policy

Attach the following policy to your RAM user. It grants the minimum permissions for each service used in this tutorial.

{
  "Version": "1",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "edas:CreateCluster",
        "edas:ReadCluster",
        "edas:DeleteCluster",
        "edas:ListResourceGroup",
        "edas:ListServiceGroups",
        "edas:ListSwimmingLaneGroup",
        "edas:ReadApplication",
        "edas:ListSlb",
        "edas:DeleteApplication"
      ],
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "ecs:CreateSecurityGroup",
        "ecs:ModifySecurityGroupPolicy",
        "ecs:DescribeSecurityGroups",
        "ecs:ListTagResources",
        "ecs:DeleteSecurityGroup",
        "ecs:DescribeSecurityGroupAttribute",
        "ecs:RunInstances",
        "ecs:DescribeInstances",
        "ecs:DescribeUserData",
        "ecs:DescribeInstanceRamRole",
        "ecs:DescribeInstanceAttribute",
        "ecs:DescribeNetworkInterfaces",
        "ecs:DescribeInstanceMaintenanceAttributes",
        "ecs:DescribeDisks",
        "ecs:DeleteInstance"
      ],
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "edas:ManageCluster",
        "edas:SynchronizeResource",
        "edas:CreateApplication",
        "edas:ManageApplication",
        "edas:QueryMigrateEcuList",
        "edas:ReadApplication"
      ],
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "slb:CreateLoadBalancer",
        "slb:DescribeLoadBalancerAttribute",
        "slb:ListTagResources",
        "slb:DeleteLoadBalancer"
      ],
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "vpc:CreateVpc",
        "vpc:DeleteVpc",
        "vpc:CreateVSwitch",
        "vpc:DeleteVSwitch"
      ],
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "vpc:DescribeVpcAttribute",
        "vpc:DescribeRouteTableList",
        "vpc:DescribeVSwitchAttributes"
      ],
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": "kms:CreateKey",
      "Resource": "*"
    }
  ]
}
Note

The resources provisioned in this tutorial incur charges. Release or unsubscribe from any resources you no longer need.

Resources used

Terraform resources

This tutorial uses the following Terraform resources. Each link points to the Terraform Registry documentation.

Resource

Purpose

alicloud_vpc

Creates a VPC

alicloud_security_group

Creates a security group

alicloud_vswitch

Creates a vSwitch

alicloud_instance

Provisions an ECS instance

alicloud_edas_cluster

Creates an ECS cluster in EDAS

alicloud_edas_instance_cluster_attachment

Attaches an ECS instance to a cluster

alicloud_edas_application

Creates an EDAS application

alicloud_edas_deploy_group

Creates a deploy group

alicloud_edas_application_scale

Scales out the application to an ECS instance

alicloud_edas_application_deployment

Deploys the application package

alicloud_slb_load_balancer

Creates a CLB instance

alicloud_edas_slb_attachment

Binds the CLB instance to the application

Step 1: Create an ECS instance

Provision the foundational networking and compute infrastructure: a VPC, security group, vSwitch, and an ECS instance.

  1. Create a working directory and add a file named main.tf with the following configuration:

    Key configuration details:

    Parameter

    Value

    Description

    region

    cn-shanghai

    The region where all resources are created

    instance_type

    ecs.e-c1m1.large

    The ECS instance specification

    vpc_cidr_block

    172.16.0.0/16

    The IP address range for the VPC

    vsw_cidr_block

    172.16.0.0/24

    The IP address range for the vSwitch

    system_disk_category

    cloud_essd

    The disk type for the system disk

       variable "region" {
         default = "cn-shanghai"
       }
    
       variable "instance_type" {
         type    = string
         default = "ecs.e-c1m1.large"
       }
    
       variable "vpc_cidr_block" {
         default = "172.16.0.0/16"
       }
    
       variable "vsw_cidr_block" {
         default = "172.16.0.0/24"
       }
    
       # Demo Spring Cloud Consumer JAR from the official EDAS download page.
       variable "war_url" {
         type    = string
         default = "http://edas-sz.oss-cn-shenzhen.aliyuncs.com/prod/demo/SPRING_CLOUD_CONSUMER.jar"
       }
    
       provider "alicloud" {
         region = var.region
       }
    
       # Look up an availability zone that supports the specified instance type
       # and disk category.
       data "alicloud_zones" "default" {
         available_instance_type     = var.instance_type
         available_resource_creation = "VSwitch"
         available_disk_category     = "cloud_essd"
       }
    
       # Generate a random suffix for globally unique resource names.
       resource "random_integer" "default" {
         min = 10000
         max = 99999
       }
    
       # Create a VPC.
       resource "alicloud_vpc" "vpc" {
         vpc_name   = "vpc-test_${random_integer.default.result}"
         cidr_block = var.vpc_cidr_block
       }
    
       # Create a security group inside the VPC.
       resource "alicloud_security_group" "group" {
         name   = "test_${random_integer.default.result}"
         vpc_id = alicloud_vpc.vpc.id
       }
    
       # Create a vSwitch in the first available zone.
       resource "alicloud_vswitch" "vswitch" {
         vpc_id       = alicloud_vpc.vpc.id
         cidr_block   = var.vsw_cidr_block
         zone_id      = data.alicloud_zones.default.zones[0].id
         vswitch_name = "vswitch-test-${random_integer.default.result}"
       }
    
       # Create an ECS instance.
       # The password below is for demonstration only. For production, use
       # Terraform variables with sensitive = true or a secrets manager.
       resource "alicloud_instance" "instance" {
         availability_zone          = data.alicloud_zones.default.zones[0].id
         security_groups            = alicloud_security_group.group.*.id
         instance_type              = var.instance_type
         system_disk_category       = "cloud_essd"
         system_disk_name           = "test_foo_system_disk_${random_integer.default.result}"
         system_disk_description    = "test_foo_system_disk_description"
         image_id                   = "aliyun_2_1903_x64_20G_alibase_20240628.vhd"
         instance_name              = "test_ecs_${random_integer.default.result}"
         vswitch_id                 = alicloud_vswitch.vswitch.id
         internet_max_bandwidth_out = 10
         password                   = "Terraform@Example"
       }
    
       # Wait for the ECS instance to finish initializing
       # (typically under 60 seconds).
       resource "time_sleep" "example" {
         depends_on      = [alicloud_instance.instance]
         create_duration = "60s"
       }
  2. Initialize the Terraform working directory: Expected output:

       terraform init
       Terraform has been successfully initialized!
  3. Validate the configuration to catch syntax errors or unsupported arguments: Expected output:

       terraform validate
       Success! The configuration is valid.
  4. Preview the execution plan to verify what Terraform will create:

       terraform plan
  5. Apply the configuration. When prompted, type yes and press Enter: Expected output on success:

       terraform apply
       Apply complete! Resources: 6 added, 0 changed, 0 destroyed.
  6. Verify the result. Run terraform show to inspect the created resources: Alternatively, open the ECS console. In the left-side navigation pane, choose Instances & Images > Instances. Select China (Shanghai) in the top navigation bar to view the instance.

       terraform show

    terraform show output for the ECS instance

    ECS console showing the created instance

Step 2: Create an ECS cluster and attach the instance

Create an EDAS ECS cluster and attach the ECS instance from Step 1.

  1. Append the following code to main.tf:

    Resource argument reference:

    Argument

    Value

    Description

    cluster_type

    "2"

    Specifies an ECS cluster. Use "1" for a Swarm cluster

    network_mode

    "2"

    Specifies VPC networking. Use "1" for classic network

    logical_region_id

    var.region

    The region where the cluster is created

       # Create an ECS cluster in EDAS.
       # cluster_type = "2": ECS cluster (as opposed to "1" for Swarm).
       # network_mode = "2": VPC network (as opposed to "1" for classic network).
       resource "alicloud_edas_cluster" "cluster" {
         cluster_name      = "tf-edas-${random_integer.default.result}"
         cluster_type      = "2"
         network_mode      = "2"
         logical_region_id = var.region
         vpc_id            = alicloud_vpc.vpc.id
       }
    
       # Attach the ECS instance to the cluster.
       # depends_on ensures the instance is fully initialized before attachment.
       resource "alicloud_edas_instance_cluster_attachment" "default" {
         depends_on   = [time_sleep.example]
         cluster_id   = alicloud_edas_cluster.cluster.id
         instance_ids = [alicloud_instance.instance.id]
       }
  2. Preview and apply the changes: When prompted, type yes and press Enter. Expected output on success:

       terraform plan
       terraform apply
       Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
  3. Verify the result. Run terraform show to inspect the cluster details: Alternatively, open the EDAS console. In the left-side navigation pane, choose Resource Management > ECS Clusters. Select China (Shanghai) in the top navigation bar. Click the cluster ID to view the cluster details, including the attached ECS instance.

       terraform show

    terraform show output for the ECS cluster

    EDAS console showing the ECS cluster

    Cluster detail page with the attached instance

Step 3: Create an application and a deploy group

Create an EDAS application and a deploy group within the cluster.

  1. Append the following code to main.tf:

       # Create a JAR-type application in the ECS cluster.
       resource "alicloud_edas_application" "app" {
         application_name = "tf-test-app-${random_integer.default.result}"
         cluster_id       = alicloud_edas_cluster.cluster.id
         package_type     = "JAR"
       }
    
       # Create a deploy group for the application.
       # Deploy groups let you manage subsets of instances independently
       # (for example, for canary releases).
       resource "alicloud_edas_deploy_group" "this" {
         app_id     = alicloud_edas_application.app.id
         group_name = "tf-test-group-${random_integer.default.result}"
       }
  2. Preview and apply the changes: When prompted, type yes and press Enter. Expected output on success:

       terraform plan
       terraform apply
       Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
  3. Verify the result. Run terraform show to inspect the application and deploy group: Alternatively, open the EDAS console. In the left-side navigation pane, choose Application Management > Applications. Select China (Shanghai) in the top navigation bar. Click the application name, then click the Instance Information tab to view the deploy group.

       terraform show

    terraform show output for the application

    EDAS console showing the application

    Application instance information tab

Step 4: Scale out and deploy the application

Assign the ECS instance to the deploy group (scale-out), then deploy the demo JAR package.

  1. Append the following code to main.tf: Why time_sleep? EDAS needs time to install the agent on the ECS instance and start the application. The time_sleep resource adds a delay so that dependent resources -- such as the CLB binding in Step 5 -- do not run before the application is ready.

       # Extract the deploy group ID from the composite resource ID.
       locals {
         parts    = split(":", alicloud_edas_deploy_group.this.id)
         group_id = local.parts[2]
       }
    
       # Scale out: assign the ECS instance to the deploy group.
       resource "alicloud_edas_application_scale" "default" {
         app_id       = alicloud_edas_application.app.id
         deploy_group = local.group_id
         ecu_info     = [alicloud_edas_instance_cluster_attachment.default.ecu_map[alicloud_instance.instance.id]]
       }
    
       # Deploy the demo JAR to the application.
       # depends_on ensures scale-out and cluster attachment finish first.
       resource "alicloud_edas_application_deployment" "default" {
         depends_on = [alicloud_edas_application_scale.default, alicloud_edas_instance_cluster_attachment.default]
         app_id     = alicloud_edas_application.app.id
         group_id   = local.group_id
         war_url    = var.war_url
       }
    
       # Wait for the application to start (typically under 60 seconds).
       resource "time_sleep" "example2" {
         depends_on      = [alicloud_edas_application_deployment.default]
         create_duration = "60s"
       }
  2. Preview and apply the changes: When prompted, type yes and press Enter. Expected output on success:

       terraform plan
       terraform apply
       Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
  3. Verify the result. Run terraform show to inspect the deployment: Alternatively, open the EDAS console. In the left-side navigation pane, choose Application Management > Applications. Select China (Shanghai) in the top navigation bar. Click the application name, then click the Instance Information tab to check the deployment status.

       terraform show

    terraform show output for the deployed application

    EDAS console showing deployment details

Step 5: Create a CLB instance and bind it to the application

Create an internal CLB instance and bind it to the application to route traffic through the load balancer.

  1. Append the following code to main.tf:

    Resource argument reference:

    Argument

    Value

    Description

    load_balancer_spec

    slb.s2.small

    The CLB instance specification

    address_type

    intranet

    Creates an internal load balancer accessible only within the VPC

       # Create an internal CLB instance in the same vSwitch.
       resource "alicloud_slb_load_balancer" "default" {
         load_balancer_name = "tf-test-slb-${random_integer.default.result}"
         vswitch_id         = alicloud_vswitch.vswitch.id
         load_balancer_spec = "slb.s2.small"
         address_type       = "intranet"
       }
    
       # Bind the CLB instance to the EDAS application.
       # depends_on ensures the application is fully deployed and started.
       resource "alicloud_edas_slb_attachment" "this" {
         depends_on = [time_sleep.example2]
         app_id     = alicloud_edas_application.app.id
         slb_id     = alicloud_slb_load_balancer.default.id
         slb_ip     = alicloud_slb_load_balancer.default.address
         type       = alicloud_slb_load_balancer.default.address_type
       }
  2. Preview and apply the changes: When prompted, type yes and press Enter. Expected output on success:

       terraform plan
       terraform apply
       Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
  3. Verify the result. Run terraform show to inspect the CLB instance: Alternatively, open the EDAS console. In the left-side navigation pane, choose Application Management > Applications. Select China (Shanghai) in the top navigation bar. Click the application name, then click the Basic Information tab to check the bound CLB instance.

       terraform show

    terraform show output for the CLB instance

    Application basic information with CLB binding

Clean up resources

When you no longer need these resources, delete them to stop incurring charges:

terraform destroy

When prompted, type yes and press Enter.

For more information about terraform destroy, see Common commands.

Complete example

Note

Run this example directly in Terraform Explorer.

The following main.tf file contains all the resources from Steps 1 through 5 in a single configuration.

variable "region" {
  default = "cn-shanghai"
}

variable "instance_type" {
  type    = string
  default = "ecs.e-c1m1.large"
}

variable "vpc_cidr_block" {
  default = "172.16.0.0/16"
}

variable "vsw_cidr_block" {
  default = "172.16.0.0/24"
}

# Demo Spring Cloud Consumer JAR from the official EDAS download page.
variable "war_url" {
  type    = string
  default = "http://edas-sz.oss-cn-shenzhen.aliyuncs.com/prod/demo/SPRING_CLOUD_CONSUMER.jar"
}

provider "alicloud" {
  region = var.region
}

# Extract the deploy group ID from the composite resource ID.
locals {
  parts    = split(":", alicloud_edas_deploy_group.this.id)
  group_id = local.parts[2]
}

data "alicloud_zones" "default" {
  available_instance_type     = var.instance_type
  available_resource_creation = "VSwitch"
  available_disk_category     = "cloud_essd"
}

# Generate a random suffix for globally unique resource names.
resource "random_integer" "default" {
  min = 10000
  max = 99999
}

# Create a VPC.
resource "alicloud_vpc" "vpc" {
  vpc_name   = "vpc-test_${random_integer.default.result}"
  cidr_block = var.vpc_cidr_block
}

# Create a security group inside the VPC.
resource "alicloud_security_group" "group" {
  name   = "test_${random_integer.default.result}"
  vpc_id = alicloud_vpc.vpc.id
}

# Create a vSwitch in the first available zone.
resource "alicloud_vswitch" "vswitch" {
  vpc_id       = alicloud_vpc.vpc.id
  cidr_block   = var.vsw_cidr_block
  zone_id      = data.alicloud_zones.default.zones[0].id
  vswitch_name = "vswitch-test-${random_integer.default.result}"
}

# Create an ECS instance.
# The password below is for demonstration only. For production, use
# Terraform variables with sensitive = true or a secrets manager.
resource "alicloud_instance" "instance" {
  availability_zone          = data.alicloud_zones.default.zones[0].id
  security_groups            = alicloud_security_group.group.*.id
  instance_type              = var.instance_type
  system_disk_category       = "cloud_essd"
  system_disk_name           = "test_foo_system_disk_${random_integer.default.result}"
  system_disk_description    = "test_foo_system_disk_description"
  image_id                   = "aliyun_2_1903_x64_20G_alibase_20240628.vhd"
  instance_name              = "test_ecs_${random_integer.default.result}"
  vswitch_id                 = alicloud_vswitch.vswitch.id
  internet_max_bandwidth_out = 10
  password                   = "Terraform@Example"
}

# Wait for the ECS instance to finish initializing (typically under 60 seconds).
resource "time_sleep" "example" {
  depends_on      = [alicloud_instance.instance]
  create_duration = "60s"
}

# Create an ECS cluster in EDAS.
# cluster_type = "2": ECS cluster (as opposed to "1" for Swarm).
# network_mode = "2": VPC network (as opposed to "1" for classic network).
resource "alicloud_edas_cluster" "cluster" {
  cluster_name      = "tf-edas-${random_integer.default.result}"
  cluster_type      = "2"
  network_mode      = "2"
  logical_region_id = var.region
  vpc_id            = alicloud_vpc.vpc.id
}

# Attach the ECS instance to the cluster.
resource "alicloud_edas_instance_cluster_attachment" "default" {
  depends_on   = [time_sleep.example]
  cluster_id   = alicloud_edas_cluster.cluster.id
  instance_ids = [alicloud_instance.instance.id]
}

# Create a JAR-type application in the ECS cluster.
resource "alicloud_edas_application" "app" {
  application_name = "tf-test-app-${random_integer.default.result}"
  cluster_id       = alicloud_edas_cluster.cluster.id
  package_type     = "JAR"
}

# Create a deploy group for the application.
resource "alicloud_edas_deploy_group" "this" {
  app_id     = alicloud_edas_application.app.id
  group_name = "tf-test-group-${random_integer.default.result}"
}

# Scale out: assign the ECS instance to the deploy group.
resource "alicloud_edas_application_scale" "default" {
  app_id       = alicloud_edas_application.app.id
  deploy_group = local.group_id
  ecu_info     = [alicloud_edas_instance_cluster_attachment.default.ecu_map[alicloud_instance.instance.id]]
}

# Deploy the demo JAR to the application.
resource "alicloud_edas_application_deployment" "default" {
  depends_on = [alicloud_edas_application_scale.default, alicloud_edas_instance_cluster_attachment.default]
  app_id     = alicloud_edas_application.app.id
  group_id   = local.group_id
  war_url    = var.war_url
}

# Wait for the application to start (typically under 60 seconds).
resource "time_sleep" "example2" {
  depends_on      = [alicloud_edas_application_deployment.default]
  create_duration = "60s"
}

# Create an internal CLB instance in the same vSwitch.
resource "alicloud_slb_load_balancer" "default" {
  load_balancer_name = "tf-test-slb-${random_integer.default.result}"
  vswitch_id         = alicloud_vswitch.vswitch.id
  load_balancer_spec = "slb.s2.small"
  address_type       = "intranet"
}

# Bind the CLB instance to the EDAS application.
resource "alicloud_edas_slb_attachment" "this" {
  depends_on = [time_sleep.example2]
  app_id     = alicloud_edas_application.app.id
  slb_id     = alicloud_slb_load_balancer.default.id
  slb_ip     = alicloud_slb_load_balancer.default.address
  type       = alicloud_slb_load_balancer.default.address_type
}

For more complete examples, see the EDAS directory on GitHub.

Related topics