全部產品
Search
文件中心

Enterprise Distributed Application Service:通過Terraform建立ECS叢集並部署應用

更新時間:Sep 22, 2026

本文為您介紹如何使用Terraform完成建立ECS叢集、部署應用、綁定SLB、建立應用分組及擴容等操作。

說明

本教程所含範例程式碼支援一鍵運行,您可以直接運行代碼。一鍵運行

前提條件

  • 完成此教程,您需要開通以下服務:

    • Enterprise Distributed Application Service:如果需要開通此服務,請參見開通EDAS。

    • 負載平衡:請參見CLB計費概述。

    • Elastic Compute Service:請參見計費概述。

  • 為了降低資訊安全風險,建議使用最小許可權的RAM使用者完成此教程的操作。請參見建立RAM使用者與管理RAM使用者的許可權,完成此教程所需最小許可權的權限原則如下:

    {
      "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": "*"
        }
      ]
    }
  • 準備Terraform運行環境,您可以選擇以下任一方式來使用Terraform。

    • Explorer:阿里雲提供了Terraform的線上運行環境,您無需安裝Terraform,登入後即可線上使用和體驗Terraform。適用於零成本、快速、便捷地體驗和調試Terraform的情境。

    • 使用Terraform快速建立資源:阿里雲Cloud Shell中預裝了Terraform的組件,並已配置好身份憑證,您可直接在Cloud Shell中運行Terraform的命令。適用於低成本、快速、便捷地訪問和使用Terraform的情境。

    • 在本地安裝和配置Terraform:適用於網路連接較差或需要自訂開發環境的情境。

說明

本教程樣本包含的部分資源會產生一定費用,請在不需要時及時進行釋放或退訂。

使用的資源

步驟一:建立ECS

  1. 建立一個工作目錄,並在該工作目錄中建立名為main.tf的設定檔。以下代碼將建立一個ECS執行個體,以及建立ECS執行個體所需的VPC、安全性群組與交換器資源。將以下代碼複製到main.tf中。

    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地址
    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
    }
    data "alicloud_zones" "default" {
      available_instance_type     = var.instance_type
      available_resource_creation = "VSwitch"
      available_disk_category     = "cloud_essd"
    }
    # 隨機數
    resource "random_integer" "default" {
      min = 10000
      max = 99999
    }
    # 建立Virtual Private Cloud
    resource "alicloud_vpc" "vpc" {
      vpc_name   = "vpc-test_${random_integer.default.result}"
      cidr_block = var.vpc_cidr_block
    }
    # 建立安全性群組
    resource "alicloud_security_group" "group" {
      name   = "test_${random_integer.default.result}"
      vpc_id = alicloud_vpc.vpc.id
    }
    # 建立交換器
    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}"
    }
    # 建立ECS
    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"
    }
    # 建立ecs執行個體成功之後等待執行個體初始化完成,一般不超過60s
    resource "time_sleep" "example" {
      depends_on      = [alicloud_instance.instance]
      create_duration = "60s"
    }
  2. 執行如下命令,初始化Terraform運行環境。

    terraform init

    返回資訊如下,則Terraform初始化成功。

    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. 執行如下命令,開始執行代碼。

    terraform apply

    在執行過程中,根據提示輸入yes並按下Enter鍵,等待命令執行完成,若出現以下資訊,則表示代碼執行成功。

    You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.
    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
    Apply complete! Resources: 6 added, 0 changed, 0 destroyed.
  4. 驗證結果。

    執行terraform show命令

    您可以在工作目錄中,使用以下命令查詢Terraform已建立資源的詳細資料:

    terraform show
    shell@Alicloud:~/edas$ terraform show
    # alicloud_instance.instance:
    resource "alicloud_instance" "instance" {
        availability_zone              = "cn-shanghai-b"
        cpu                            = 2
        create_time                    = "2024-11-07T02:08Z"
        deletion_protection            = false
        dry_run                        = false
        enable_jumbo_frame             = false
        expired_time                   = "2099-12-31T15:59Z"
        host_name                      = "iZuf651njxxx"
        http_put_response_hop_limit    = 0
        id                             = "i-uf651njmoxxx"
        image_id                       = "aliyun_2_1903_x64_20G_alibase_20240628.vhd"
        instance_charge_type           = "PostPaid"
        instance_name                  = "test_ecs_xxx"
        instance_type                  = "ecs.e-c1m1.large"
        internet_charge_type           = "PayByTraffic"
        internet_max_bandwidth_in      = 200
        internet_max_bandwidth_out     = 10
        ipv6_address_count             = 0
        ipv6_addresses                 = []
        maintenance_action             = "AutoRecover"
        maintenance_notify             = false
        ...
    }
        memory                         = 2048
        network_interface_id           = "eni-uf61r0ja5xxx"
        network_interface_traffic_mode = "Standard"
        os_name                        = "Alibaba Cloud Linux  2.1903 LTS 64位 "
        os_type                        = "linux"
        password                       = (sensitive value)
        primary_ip_address             = "172.16.xxx"
        private_ip                     = "172.16.xxx"
        public_ip                      = "xxx"
        queue_pair_number              = 0
        secondary_private_ip_address_count = 0
        secondary_private_ips          = []
        security_groups                = [
            "sg-xxx",
        ]
        spot_duration                  = 0
        spot_price_limit               = 0
        spot_strategy                  = "NoSpot"
        start_time                     = "2024-11-07T02:08Z"
        status                         = "Running"
        stopped_mode                   = "Not-applicable"
        subnet_id                      = "vsw-uf6m03nh7cfxxx"
        system_disk_category           = "cloud_essd"
    }

    登入控制台查看

    登入Enterprise Distributed Application Service(EDAS),進入執行個體與鏡像 > 執行個體頁面,左上方選擇地區,此例中選擇华东2(上海),查看已建立的ECS執行個體。

    執行個體列表中顯示一台名為 test_ecs_ 的執行個體,狀態為運行中,可用性區域為華東2(上海)B,配置為 2核(vCPU) 2 GiB 10 Mbps,規格為 ecs.e-c1m1.large,付費方式為隨用隨付。

