与 AWS SDK或CreateCluster一起使用 CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

与 AWS SDK或CreateCluster一起使用 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 的集群,并将 MyCapacityProvider1MyCapacityProvider2 容量提供程序与其相关联。指定默认容量提供程序策略,将任务平均分散到两个容量提供程序。

aws ecs create-cluster-name--cluster-name MyCluster --cluster-providers MyCapacityProvider 1 MyCapacityProvider 2 — default-capacity-provider-strategy capacityProvider = 1,权重 =1 = MyCapacityProvider 2,权重 =1 capacityProvider MyCapacityProvider

输出:

{ "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 命令行界面中使用速记语法。AWS CLI

aws ecs create-cluster \ --cluster-name MyCluster \ --tags key=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 开发者指南中的创建集群

Java
SDK适用于 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
用于 PowerShell

示例 1:此 cmdlet 创建了一个新的 Ama ECS zon 集群。

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对于 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详细信息,请参见CreateCluster中的 Rust AWS SDK API 参考