文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS SDK 範例
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
CreateCluster
搭配 AWS SDK 或 CLI 使用
下列程式碼範例示範如何使用 CreateCluster
。
- CLI
-
- AWS CLI
-
範例 1:建立新叢集
下列
create-cluster
範例會建立叢集。aws ecs create-cluster \ --cluster-name
MyCluster
輸出:
{ "cluster": { "clusterArn": "arn:aws:ecs:us-west-2:123456789012:cluster/MyCluster", "clusterName": "MyCluster", "status": "ACTIVE", "registeredContainerInstancesCount": 0, "pendingTasksCount": 0, "runningTasksCount": 0, "activeServicesCount": 0, "statistics": [], "tags": [] } }
如需詳細資訊,請參閱《Amazon ECS 開發人員指南》中的建立叢集。
範例 2:使用容量提供者建立新叢集
下列
create-cluster
範例會建立叢集,並將兩個現有容量提供者與其建立關聯。create-capacity-provider
命令用於建立容量提供者。指定預設容量提供者策略是選用的,但建議使用。在此範例中,我們會建立名為 的叢集,MyCluster
並將MyCapacityProvider1
和MyCapacityProvider2
容量提供者與其建立關聯。系統會指定預設容量提供者策略,將任務平均分散到兩個容量提供者。aws ecs create-cluster --cluster-name MyCluster --capacity-providers MyCapacityProvider1 MyCapacityProvider2 --default-capacity-provider-strategy capacityProvider=MyCapacityProvider1,weight=1 capacityProvider=MyCapacityProvider2,weight=1
輸出:
{ "cluster": { "clusterArn": "arn:aws:ecs:us-west-2:123456789012:cluster/MyCluster", "clusterName": "MyCluster", "status": "PROVISIONING", "registeredContainerInstancesCount": 0, "pendingTasksCount": 0, "runningTasksCount": 0, "activeServicesCount": 0, "statistics": [], "settings": [ { "name": "containerInsights", "value": "enabled" } ], "capacityProviders": [ "MyCapacityProvider1", "MyCapacityProvider2" ], "defaultCapacityProviderStrategy": [ { "capacityProvider": "MyCapacityProvider1", "weight": 1, "base": 0 }, { "capacityProvider": "MyCapacityProvider2", "weight": 1, "base": 0 } ], "attachments": [ { "id": "0fb0c8f4-6edd-4de1-9b09-17e470ee1918", "type": "asp", "status": "PRECREATED", "details": [ { "name": "capacityProviderName", "value": "MyCapacityProvider1" }, { "name": "scalingPlanName", "value": "ECSManagedAutoScalingPlan-a1b2c3d4-5678-90ab-cdef-EXAMPLE11111" } ] }, { "id": "ae592060-2382-4663-9476-b015c685593c", "type": "asp", "status": "PRECREATED", "details": [ { "name": "capacityProviderName", "value": "MyCapacityProvider2" }, { "name": "scalingPlanName", "value": "ECSManagedAutoScalingPlan-a1b2c3d4-5678-90ab-cdef-EXAMPLE22222" } ] } ], "attachmentsStatus": "UPDATE_IN_PROGRESS" } }
如需詳細資訊,請參閱《Amazon ECS 開發人員指南》中的叢集容量提供者。
範例 3:建立具有多個標籤的新叢集
下列
create-cluster
範例會建立具有多個標籤的叢集。如需使用速記語法新增標籤的詳細資訊,請參閱《AWS CLI 使用者指南》中的使用速記語法搭配 AWS 命令列界面。aws ecs create-cluster \ --cluster-name
MyCluster
\ --tagskey=key1,value=value1
key=key2,value=value2
key=key3,value=value3
輸出:
{ "cluster": { "clusterArn": "arn:aws:ecs:us-west-2:123456789012:cluster/MyCluster", "clusterName": "MyCluster", "status": "ACTIVE", "registeredContainerInstancesCount": 0, "pendingTasksCount": 0, "runningTasksCount": 0, "activeServicesCount": 0, "statistics": [], "tags": [ { "key": "key1", "value": "value1" }, { "key": "key2", "value": "value2" }, { "key": "key3", "value": "value3" } ] } }
如需詳細資訊,請參閱《Amazon ECS 開發人員指南》中的建立叢集。
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 CreateCluster
。
-
- Java
-
- SDK for Java 2.x
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ecs.EcsClient; import software.amazon.awssdk.services.ecs.model.ExecuteCommandConfiguration; import software.amazon.awssdk.services.ecs.model.ExecuteCommandLogging; import software.amazon.awssdk.services.ecs.model.ClusterConfiguration; import software.amazon.awssdk.services.ecs.model.CreateClusterResponse; import software.amazon.awssdk.services.ecs.model.EcsException; import software.amazon.awssdk.services.ecs.model.CreateClusterRequest; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class CreateCluster { public static void main(String[] args) { final String usage = """ Usage: <clusterName>\s Where: clusterName - The name of the ECS cluster to create. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String clusterName = args[0]; Region region = Region.US_EAST_1; EcsClient ecsClient = EcsClient.builder() .region(region) .build(); String clusterArn = createGivenCluster(ecsClient, clusterName); System.out.println("The cluster ARN is " + clusterArn); ecsClient.close(); } public static String createGivenCluster(EcsClient ecsClient, String clusterName) { try { ExecuteCommandConfiguration commandConfiguration = ExecuteCommandConfiguration.builder() .logging(ExecuteCommandLogging.DEFAULT) .build(); ClusterConfiguration clusterConfiguration = ClusterConfiguration.builder() .executeCommandConfiguration(commandConfiguration) .build(); CreateClusterRequest clusterRequest = CreateClusterRequest.builder() .clusterName(clusterName) .configuration(clusterConfiguration) .build(); CreateClusterResponse response = ecsClient.createCluster(clusterRequest); return response.cluster().clusterArn(); } catch (EcsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; } }
-
如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 CreateCluster。
-
- PowerShell
-
- Tools for PowerShell
-
範例 1:此 cmdlet 會建立新的 Amazon ECS 叢集。
New-ECSCluster -ClusterName "LAB-ECS-CL" -Setting @{Name="containerInsights"; Value="enabled"}
輸出:
ActiveServicesCount : 0 Attachments : {} AttachmentsStatus : CapacityProviders : {} ClusterArn : arn:aws:ecs:us-west-2:012345678912:cluster/LAB-ECS-CL ClusterName : LAB-ECS-CL DefaultCapacityProviderStrategy : {} PendingTasksCount : 0 RegisteredContainerInstancesCount : 0 RunningTasksCount : 0 Settings : {containerInsights} Statistics : {} Status : ACTIVE Tags : {}
-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 CreateCluster。
-
- Rust
-
- SDK for Rust
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 async fn make_cluster(client: &aws_sdk_ecs::Client, name: &str) -> Result<(), aws_sdk_ecs::Error> { let cluster = client.create_cluster().cluster_name(name).send().await?; println!("cluster created: {:?}", cluster); Ok(()) }
-
如需 API 詳細資訊,請參閱 AWS SDK for Rust API 參考中的 CreateCluster
。
-