步驟二:建立ECS叢集並添加ECS

  1. 在main.tf檔案中增加如下代碼。

    # 建立ECS叢集
    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
    }
    # 添加ECS執行個體到ECS叢集
    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. 建立執行計畫,並預覽變更。

    terraform plan
  3. 執行如下命令,開始執行代碼。

    terraform apply

    在執行過程中,根據提示輸入yes並按下Enter鍵,等待命令執行完成,若出現以下資訊,則表示代碼執行成功。

    Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
  4. 驗證結果。

    執行terraform show命令

    您可以在工作目錄中,使用以下命令查詢Terraform已建立資源的詳細資料:

    terraform show
    shell@Alicloud:~/edas$ terraform show
    # alicloud_edas_cluster.cluster:
    resource "alicloud_edas_cluster" "cluster" {
        cluster_name      = "tf-edas-xxx"
        cluster_type      = 2
        id                = "be02e4e1-6720-401b-xxx"
        logical_region_id = "cn-shanghai"
        network_mode      = 2
        vpc_id            = "vpc-uf6m7ujhvxxx"
    }
    # alicloud_edas_instance_cluster_attachment.default:
    resource "alicloud_edas_instance_cluster_attachment" "default" {
        cluster_id         = "be02e4e1-6720-401b-xxx"
        cluster_member_ids = {
            "i-uf651njmo50xxx" = "d86b2524-9c27-4647-xxx"
        }
        ecu_map            = {
            "i-uf651njmo50xxx" = "855c7d64-add2-4e50-xxx"
        }
        id                 = "be02e4e1-6720-401b-bc54-xxx:i-uf651njmo50xxx"
        instance_ids       = [
            "i-uf651njmo50xxx",
        ]
        status_map         = {
            "i-uf651njmo50xxx" = 1
        }
    }

    登入控制台查看

    1. 登入Enterprise Distributed Application Service(EDAS),進入Resource Management > EDAS ECS 叢集頁面,左上方選擇地區,此例中選擇华东2(上海),查看已建立的ECS叢集。

      頁面顯示已建立的ECS叢集 tf-edas,叢集狀態為正常,執行個體數為1,資源量為CPU 2核、記憶體2 GiB。

    2. 單擊此此教程建立的ECS叢集ID,查看叢集詳情。

      叢集詳情頁包含以下資訊: - 叢集資訊:顯示叢集歸屬(阿里雲)、叢集ID、叢集名稱、微服務空間(cn-shanghai)、叢集類型(ECS叢集)、VPC ID、網路類型(專用網路)等欄位。 - ECS執行個體:已加入一台ECS執行個體,規格為2核CPU/2 GiB記憶體,健全狀態檢查狀態為運行中,已部署應用為N/A。 - 應用列表:暫無資料。

