Utilizzare CreateCluster con un SDK AWS o una CLI - Esempi di codice per SDK AWS

Sono disponibili altri esempi per SDK AWS nel repository GitHub della documentazione degli esempi per SDK AWS.

Utilizzare CreateCluster con un SDK AWS o una CLI

Gli esempi di codice seguenti mostrano come utilizzare CreateCluster.

CLI
AWS CLI

Esempio 1: come creare un nuovo cluster.

L’esempio create-cluster seguente crea un cluster denominato MyCluster e abilita gli approfondimenti sui container Amazon CloudWatch con osservabilità migliorata.

aws ecs create-cluster \ --cluster-name MyCluster \ --settings name=containerInsights,value=enhanced

Output:

{ "cluster": { "clusterArn": "arn:aws:ecs:us-west-2:123456789012:cluster/MyCluster", "clusterName": "MyCluster", "status": "ACTIVE", "registeredContainerInstancesCount": 0, "pendingTasksCount": 0, "runningTasksCount": 0, "activeServicesCount": 0, "statistics": [], "settings": [ { "name": "containerInsights", "value": "enhanced" } ], "tags": [] } }

Per ulteriori informazioni, consulta Creazione di un cluster nella Guida per gli sviluppatori di Amazon ECS.

Esempio 2: come creare un nuovo cluster utilizzando provider di capacità.

L’esempio create-cluster seguente crea un cluster a cui associa due provider di capacità esistenti. Il comando create-capacity-provider viene utilizzato per creare un provider di capacità. La definizione di una strategia predefinita per i provider di capacità è facoltativa, ma consigliata. In questo esempio, viene creato un cluster denominato MyCluster a cui vengono associati i provider di capacità MyCapacityProvider1 e MyCapacityProvider2. Viene specificata una strategia predefinita dei provider di capacità che distribuisce le attività in modo uniforme su entrambi i provider di capacità.

aws ecs create-cluster \ --cluster-name MyCluster \ --capacity-providers MyCapacityProvider1 MyCapacityProvider2 \ --default-capacity-provider-strategy capacityProvider=MyCapacityProvider1,weight=1 capacityProvider=MyCapacityProvider2,weight=1

Output:

{ "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" } }

Per ulteriori informazioni, consulta Provider di capacità dei cluster nella Guida per gli sviluppatori di Amazon ECS.

Esempio 3: come creare un nuovo cluster con più tag.

L’esempio create-cluster seguente crea un cluster con più tag. Per ulteriori informazioni sull’aggiunta di tag utilizzando la sintassi abbreviata, consulta Utilizzo della sintassi abbreviata con l’interfaccia della linea di comandoAWS nella Guida per l’utente di AWS CLI.

aws ecs create-cluster \ --cluster-name MyCluster \ --tags key=key1,value=value1 key=key2,value=value2

Output:

{ "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" } ] } }

Per ulteriori informazioni, consulta Creazione di un cluster nella Guida per gli sviluppatori di Amazon ECS.

  • Per informazioni dettagliate sull’API, consulta CreateCluster nella documentazione di riferimento dei comandi della AWS CLI.

Java
SDK per Java 2.x
Nota

Ulteriori informazioni su GitHub. Trova l’esempio completo e scopri di più sulla configurazione e l’esecuzione nel Repository di esempi di codice 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 ""; } }
  • Per informazioni dettagliate sull’API, consulta CreateCluster nella documentazione di riferimento dell’API AWS SDK for Java 2.x.

PowerShell
Strumenti per PowerShell V4

Esempio 1: questo cmdlet crea un nuovo cluster Amazon ECS.

New-ECSCluster -ClusterName "LAB-ECS-CL" -Setting @{Name="containerInsights"; Value="enabled"}

Output:

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 : {}
  • Per informazioni dettagliate sull’API, consulta CreateCluster nella documentazione di riferimento dei cmdlet di AWS Strumenti per PowerShell (V4).

Strumenti per PowerShell V5

Esempio 1: questo cmdlet crea un nuovo cluster Amazon ECS.

New-ECSCluster -ClusterName "LAB-ECS-CL" -Setting @{Name="containerInsights"; Value="enabled"}

Output:

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 : {}
  • Per informazioni dettagliate sull’API, consulta CreateCluster nella documentazione di riferimento dei cmdlet di AWS Strumenti per PowerShell (V5).

Rust
SDK per Rust
Nota

Ulteriori informazioni su GitHub. Trova l’esempio completo e scopri di più sulla configurazione e l’esecuzione nel Repository di esempi di codice 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(()) }
  • Per informazioni dettagliate sull’API, consulta CreateCluster nella documentazione di riferimento dell’API SDK AWS per Rust.