步驟三:建立應用與應用分組

  1. 在main.tf檔案中增加如下代碼。

    # 建立應用
    resource "alicloud_edas_application" "app" {
      application_name = "tf-test-app-${random_integer.default.result}"
      cluster_id       = alicloud_edas_cluster.cluster.id
      package_type     = "JAR"
    }
    # 建立應用分組
    resource "alicloud_edas_deploy_group" "this" {
      app_id     = alicloud_edas_application.app.id
      group_name = "tf-test-group-${random_integer.default.result}"
    }
  2. 建立執行計畫,並預覽變更。

    terraform plan
  3. 執行如下命令,開始執行代碼。

    terraform apply

    在執行過程中,根據提示輸入yes並按下Enter鍵,等待命令執行完成,若出現以下資訊,則表示代碼執行成功。

    Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
  4. 驗證結果。

    執行terraform show命令

    您可以在工作目錄中,使用以下命令查詢Terraform已建立資源的詳細資料:

    terraform show
    Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
    shell@Alicloud:~/edas$ terraform show
    # alicloud_edas_application.app:
    resource "alicloud_edas_application" "app" {
        application_name = "tf-test-app-xxx"
        build_pack_id    = 0
        cluster_id       = "be02e4e1-6720-401b-bc54-xxx"
        id               = "f7807d2d-b1a6-47a9-94c4-xxx"
        package_type     = "JAR"
    }
    # alicloud_edas_cluster.cluster:
    resource "alicloud_edas_cluster" "cluster" {
        cluster_name      = "tf-edas-xxx"
        cluster_type      = 2
        id                = "be02e4e1-6720-401b-bc54-xxx"
        logical_region_id = "cn-shanghai"
        network_mode      = 2
        vpc_id            = "vpc-uf6m7ujhv2noxxx"
    }
    # alicloud_edas_deploy_group.this:
    resource "alicloud_edas_deploy_group" "this" {
        app_id     = "f7807d2d-b1a6-47a9-94c4-xxx"
        group_name = "tf-test-group-xxx"
        group_type = 1
        id         = "f7807d2d-b1a6-47a9-94c4-xxx:tf-test-group-64914:78ddbcb5-b6b4-4344-a42a-xxx"
    }

    登入控制台查看

    1. 登入Enterprise Distributed Application Service(EDAS),進入應用管理 > 應用列表頁面,左上方選擇地區,此例中選擇华东2(上海),查看已建立的應用。

      應用列表中顯示已建立的應用 tf-test-app-xxx,所屬微服務空間為預設微服務空間,應用狀態為已停止,叢集類型為 ECS叢集,應用運行環境為標準Java應用,運行執行個體數和執行個體總數均為 0。

    2. 單擊此例建立應用的应用名称,進入实例部署信息頁簽,查看應用的分組資訊。

      頁面顯示預設分組和通過 Terraform 建立的自訂分組 tf-test-group-xxx,兩個分組均無執行個體部署。

步驟四:應用擴容並部署

  1. 在main.tf檔案中增加如下代碼。

    # 截取應用分組ID
    locals {
      parts    = split(":", alicloud_edas_deploy_group.this.id)
      group_id = local.parts[2]
    }
    # 應用擴容
    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]]
    }
    # 部署應用
    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
    }
    # 部署應用成功後等待應用啟動,一般不超過60s
    resource "time_sleep" "example2" {
      depends_on      = [alicloud_edas_application_deployment.default]
      create_duration = "60s"
    }
  2. 建立執行計畫,並預覽變更。

    terraform plan
  3. 執行如下命令,開始執行代碼。

    terraform apply

    在執行過程中,根據提示輸入yes並按下Enter鍵,等待命令執行完成,若出現以下資訊,則表示代碼執行成功。

    Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
  4. 驗證結果。

    執行terraform show命令

    您可以在工作目錄中,使用以下命令查詢Terraform已建立資源的詳細資料:

    terraform show
    shell@Alicloud:~/edas$ terraform show
    # alicloud_edas_application.app:
    resource "alicloud_edas_application" "app" {
        application_name = "tf-test-app-xxx"
        build_pack_id    = 0
        cluster_id       = "be02e4e1-6720-401b-bc54-xxx"
        id               = "f7807d2d-b1a6-47a9-94c4-xxx"
        package_type     = "JAR"
    }
    # alicloud_edas_application_deployment.default:
    resource "alicloud_edas_application_deployment" "default" {
        app_id               = "f7807d2d-b1a6-47a9-94c4-xxx"
        group_id             = "78ddbcb5-b6b4-4344-a42a-xxx"
        id                   = "f7807d2d-b1a6-47a9-94c4-xxx:8b27efd4-0bed-4eb5-b9d7-xxx"
        last_package_version = "1730948612"
        war_url              = "http://edas-sz.oss-cn-shenzhen.aliyuncs.com/prod/demo/SPRING_CLOUD_CONSUMER.jar"
    }
    # alicloud_edas_application_scale.default:
    resource "alicloud_edas_application_scale" "default" {
        app_id       = "f7807d2d-b1a6-47a9-94c4-xxx"
        deploy_group = "78ddbcb5-b6b4-4344-a42a-xxx"
        ecc_info     = "9ab87eae-fb38-4d63-9685-xxx"
        ecu_info     = [
            "855c7d64-add2-4e50-9dd7-xxx",
        ]
        id           = "f7807d2d-b1a6-47a9-94c4-xxx:855c7d64-add2-4e50-9dd7-xxx"
    }

    登入控制台查看

    登入Enterprise Distributed Application Service(EDAS),進入應用管理 > 應用列表頁面,左上方選擇地區,此例中選擇华东2(上海),查看已建立的應用。單擊此例建立應用的应用名称,進入实例部署信息頁簽,查看應用的部署資訊。

    頁面顯示 tf-test-group 分組下有一台 ECS 執行個體(部署套件版本 1730949226),運行狀態為運行正常,變更狀態為成功,表明應用已成功部署。

步驟五:建立SLB並綁定應用

  1. 在main.tf檔案中增加如下代碼。

    # 建立SLB
    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"
    }
    # 綁定SLB
    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. 建立執行計畫,並預覽變更。

    terraform plan
  3. 執行如下命令,開始執行代碼。

    terraform apply

    在執行過程中,根據提示輸入yes並按下Enter鍵,等待命令執行完成,若出現以下資訊,則表示代碼執行成功。

    Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
  4. 驗證結果。

    執行terraform show命令

    您可以在工作目錄中,使用以下命令查詢Terraform已建立資源的詳細資料:

    terraform show
    # alicloud_edas_slb_attachment.this:
    resource "alicloud_edas_slb_attachment" "this" {
        app_id     = "f7807d2d-b1a6-47a9-94c4-xxx"
        id         = "f7807d2d-b1a6-47a9-94c4-xxx:lb-uf6bo3hea    xxx"
        slb_id     = "lb-uf6bo3hea7cxxx"
        slb_ip     = "172.16.xxx"
        slb_status = "active"
        type       = "intranet"
        vswitch_id = "vsw-uf6m03nh7cfxxx"
    }
    # alicloud_instance.instance:
    resource "alicloud_instance" "instance" {
        availability_zone              = "cn-shanghai-b"
        cpu                            = 2
        ...
    }

    登入控制台查看

    登入Enterprise Distributed Application Service(EDAS),進入應用管理 > 應用列表頁面,左上方選擇地區,此例中選擇华东2(上海),查看已建立的應用。單擊此例建立應用的应用名称,進入基本資料頁簽,查看應用的基本資料。

    應用 基本資料 頁簽顯示以下關鍵資訊: - 叢集類型:ECS 叢集 - 應用運行環境:標準 Java 應用運行環境 - 部署套件類型:FATJAR - 微服務空間:cn-shanghai - 狀態:運行中(1/共 1) - 應用連接埠:8080 - 負載平衡(私網):已綁定私網 IP 位址,可執行 修改 和 解除綁定 操作

清理資源

當您不再需要上述通過Terraform建立或管理的資源時,請運行以下命令以釋放資源。關於terraform destroy的更多資訊,請參見Terraform常用命令。

terraform destroy

完整樣本

說明

本教程所含範例程式碼支援一鍵運行,您可以直接運行代碼。一鍵運行

範例程式碼

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地址
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
}
# 截取應用分組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"
}
# 隨機數
resource "random_integer" "default" {
  min = 10000
  max = 99999
}
# 建立Virtual Private Cloud
resource "alicloud_vpc" "vpc" {
  vpc_name   = "vpc-test_${random_integer.default.result}"
  cidr_block = var.vpc_cidr_block
}
# 建立安全性群組
resource "alicloud_security_group" "group" {
  name   = "test_${random_integer.default.result}"
  vpc_id = alicloud_vpc.vpc.id
}
# 建立交換器
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}"
}
# 建立ECS
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"
}
# 建立ecs執行個體成功之後等待執行個體初始化完成,一般不超過60s
resource "time_sleep" "example" {
  depends_on      = [alicloud_instance.instance]
  create_duration = "60s"
}
# 建立ECS叢集
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
}
# 添加ECS執行個體到ECS叢集
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]
}
# 建立應用
resource "alicloud_edas_application" "app" {
  application_name = "tf-test-app-${random_integer.default.result}"
  cluster_id       = alicloud_edas_cluster.cluster.id
  package_type     = "JAR"
}
# 建立應用分組
resource "alicloud_edas_deploy_group" "this" {
  app_id     = alicloud_edas_application.app.id
  group_name = "tf-test-group-${random_integer.default.result}"
}
# 應用擴容
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]]
}
# 部署應用
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
}
# 部署應用成功後等待應用啟動,一般不超過60s
resource "time_sleep" "example2" {
  depends_on      = [alicloud_edas_application_deployment.default]
  create_duration = "60s"
}
# 建立SLB
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"
}
# 綁定SLB
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
}
                

如果您想體驗更多完整樣本,請前往更多完整樣本中對應產品的檔案夾查看。

相關文檔