本文档 AWS CLI 仅适用于版本 1。有关版本 2 的文档 AWS CLI,请参阅版本 2 用户指南。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用亚马逊的ECS示例 AWS CLI
以下代码示例向您展示如何在 Amazon 中使用来执行操作和实现常见场景ECS。 AWS Command Line Interface
操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。
每个示例都包含一个指向完整源代码的链接,您可以在其中找到有关如何在上下文中设置和运行代码的说明。
主题
操作
以下代码示例显示了如何使用create-capacity-provider
。
- AWS CLI
-
创建容量提供商
以下 create-capacity-provider示例创建了一个容量提供商,该容量提供者使用名为 My 的 Auto Scaling 组ASG,该组已启用托管扩展和托管终止保护。此配置用于 Amazon ECS 集群的自动扩展。
aws ecs create-capacity-provider \ --name
"MyCapacityProvider"
\ --auto-scaling-group-provider"autoScalingGroupArn=arn:aws:autoscaling:us-east-1:123456789012:autoScalingGroup:57ffcb94-11f0-4d6d-bf60-3bac5EXAMPLE:autoScalingGroupName/MyASG,managedScaling={status=ENABLED,targetCapacity=100},managedTerminationProtection=ENABLED"
输出:
{ "capacityProvider": { "capacityProviderArn": "arn:aws:ecs:us-east-1:123456789012:capacity-provider/MyCapacityProvider", "name": "MyCapacityProvider", "status": "ACTIVE", "autoScalingGroupProvider": { "autoScalingGroupArn": "arn:aws:autoscaling:us-east-1:132456789012:autoScalingGroup:57ffcb94-11f0-4d6d-bf60-3bac5EXAMPLE:autoScalingGroupName/MyASG", "managedScaling": { "status": "ENABLED", "targetCapacity": 100, "minimumScalingStepSize": 1, "maximumScalingStepSize": 10000, "instanceWarmupPeriod": 300 }, "managedTerminationProtection": "ENABLED" }, "tags": [] }
有关更多信息,请参阅《亚马逊ECS开发者指南》中的 Amazon ECS 集群自动扩展。
-
有关API详细信息,请参阅 “CreateCapacityProvider AWS CLI
命令参考”。
-
以下代码示例显示了如何使用create-cluster
。
- 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-name--cluster-name MyCluster --cality-providers 1 2 — default-capacity-provider-strategy capacityProvider = MyCapacityProvider 1,weight=1 = MyCapacityProvider 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 CLIaws 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详细信息,请参阅 “CreateCluster AWS CLI
命令参考”。
-
以下代码示例显示了如何使用create-service
。
- AWS CLI
-
示例 1:使用 Fargate 任务创建服务
以下
create-service
示例演示了如何使用 Fargate 任务创建服务。aws ecs create-service \ --cluster
MyCluster
\ --service-nameMyService
\ --task-definitionsample-fargate:1
\ --desired-count2
\ --launch-typeFARGATE
\ --platform-versionLATEST
\ --network-configuration"awsvpcConfiguration={subnets=[subnet-12344321],securityGroups=[sg-12344321],assignPublicIp=ENABLED}"
\ --tagskey=key1,value=value1
key=key2,value=value2
key=key3,value=value3
输出:
{ "service": { "serviceArn": "arn:aws:ecs:us-west-2:123456789012:service/MyCluster/MyService", "serviceName": "MyService", "clusterArn": "arn:aws:ecs:us-west-2:123456789012:cluster/MyCluster", "loadBalancers": [], "serviceRegistries": [], "status": "ACTIVE", "desiredCount": 2, "runningCount": 0, "pendingCount": 0, "launchType": "FARGATE", "platformVersion": "LATEST", "taskDefinition": "arn:aws:ecs:us-west-2:123456789012:task-definition/sample-fargate:1", "deploymentConfiguration": { "maximumPercent": 200, "minimumHealthyPercent": 100 }, "deployments": [ { "id": "ecs-svc/1234567890123456789", "status": "PRIMARY", "taskDefinition": "arn:aws:ecs:us-west-2:123456789012:task-definition/sample-fargate:1", "desiredCount": 2, "pendingCount": 0, "runningCount": 0, "createdAt": 1557119253.821, "updatedAt": 1557119253.821, "launchType": "FARGATE", "platformVersion": "1.3.0", "networkConfiguration": { "awsvpcConfiguration": { "subnets": [ "subnet-12344321" ], "securityGroups": [ "sg-12344321" ], "assignPublicIp": "ENABLED" } } } ], "roleArn": "arn:aws:iam::123456789012:role/aws-service-role/ecs.amazonaws.com/AWSServiceRoleForECS", "events": [], "createdAt": 1557119253.821, "placementConstraints": [], "placementStrategy": [], "networkConfiguration": { "awsvpcConfiguration": { "subnets": [ "subnet-12344321" ], "securityGroups": [ "sg-12344321" ], "assignPublicIp": "ENABLED" } }, "schedulingStrategy": "REPLICA", "tags": [ { "key": "key1", "value": "value1" }, { "key": "key2", "value": "value2" }, { "key": "key3", "value": "value3" } ], "enableECSManagedTags": false, "propagateTags": "NONE" } }
示例 2:使用EC2启动类型创建服务
以下
create-service
示例说明如何使用EC2启动类型的任务创建名ecs-simple-service
为的服务。该服务使用sleep360
任务定义并维护任务的 1 个实例化。aws ecs create-service \ --cluster
MyCluster
\ --service-nameecs-simple-service
\ --task-definitionsleep360:2
\ --desired-count1
输出:
{ "service": { "serviceArn": "arn:aws:ecs:us-west-2:123456789012:service/MyCluster/ecs-simple-service", "serviceName": "ecs-simple-service", "clusterArn": "arn:aws:ecs:us-west-2:123456789012:cluster/MyCluster", "loadBalancers": [], "serviceRegistries": [], "status": "ACTIVE", "desiredCount": 1, "runningCount": 0, "pendingCount": 0, "launchType": "EC2", "taskDefinition": "arn:aws:ecs:us-west-2:123456789012:task-definition/sleep360:2", "deploymentConfiguration": { "maximumPercent": 200, "minimumHealthyPercent": 100 }, "deployments": [ { "id": "ecs-svc/1234567890123456789", "status": "PRIMARY", "taskDefinition": "arn:aws:ecs:us-west-2:123456789012:task-definition/sleep360:2", "desiredCount": 1, "pendingCount": 0, "runningCount": 0, "createdAt": 1557206498.798, "updatedAt": 1557206498.798, "launchType": "EC2" } ], "events": [], "createdAt": 1557206498.798, "placementConstraints": [], "placementStrategy": [], "schedulingStrategy": "REPLICA", "enableECSManagedTags": false, "propagateTags": "NONE" } }
示例 3:创建使用外部部署控制器的服务
以下
create-service
示例将创建使用外部部署控制器的服务。aws ecs create-service \ --cluster
MyCluster
\ --service-nameMyService
\ --deployment-controllertype=EXTERNAL
\ --desired-count1
输出:
{ "service": { "serviceArn": "arn:aws:ecs:us-west-2:123456789012:service/MyCluster/MyService", "serviceName": "MyService", "clusterArn": "arn:aws:ecs:us-west-2:123456789012:cluster/MyCluster", "loadBalancers": [], "serviceRegistries": [], "status": "ACTIVE", "desiredCount": 1, "runningCount": 0, "pendingCount": 0, "launchType": "EC2", "deploymentConfiguration": { "maximumPercent": 200, "minimumHealthyPercent": 100 }, "taskSets": [], "deployments": [], "roleArn": "arn:aws:iam::123456789012:role/aws-service-role/ecs.amazonaws.com/AWSServiceRoleForECS", "events": [], "createdAt": 1557128207.101, "placementConstraints": [], "placementStrategy": [], "schedulingStrategy": "REPLICA", "deploymentController": { "type": "EXTERNAL" }, "enableECSManagedTags": false, "propagateTags": "NONE" } }
示例 4:在负载均衡器后面创建新服务
以下
create-service
示例将显示如何创建位于负载均衡器之后的服务。您必须将负载均衡器配置为您的容器实例所在的同一区域。此示例使用--cli-input-json
选项和名为的JSON输入文件,ecs-simple-service-elb.json
其中包含以下内容:{ "serviceName": "ecs-simple-service-elb", "taskDefinition": "ecs-demo", "loadBalancers": [ { "loadBalancerName": "EC2Contai-EcsElast-123456789012", "containerName": "simple-demo", "containerPort": 80 } ], "desiredCount": 10, "role": "ecsServiceRole" }
命令:
aws ecs create-service \ --cluster
MyCluster
\ --service-nameecs-simple-service-elb
\ --cli-input-jsonfile://ecs-simple-service-elb.json
输出:
{ "service": { "status": "ACTIVE", "taskDefinition": "arn:aws:ecs:us-west-2:123456789012:task-definition/ecs-demo:1", "pendingCount": 0, "loadBalancers": [ { "containerName": "ecs-demo", "containerPort": 80, "loadBalancerName": "EC2Contai-EcsElast-123456789012" } ], "roleArn": "arn:aws:iam::123456789012:role/ecsServiceRole", "desiredCount": 10, "serviceName": "ecs-simple-service-elb", "clusterArn": "arn:aws:ecs:<us-west-2:123456789012:cluster/MyCluster", "serviceArn": "arn:aws:ecs:us-west-2:123456789012:service/ecs-simple-service-elb", "deployments": [ { "status": "PRIMARY", "pendingCount": 0, "createdAt": 1428100239.123, "desiredCount": 10, "taskDefinition": "arn:aws:ecs:us-west-2:123456789012:task-definition/ecs-demo:1", "updatedAt": 1428100239.123, "id": "ecs-svc/1234567890123456789", "runningCount": 0 } ], "events": [], "runningCount": 0 } }
有关更多信息,请参阅《Amazon ECS 开发者指南》中的创建服务。
-
有关API详细信息,请参阅 “CreateService AWS CLI
命令参考”。
-
以下代码示例显示了如何使用create-task-set
。
- AWS CLI
-
创建任务集
以下
create-task-set
示例在使用外部部署控制器的服务中创建任务集。aws ecs create-task-set \ --cluster
MyCluster
\ --serviceMyService
\ --task-definitionMyTaskDefinition:2
\ --network-configuration"awsvpcConfiguration={subnets=[subnet-12344321],securityGroups=[sg-12344321]}"
输出:
{ "taskSet": { "id": "ecs-svc/1234567890123456789", "taskSetArn": "arn:aws:ecs:us-west-2:123456789012:task-set/MyCluster/MyService/ecs-svc/1234567890123456789", "status": "ACTIVE", "taskDefinition": "arn:aws:ecs:us-west-2:123456789012:task-definition/MyTaskDefinition:2", "computedDesiredCount": 0, "pendingCount": 0, "runningCount": 0, "createdAt": 1557128360.711, "updatedAt": 1557128360.711, "launchType": "EC2", "networkConfiguration": { "awsvpcConfiguration": { "subnets": [ "subnet-12344321" ], "securityGroups": [ "sg-12344321" ], "assignPublicIp": "DISABLED" } }, "loadBalancers": [], "serviceRegistries": [], "scale": { "value": 0.0, "unit": "PERCENT" }, "stabilityStatus": "STABILIZING", "stabilityStatusAt": 1557128360.711 } }
-
有关API详细信息,请参阅 “CreateTaskSet AWS CLI
命令参考”。
-
以下代码示例显示了如何使用delete-account-setting
。
- AWS CLI
-
删除特定IAM用户或IAM角色的账户设置
以下示例
delete-account-setting
删除了特定IAM用户或IAM角色的账户设置。aws ecs delete-account-setting \ --name
serviceLongArnFormat
\ --principal-arnarn:aws:iam::123456789012:user/MyUser
输出:
{ "setting": { "name": "serviceLongArnFormat", "value": "enabled", "principalArn": "arn:aws:iam::123456789012:user/MyUser" } }
有关更多信息,请参阅亚马逊资源名称 (ARNs) 和IDs亚马逊ECS开发者指南。
-
有关API详细信息,请参阅 “DeleteAccountSetting AWS CLI
命令参考”。
-
以下代码示例显示了如何使用delete-attributes
。
- AWS CLI
-
从 Amazon ECS 资源中删除一个或多个自定义属性
以下内容
stack
从容器实例中delete-attributes
删除名称为的属性。aws ecs delete-attributes \ --attributes
name=stack,targetId=arn:aws:ecs:us-west-2:130757420319:container-instance/1c3be8ed-df30-47b4-8f1e-6e68ebd01f34
输出:
{ "attributes": [ { "name": "stack", "targetId": "arn:aws:ecs:us-west-2:130757420319:container-instance/1c3be8ed-df30-47b4-8f1e-6e68ebd01f34", "value": "production" } ] }
-
有关API详细信息,请参阅 “DeleteAttributes AWS CLI
命令参考”。
-
以下代码示例显示了如何使用delete-capacity-provider
。
- AWS CLI
-
示例 1:使用 Amazon 资源名称删除容量提供商 (ARN)
以下
delete-capacity-provider
示例通过指定容量提供商的 Amazon 资源名称 (ARN) 来删除容量提供商。可以使用describe-capacity-providers
命令检索容量提供商删除的状态和状态。ARNaws ecs delete-capacity-provider \ --capacity-provider
arn:aws:ecs:us-west-2:123456789012:capacity-provider/ExampleCapacityProvider
输出:
{ "capacityProvider": { "capacityProviderArn": "arn:aws:ecs:us-west-2:123456789012:capacity-provider/ExampleCapacityProvider", "name": "ExampleCapacityProvider", "status": "ACTIVE", "autoScalingGroupProvider": { "autoScalingGroupArn": "arn:aws:autoscaling:us-west-2:123456789012:autoScalingGroup:a1b2c3d4-5678-90ab-cdef-EXAMPLE11111:autoScalingGroupName/MyAutoScalingGroup", "managedScaling": { "status": "ENABLED", "targetCapacity": 100, "minimumScalingStepSize": 1, "maximumScalingStepSize": 10000 }, "managedTerminationProtection": "DISABLED" }, "updateStatus": "DELETE_IN_PROGRESS", "tags": [] } }
有关更多信息,请参阅 Amazon ECS 开发者指南中的集群容量提供商。
示例 2:使用名称删除容量提供商
以下
delete-capacity-provider
示例通过指定容量提供者的短名称来删除容量提供商。可以使用describe-capacity-providers
命令检索容量提供商删除的短名称和状态。aws ecs delete-capacity-provider \ --capacity-provider
ExampleCapacityProvider
输出:
{ "capacityProvider": { "capacityProviderArn": "arn:aws:ecs:us-west-2:123456789012:capacity-provider/ExampleCapacityProvider", "name": "ExampleCapacityProvider", "status": "ACTIVE", "autoScalingGroupProvider": { "autoScalingGroupArn": "arn:aws:autoscaling:us-west-2:123456789012:autoScalingGroup:a1b2c3d4-5678-90ab-cdef-EXAMPLE11111:autoScalingGroupName/MyAutoScalingGroup", "managedScaling": { "status": "ENABLED", "targetCapacity": 100, "minimumScalingStepSize": 1, "maximumScalingStepSize": 10000 }, "managedTerminationProtection": "DISABLED" }, "updateStatus": "DELETE_IN_PROGRESS", "tags": [] } }
有关更多信息,请参阅 Amazon ECS 开发者指南中的集群容量提供商。
-
有关API详细信息,请参阅 “DeleteCapacityProvider AWS CLI
命令参考”。
-
以下代码示例显示了如何使用delete-cluster
。
- AWS CLI
-
删除空集群
以下
delete-cluster
示例将删除指定的空集群。aws ecs delete-cluster --cluster
MyCluster
输出:
{ "cluster": { "clusterArn": "arn:aws:ecs:us-west-2:123456789012:cluster/MyCluster", "status": "INACTIVE", "clusterName": "MyCluster", "registeredContainerInstancesCount": 0, "pendingTasksCount": 0, "runningTasksCount": 0, "activeServicesCount": 0 "statistics": [], "tags": [] } }
有关更多信息,请参阅 Amazon ECS 开发者指南中的删除集群。
-
有关API详细信息,请参阅 “DeleteCluster AWS CLI
命令参考”。
-
以下代码示例显示了如何使用delete-service
。
- AWS CLI
-
删除服务
以下
ecs delete-service
示例将从集群中删除指定的服务。您可以包含--force
参数来删除服务,即使它尚未缩减至 0 个任务。aws ecs delete-service --cluster
MyCluster
--serviceMyService1
--force有关更多信息,请参阅《Amazon ECS 开发者指南》中的删除服务。
-
有关API详细信息,请参阅 “DeleteService AWS CLI
命令参考”。
-
以下代码示例显示了如何使用delete-task-definitions
。
- AWS CLI
-
删除任务定义
以下
delete-task-definitions
示例删除了INACTIVE任务定义。aws ecs delete-task-definitions \ --task-definition
curltest:1
输出:
{ "taskDefinitions": [ { "taskDefinitionArn": "arn:aws:ecs:us-east-1:123456789012:task-definition/curltest:1", "containerDefinitions": [ { "name": "ctest", "image": "mreferre/eksutils", "cpu": 0, "portMappings": [], "essential": true, "entryPoint": [ "sh", "-c" ], "command": [ "curl ${ECS_CONTAINER_METADATA_URI_V4}/task" ], "environment": [], "mountPoints": [], "volumesFrom": [], "logConfiguration": { "logDriver": "awslogs", "options": { "awslogs-create-group": "true", "awslogs-group": "/ecs/curltest", "awslogs-region": "us-east-1", "awslogs-stream-prefix": "ecs" } } } ], "family": "curltest", "taskRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole", "executionRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole", "networkMode": "awsvpc", "revision": 1, "volumes": [], "status": "DELETE_IN_PROGRESS", "compatibilities": [ "EC2", "FARGATE" ], "requiresCompatibilities": [ "FARGATE" ], "cpu": "256", "memory": "512", "registeredAt": "2021-09-10T12:56:24.704000+00:00", "deregisteredAt": "2023-03-14T15:20:59.419000+00:00", "registeredBy": "arn:aws:sts::123456789012:assumed-role/Admin/jdoe" } ], "failures": [] }
有关更多信息,请参阅《亚马逊ECS开发者指南》中的亚马逊ECS任务定义。
-
有关API详细信息,请参阅 “DeleteTaskDefinitions AWS CLI
命令参考”。
-
以下代码示例显示了如何使用delete-task-set
。
- AWS CLI
-
删除任务集
以下
delete-task-set
示例说明如何删除任务集。即使任务集尚未缩放为零,也可以添加用于删除该任务集的--force
参数。aws ecs delete-task-set \ --cluster
MyCluster
\ --serviceMyService
\ --task-setarn:aws:ecs:us-west-2:123456789012:task-set/MyCluster/MyService/ecs-svc/1234567890123456789
\ --force输出:
{ "taskSet": { "id": "ecs-svc/1234567890123456789", "taskSetArn": "arn:aws:ecs:us-west-2:123456789012:task-set/MyCluster/MyService/ecs-svc/1234567890123456789", "status": "DRAINING", "taskDefinition": "arn:aws:ecs:us-west-2:123456789012:task-definition/sample-fargate:2", "computedDesiredCount": 0, "pendingCount": 0, "runningCount": 0, "createdAt": 1557130260.276, "updatedAt": 1557130290.707, "launchType": "EC2", "networkConfiguration": { "awsvpcConfiguration": { "subnets": [ "subnet-12345678" ], "securityGroups": [ "sg-12345678" ], "assignPublicIp": "DISABLED" } }, "loadBalancers": [], "serviceRegistries": [], "scale": { "value": 0.0, "unit": "PERCENT" }, "stabilityStatus": "STABILIZING", "stabilityStatusAt": 1557130290.707 } }
-
有关API详细信息,请参阅 “DeleteTaskSet AWS CLI
命令参考”。
-
以下代码示例显示了如何使用deregister-container-instance
。
- AWS CLI
-
从集群中注销容器实例
以下
deregister-container-instance
示例从指定集群中注销容器实例。如果容器实例中仍有任务在运行,则必须在注销注册之前停止这些任务,或者使用--force
选项。aws ecs deregister-container-instance \ --cluster
arn:aws:ecs:us-west-2:123456789012:cluster/MyCluster
\ --container-instancearn:aws:ecs:us-west-2:123456789012:container-instance/a1b2c3d4-5678-90ab-cdef-11111EXAMPLE
\ --force输出:
{ "containerInstance": { "remainingResources": [ { "integerValue": 1024, "doubleValue": 0.0, "type": "INTEGER", "longValue": 0, "name": "CPU" }, { "integerValue": 985, "doubleValue": 0.0, "type": "INTEGER", "longValue": 0, "name": "MEMORY" }, { "type": "STRINGSET", "integerValue": 0, "name": "PORTS", "stringSetValue": [ "22", "2376", "2375", "51678", "51679" ], "longValue": 0, "doubleValue": 0.0 }, { "type": "STRINGSET", "integerValue": 0, "name": "PORTS_UDP", "stringSetValue": [], "longValue": 0, "doubleValue": 0.0 } ], "agentConnected": true, "attributes": [ { "name": "ecs.capability.secrets.asm.environment-variables" }, { "name": "com.amazonaws.ecs.capability.logging-driver.syslog" }, { "value": "ami-01a82c3fce2c3ba58", "name": "ecs.ami-id" }, { "name": "ecs.capability.secrets.asm.bootstrap.log-driver" }, { "name": "com.amazonaws.ecs.capability.logging-driver.none" }, { "name": "ecs.capability.ecr-endpoint" }, { "name": "com.amazonaws.ecs.capability.logging-driver.json-file" }, { "value": "vpc-1234567890123467", "name": "ecs.vpc-id" }, { "name": "ecs.capability.execution-role-awslogs" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.17" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.18" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.19" }, { "name": "ecs.capability.docker-plugin.local" }, { "name": "ecs.capability.task-eni" }, { "name": "ecs.capability.task-cpu-mem-limit" }, { "name": "ecs.capability.secrets.ssm.bootstrap.log-driver" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.30" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.31" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.32" }, { "name": "ecs.capability.execution-role-ecr-pull" }, { "name": "ecs.capability.container-health-check" }, { "value": "subnet-1234567890123467", "name": "ecs.subnet-id" }, { "value": "us-west-2a", "name": "ecs.availability-zone" }, { "value": "t2.micro", "name": "ecs.instance-type" }, { "name": "com.amazonaws.ecs.capability.task-iam-role-network-host" }, { "name": "ecs.capability.aws-appmesh" }, { "name": "com.amazonaws.ecs.capability.logging-driver.awslogs" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.24" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.25" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.26" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.27" }, { "name": "com.amazonaws.ecs.capability.privileged-container" }, { "name": "ecs.capability.container-ordering" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.28" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.29" }, { "value": "x86_64", "name": "ecs.cpu-architecture" }, { "value": "93f43776-2018.10.0", "name": "ecs.capability.cni-plugin-version" }, { "name": "ecs.capability.secrets.ssm.environment-variables" }, { "name": "ecs.capability.pid-ipc-namespace-sharing" }, { "name": "com.amazonaws.ecs.capability.ecr-auth" }, { "value": "linux", "name": "ecs.os-type" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.20" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.21" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.22" }, { "name": "ecs.capability.task-eia" }, { "name": "ecs.capability.private-registry-authentication.secretsmanager" }, { "name": "com.amazonaws.ecs.capability.task-iam-role" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.23" } ], "pendingTasksCount": 0, "tags": [], "containerInstanceArn": "arn:aws:ecs:us-west-2:123456789012:container-instance/a1b2c3d4-5678-90ab-cdef-11111EXAMPLE", "registeredResources": [ { "integerValue": 1024, "doubleValue": 0.0, "type": "INTEGER", "longValue": 0, "name": "CPU" }, { "integerValue": 985, "doubleValue": 0.0, "type": "INTEGER", "longValue": 0, "name": "MEMORY" }, { "type": "STRINGSET", "integerValue": 0, "name": "PORTS", "stringSetValue": [ "22", "2376", "2375", "51678", "51679" ], "longValue": 0, "doubleValue": 0.0 }, { "type": "STRINGSET", "integerValue": 0, "name": "PORTS_UDP", "stringSetValue": [], "longValue": 0, "doubleValue": 0.0 } ], "status": "INACTIVE", "registeredAt": 1557768075.681, "version": 4, "versionInfo": { "agentVersion": "1.27.0", "agentHash": "aabe65ee", "dockerVersion": "DockerVersion: 18.06.1-ce" }, "attachments": [], "runningTasksCount": 0, "ec2InstanceId": "i-12345678901234678" } }
有关更多信息,请参阅《ECS开发人员指南》中的注销容器实例。
-
有关API详细信息,请参阅 “DeregisterContainerInstance AWS CLI
命令参考”。
-
以下代码示例显示了如何使用deregister-task-definition
。
- AWS CLI
-
取消注册任务定义
以下
deregister-task-definition
示例取消注册默认区域中curler
任务定义的第一个修订版。aws ecs deregister-task-definition --task-definition
curler:1
请注意,在生成的输出中,任务定义状态显示
INACTIVE
:{ "taskDefinition": { "status": "INACTIVE", "family": "curler", "volumes": [], "taskDefinitionArn": "arn:aws:ecs:us-west-2:123456789012:task-definition/curler:1", "containerDefinitions": [ { "environment": [], "name": "curler", "mountPoints": [], "image": "curl:latest", "cpu": 100, "portMappings": [], "entryPoint": [], "memory": 256, "command": [ "curl -v http://example.com/" ], "essential": true, "volumesFrom": [] } ], "revision": 1 } }
有关更多信息,请参阅《亚马逊ECS开发者指南》中的亚马逊ECS任务定义。
-
有关API详细信息,请参阅 “DeregisterTaskDefinition AWS CLI
命令参考”。
-
以下代码示例显示了如何使用describe-capacity-providers
。
- AWS CLI
-
示例 1:描述所有容量提供商
以下
describe-capacity-providers
示例检索有关所有容量提供商的详细信息。aws ecs describe-capacity-providers
输出:
{ "capacityProviders": [ { "capacityProviderArn": "arn:aws:ecs:us-west-2:123456789012:capacity-provider/MyCapacityProvider", "name": "MyCapacityProvider", "status": "ACTIVE", "autoScalingGroupProvider": { "autoScalingGroupArn": "arn:aws:autoscaling:us-west-2:123456789012:autoScalingGroup:a1b2c3d4-5678-90ab-cdef-EXAMPLE11111:autoScalingGroupName/MyAutoScalingGroup", "managedScaling": { "status": "ENABLED", "targetCapacity": 100, "minimumScalingStepSize": 1, "maximumScalingStepSize": 1000 }, "managedTerminationProtection": "ENABLED" }, "tags": [] }, { "capacityProviderArn": "arn:aws:ecs:us-west-2:123456789012:capacity-provider/FARGATE", "name": "FARGATE", "status": "ACTIVE", "tags": [] }, { "capacityProviderArn": "arn:aws:ecs:us-west-2:123456789012:capacity-provider/FARGATE_SPOT", "name": "FARGATE_SPOT", "status": "ACTIVE", "tags": [] } ] }
有关更多信息,请参阅 Amazon ECS 开发者指南中的集群容量提供商。
示例 2:描述特定的容量提供商
以下
describe-capacity-providers
示例检索有关特定容量提供商的详细信息。使用该--include TAGS
参数会将与容量提供商关联的标签添加到输出中。aws ecs describe-capacity-providers \ --capacity-providers
MyCapacityProvider
\ --includeTAGS
输出:
{ "capacityProviders": [ { "capacityProviderArn": "arn:aws:ecs:us-west-2:123456789012:capacity-provider/MyCapacityProvider", "name": "MyCapacityProvider", "status": "ACTIVE", "autoScalingGroupProvider": { "autoScalingGroupArn": "arn:aws:autoscaling:us-west-2:123456789012:autoScalingGroup:a1b2c3d4-5678-90ab-cdef-EXAMPLE11111:autoScalingGroupName/MyAutoScalingGroup", "managedScaling": { "status": "ENABLED", "targetCapacity": 100, "minimumScalingStepSize": 1, "maximumScalingStepSize": 1000 }, "managedTerminationProtection": "ENABLED" }, "tags": [ { "key": "environment", "value": "production" } ] } ] }
有关更多信息,请参阅 Amazon ECS 开发者指南中的集群容量提供商。
-
有关API详细信息,请参阅 “DescribeCapacityProviders AWS CLI
命令参考”。
-
以下代码示例显示了如何使用describe-clusters
。
- AWS CLI
-
示例 1:描述集群
以下
describe-clusters
示例将检索指定集群的详细信息。aws ecs describe-clusters \ --cluster
default
输出:
{ "clusters": [ { "status": "ACTIVE", "clusterName": "default", "registeredContainerInstancesCount": 0, "pendingTasksCount": 0, "runningTasksCount": 0, "activeServicesCount": 1, "clusterArn": "arn:aws:ecs:us-west-2:123456789012:cluster/default" } ], "failures": [] }
有关更多信息,请参阅《亚马逊ECS开发者指南》中的亚马逊ECS集群。
示例 2:描述带有附加选项的集群
以下
describe-clusters
示例指定了该ATTACHMENTS选项。它以附件的形式检索有关指定集群的详细信息以及附加到集群的资源列表。在集群中使用容量提供程序时,资源(无论是 AutoScaling 计划还是扩展策略)都将以 asp 或 as_p ATTACHMENTS olicy 表示。aws ecs describe-clusters \ --include
ATTACHMENTS
\ --clusterssampleCluster
输出:
{ "clusters": [ { "clusterArn": "arn:aws:ecs:af-south-1:123456789222:cluster/sampleCluster", "clusterName": "sampleCluster", "status": "ACTIVE", "registeredContainerInstancesCount": 0, "runningTasksCount": 0, "pendingTasksCount": 0, "activeServicesCount": 0, "statistics": [], "tags": [], "settings": [], "capacityProviders": [ "sampleCapacityProvider" ], "defaultCapacityProviderStrategy": [], "attachments": [ { "id": "a1b2c3d4-5678-901b-cdef-EXAMPLE22222", "type": "as_policy", "status": "CREATED", "details": [ { "name": "capacityProviderName", "value": "sampleCapacityProvider" }, { "name": "scalingPolicyName", "value": "ECSManagedAutoScalingPolicy-3048e262-fe39-4eaf-826d-6f975d303188" } ] } ], "attachmentsStatus": "UPDATE_COMPLETE" } ], "failures": [] }
有关更多信息,请参阅《亚马逊ECS开发者指南》中的亚马逊ECS集群。
-
有关API详细信息,请参阅 “DescribeClusters AWS CLI
命令参考”。
-
以下代码示例显示了如何使用describe-container-instances
。
- AWS CLI
-
描述容器实例
以下
describe-container-instances
示例使用容器实例UUID作为标识符检索update
集群中容器实例的详细信息。aws ecs describe-container-instances \ --cluster
update
\ --container-instancesa1b2c3d4-5678-90ab-cdef-11111EXAMPLE
输出:
{ "failures": [], "containerInstances": [ { "status": "ACTIVE", "registeredResources": [ { "integerValue": 2048, "longValue": 0, "type": "INTEGER", "name": "CPU", "doubleValue": 0.0 }, { "integerValue": 3955, "longValue": 0, "type": "INTEGER", "name": "MEMORY", "doubleValue": 0.0 }, { "name": "PORTS", "longValue": 0, "doubleValue": 0.0, "stringSetValue": [ "22", "2376", "2375", "51678" ], "type": "STRINGSET", "integerValue": 0 } ], "ec2InstanceId": "i-A1B2C3D4", "agentConnected": true, "containerInstanceArn": "arn:aws:ecs:us-west-2:123456789012:container-instance/a1b2c3d4-5678-90ab-cdef-11111EXAMPLE", "pendingTasksCount": 0, "remainingResources": [ { "integerValue": 2048, "longValue": 0, "type": "INTEGER", "name": "CPU", "doubleValue": 0.0 }, { "integerValue": 3955, "longValue": 0, "type": "INTEGER", "name": "MEMORY", "doubleValue": 0.0 }, { "name": "PORTS", "longValue": 0, "doubleValue": 0.0, "stringSetValue": [ "22", "2376", "2375", "51678" ], "type": "STRINGSET", "integerValue": 0 } ], "runningTasksCount": 0, "versionInfo": { "agentVersion": "1.0.0", "agentHash": "4023248", "dockerVersion": "DockerVersion: 1.5.0" } } ] }
有关更多信息,请参阅《亚马逊ECS开发者指南》中的亚马逊ECS容器实例。
-
有关API详细信息,请参阅 “DescribeContainerInstances AWS CLI
命令参考”。
-
以下代码示例显示了如何使用describe-services
。
- AWS CLI
-
描述一项服务
以下
describe-services
示例检索默认集群中my-http-service
服务的详细信息。aws ecs describe-services --services
my-http-service
输出:
{ "services": [ { "status": "ACTIVE", "taskDefinition": "arn:aws:ecs:us-west-2:123456789012:task-definition/amazon-ecs-sample:1", "pendingCount": 0, "loadBalancers": [], "desiredCount": 10, "createdAt": 1466801808.595, "serviceName": "my-http-service", "clusterArn": "arn:aws:ecs:us-west-2:123456789012:cluster/default", "serviceArn": "arn:aws:ecs:us-west-2:123456789012:service/my-http-service", "deployments": [ { "status": "PRIMARY", "pendingCount": 0, "createdAt": 1466801808.595, "desiredCount": 10, "taskDefinition": "arn:aws:ecs:us-west-2:123456789012:task-definition/amazon-ecs-sample:1", "updatedAt": 1428326312.703, "id": "ecs-svc/1234567890123456789", "runningCount": 10 } ], "events": [ { "message": "(service my-http-service) has reached a steady state.", "id": "a1b2c3d4-5678-90ab-cdef-11111EXAMPLE", "createdAt": 1466801812.435 } ], "runningCount": 10 } ], "failures": [] }
有关更多信息,请参阅《Amazon ECS 开发者指南》中的服务。
-
有关API详细信息,请参阅 “DescribeServices AWS CLI
命令参考”。
-
以下代码示例显示了如何使用describe-task-definition
。
- AWS CLI
-
描述任务定义
以下
describe-task-definition
示例检索任务定义的详细信息。aws ecs describe-task-definition \ --task-definition
hello_world:8
输出:
{ "taskDefinition": { "taskDefinitionArn": "arn:aws:ecs:us-east-1:012345678910:task-definition/hello_world:8", "containerDefinitions": [ { "cpu": 10, "environment": [], "essential": true, "image": "wordpress", "links": [ "mysql" ] , "memory": 500, "mountPoints": [], "name": "wordpress", "portMappings": [ { "containerPort": 80, "hostPort": 80 } ], "volumesFrom": [] }, { "cpu": 10, "environment": [ { "name": "MYSQL_ROOT_PASSWORD", "value": "password" } ], "essential": true, "image": "mysql", "memory": 500, "mountPoints": [], "name": "mysql", "portMappings": [], "volumesFrom": [] } ], "family": "hello_world", "revision": 8, "volumes": [], "status": "ACTIVE", "placementConstraints": [], "compatibilities": [ "EXTERNAL", "EC2" ], "registeredAt": "2024-06-21T11:15:12.669000-05:00", "registeredBy": "arn:aws:sts::012345678910:assumed-role/demo-role/jane-doe" }, "tags": [] }
有关更多信息,请参阅《亚马逊ECS开发者指南》中的亚马逊ECS任务定义。
-
有关API详细信息,请参阅 “DescribeTaskDefinition AWS CLI
命令参考”。
-
以下代码示例显示了如何使用describe-task-sets
。
- AWS CLI
-
描述任务集
以下
describe-task-sets
示例描述了使用外部部署程序的服务中的任务集。aws ecs describe-task-sets \ --cluster
MyCluster
\ --serviceMyService
\ --task-setsarn:aws:ecs:us-west-2:123456789012:task-set/MyCluster/MyService/ecs-svc/1234567890123456789
输出:
{ "taskSets": [ { "id": "ecs-svc/1234567890123456789", "taskSetArn": "arn:aws:ecs:us-west-2:123456789012:task-set/MyCluster/MyService/ecs-svc/1234567890123456789", "status": "ACTIVE", "taskDefinition": "arn:aws:ecs:us-west-2:123456789012:task-definition/sample-fargate:2", "computedDesiredCount": 0, "pendingCount": 0, "runningCount": 0, "createdAt": 1557207715.195, "updatedAt": 1557207740.014, "launchType": "EC2", "networkConfiguration": { "awsvpcConfiguration": { "subnets": [ "subnet-12344321" ], "securityGroups": [ "sg-1234431" ], "assignPublicIp": "DISABLED" } }, "loadBalancers": [], "serviceRegistries": [], "scale": { "value": 0.0, "unit": "PERCENT" }, "stabilityStatus": "STEADY_STATE", "stabilityStatusAt": 1557207740.014 } ], "failures": [] }
-
有关API详细信息,请参阅 “DescribeTaskSets AWS CLI
命令参考”。
-
以下代码示例显示了如何使用describe-tasks
。
- AWS CLI
-
示例 1:描述单个任务
以下
describe-tasks
示例将检索集群中任务的详细信息。您可以使用任务的 ID 或完整ARN信息来指定任务。此示例使用了任务ARN的全部内容。aws ecs describe-tasks \ --cluster
MyCluster
\ --tasksarn:aws:ecs:us-east-1:123456789012:task/MyCluster/4d590253bb114126b7afa7b58EXAMPLE
输出:
{ "tasks": [ { "attachments": [], "attributes": [ { "name": "ecs.cpu-architecture", "value": "x86_64" } ], "availabilityZone": "us-east-1b", "clusterArn": "arn:aws:ecs:us-east-1:123456789012:cluster/MyCluster", "connectivity": "CONNECTED", "connectivityAt": "2021-08-11T12:21:26.681000-04:00", "containerInstanceArn": "arn:aws:ecs:us-east-1:123456789012:container-instance/test/025c7e2c5e054a6790a29fc1fEXAMPLE", "containers": [ { "containerArn": "arn:aws:ecs:us-east-1:123456789012:container/MyCluster/4d590253bb114126b7afa7b58eea9221/a992d1cc-ea46-474a-b6e8-24688EXAMPLE", "taskArn": "arn:aws:ecs:us-east-1:123456789012:task/MyCluster/4d590253bb114126b7afa7b58EXAMPLE", "name": "simple-app", "image": "httpd:2.4", "runtimeId": "91251eed27db90006ad67b1a08187290869f216557717dd5c39b37c94EXAMPLE", "lastStatus": "RUNNING", "networkBindings": [ { "bindIP": "0.0.0.0", "containerPort": 80, "hostPort": 80, "protocol": "tcp" } ], "networkInterfaces": [], "healthStatus": "UNKNOWN", "cpu": "10", "memory": "300" } ], "cpu": "10", "createdAt": "2021-08-11T12:21:26.681000-04:00", "desiredStatus": "RUNNING", "enableExecuteCommand": false, "group": "service:testupdate", "healthStatus": "UNKNOWN", "lastStatus": "RUNNING", "launchType": "EC2", "memory": "300", "overrides": { "containerOverrides": [ { "name": "simple-app" } ], "inferenceAcceleratorOverrides": [] }, "pullStartedAt": "2021-08-11T12:21:28.234000-04:00", "pullStoppedAt": "2021-08-11T12:21:33.793000-04:00", "startedAt": "2021-08-11T12:21:34.945000-04:00", "startedBy": "ecs-svc/968695068243EXAMPLE", "tags": [], "taskArn": "arn:aws:ecs:us-east-1:123456789012:task/MyCluster/4d590253bb114126b7afa7b58eea9221", "taskDefinitionArn": "arn:aws:ecs:us-east-1:123456789012:task-definition/console-sample-app-static2:1", "version": 2 } ], "failures": [] }
有关更多信息,请参阅《亚马逊ECS开发者指南》中的亚马逊ECS任务定义。
示例 2:描述多个任务
以下
describe-tasks
示例将检索集群中多个任务的详细信息。您可以使用任务的 ID 或完整ARN信息来指定任务。此示例使用IDs了全部任务。aws ecs describe-tasks \ --cluster
MyCluster
\ --tasks"74de0355a10a4f979ac495c14EXAMPLE"
"d789e94343414c25b9f6bd59eEXAMPLE"
输出:
{ "tasks": [ { "attachments": [ { "id": "d9e7735a-16aa-4128-bc7a-b2d51EXAMPLE", "type": "ElasticNetworkInterface", "status": "ATTACHED", "details": [ { "name": "subnetId", "value": "subnet-0d0eab1bb3EXAMPLE" }, { "name": "networkInterfaceId", "value": "eni-0fa40520aeEXAMPLE" }, { "name": "macAddress", "value": "0e:89:76:28:07:b3" }, { "name": "privateDnsName", "value": "ip-10-0-1-184.ec2.internal" }, { "name": "privateIPv4Address", "value": "10.0.1.184" } ] } ], "attributes": [ { "name": "ecs.cpu-architecture", "value": "x86_64" } ], "availabilityZone": "us-east-1b", "clusterArn": "arn:aws:ecs:us-east-1:123456789012:cluster/MyCluster", "connectivity": "CONNECTED", "connectivityAt": "2021-12-20T12:13:37.875000-05:00", "containers": [ { "containerArn": "arn:aws:ecs:us-east-1:123456789012:container/MyCluster/74de0355a10a4f979ac495c14EXAMPLE/aad3ba00-83b3-4dac-84d4-11f8cEXAMPLE", "taskArn": "arn:aws:ecs:us-east-1:123456789012:task/MyCluster/74de0355a10a4f979ac495c14EXAMPLE", "name": "web", "image": "nginx", "runtimeId": "74de0355a10a4f979ac495c14EXAMPLE-265927825", "lastStatus": "RUNNING", "networkBindings": [], "networkInterfaces": [ { "attachmentId": "d9e7735a-16aa-4128-bc7a-b2d51EXAMPLE", "privateIpv4Address": "10.0.1.184" } ], "healthStatus": "UNKNOWN", "cpu": "99", "memory": "100" } ], "cpu": "256", "createdAt": "2021-12-20T12:13:20.226000-05:00", "desiredStatus": "RUNNING", "enableExecuteCommand": false, "group": "service:tdsevicetag", "healthStatus": "UNKNOWN", "lastStatus": "RUNNING", "launchType": "FARGATE", "memory": "512", "overrides": { "containerOverrides": [ { "name": "web" } ], "inferenceAcceleratorOverrides": [] }, "platformVersion": "1.4.0", "platformFamily": "Linux", "pullStartedAt": "2021-12-20T12:13:42.665000-05:00", "pullStoppedAt": "2021-12-20T12:13:46.543000-05:00", "startedAt": "2021-12-20T12:13:48.086000-05:00", "startedBy": "ecs-svc/988401040018EXAMPLE", "tags": [], "taskArn": "arn:aws:ecs:us-east-1:123456789012:task/MyCluster/74de0355a10a4f979ac495c14EXAMPLE", "taskDefinitionArn": "arn:aws:ecs:us-east-1:123456789012:task-definition/webserver:2", "version": 3, "ephemeralStorage": { "sizeInGiB": 20 } }, { "attachments": [ { "id": "214eb5a9-45cd-4bf8-87bc-57fefEXAMPLE", "type": "ElasticNetworkInterface", "status": "ATTACHED", "details": [ { "name": "subnetId", "value": "subnet-0d0eab1bb3EXAMPLE" }, { "name": "networkInterfaceId", "value": "eni-064c7766daEXAMPLE" }, { "name": "macAddress", "value": "0e:76:83:01:17:a9" }, { "name": "privateDnsName", "value": "ip-10-0-1-41.ec2.internal" }, { "name": "privateIPv4Address", "value": "10.0.1.41" } ] } ], "attributes": [ { "name": "ecs.cpu-architecture", "value": "x86_64" } ], "availabilityZone": "us-east-1b", "clusterArn": "arn:aws:ecs:us-east-1:123456789012:cluster/MyCluster", "connectivity": "CONNECTED", "connectivityAt": "2021-12-20T12:13:35.243000-05:00", "containers": [ { "containerArn": "arn:aws:ecs:us-east-1:123456789012:container/MyCluster/d789e94343414c25b9f6bd59eEXAMPLE/9afef792-609b-43a5-bb6a-3efdbEXAMPLE", "taskArn": "arn:aws:ecs:us-east-1:123456789012:task/MyCluster/d789e94343414c25b9f6bd59eEXAMPLE", "name": "web", "image": "nginx", "runtimeId": "d789e94343414c25b9f6bd59eEXAMPLE-265927825", "lastStatus": "RUNNING", "networkBindings": [], "networkInterfaces": [ { "attachmentId": "214eb5a9-45cd-4bf8-87bc-57fefEXAMPLE", "privateIpv4Address": "10.0.1.41" } ], "healthStatus": "UNKNOWN", "cpu": "99", "memory": "100" } ], "cpu": "256", "createdAt": "2021-12-20T12:13:20.226000-05:00", "desiredStatus": "RUNNING", "enableExecuteCommand": false, "group": "service:tdsevicetag", "healthStatus": "UNKNOWN", "lastStatus": "RUNNING", "launchType": "FARGATE", "memory": "512", "overrides": { "containerOverrides": [ { "name": "web" } ], "inferenceAcceleratorOverrides": [] }, "platformVersion": "1.4.0", "platformFamily": "Linux", "pullStartedAt": "2021-12-20T12:13:44.611000-05:00", "pullStoppedAt": "2021-12-20T12:13:48.251000-05:00", "startedAt": "2021-12-20T12:13:49.326000-05:00", "startedBy": "ecs-svc/988401040018EXAMPLE", "tags": [], "taskArn": "arn:aws:ecs:us-east-1:123456789012:task/MyCluster/d789e94343414c25b9f6bd59eEXAMPLE", "taskDefinitionArn": "arn:aws:ecs:us-east-1:123456789012:task-definition/webserver:2", "version": 3, "ephemeralStorage": { "sizeInGiB": 20 } } ], "failures": [] }
有关更多信息,请参阅《亚马逊ECS开发者指南》中的亚马逊ECS任务定义。
-
有关API详细信息,请参阅 “DescribeTasks AWS CLI
命令参考”。
-
以下代码示例显示了如何使用execute-command
。
- AWS CLI
-
运行interactive /bin/sh命令
以下
execute-command
示例对名为 ID MyContainer 为的任务的容器运行interactive /bin/sh命令arn:aws:ecs:us-east-1:123456789012:task/MyCluster/d789e94343414c25b9f6bd59eEXAMPLE
。aws ecs execute-command \ --cluster
MyCluster
\ --taskarn:aws:ecs:us-east-1:123456789012:task/MyCluster/d789e94343414c25b9f6bd59eEXAMPLE
\ --containerMyContainer
\ --interactive \ --command"/bin/sh"
此命令不生成任何输出。
有关更多信息,请参阅《亚马逊ECS开发者指南》中的使用 Amazon ECS Exec 进行调试。
-
有关API详细信息,请参阅 “ExecuteCommand AWS CLI
命令参考”。
-
以下代码示例显示了如何使用list-account-settings
。
- AWS CLI
-
示例 1:查看账户的账户设置
以下
list-account-settings
示例显示了账户的有效账户设置。aws ecs list-account-settings --effective-settings
输出:
{ "settings": [ { "name": "containerInstanceLongArnFormat", "value": "enabled", "principalArn": "arn:aws:iam::123456789012:root" }, { "name": "serviceLongArnFormat", "value": "enabled", "principalArn": "arn:aws:iam::123456789012:root" }, { "name": "taskLongArnFormat", "value": "enabled", "principalArn": "arn:aws:iam::123456789012:root" } ] }
示例 2:查看特定IAM用户或IAM角色的账户设置
以下
list-account-settings
示例显示了指定IAM用户或IAM角色的账户设置。aws ecs list-account-settings --principal-arn
arn:aws:iam::123456789012:user/MyUser
输出:
{ "settings": [ { "name": "serviceLongArnFormat", "value": "enabled", "principalArn": "arn:aws:iam::123456789012:user/MyUser" } ] }
有关更多信息,请参阅亚马逊资源名称 (ARNs) 和IDs亚马逊ECS开发者指南。
-
有关API详细信息,请参阅 “ListAccountSettings AWS CLI
命令参考”。
-
以下代码示例显示了如何使用list-attributes
。
- AWS CLI
-
列出包含特定属性的容器实例
以下示例列出了默认集群中具有该
stack=production
属性的容器实例的属性。aws ecs list-attributes \ --target-type
container-instance
\ --attribute-namestack
\ --attribute-valueproduction
\ --clusterdefault
输出:
{ "attributes": [ { "name": "stack", "targetId": "arn:aws:ecs:us-west-2:130757420319:container-instance/1c3be8ed-df30-47b4-8f1e-6e68ebd01f34", "value": "production" } ] }
有关更多信息,请参阅《亚马逊ECS开发者指南》中的亚马逊ECS容器代理配置。
-
有关API详细信息,请参阅 “ListAttributes AWS CLI
命令参考”。
-
以下代码示例显示了如何使用list-clusters
。
- AWS CLI
-
列出您的可用集群
以下
list-clusters
示例将列出所有可用的集群。aws ecs list-clusters
输出:
{ "clusterArns": [ "arn:aws:ecs:us-west-2:123456789012:cluster/MyECSCluster1", "arn:aws:ecs:us-west-2:123456789012:cluster/AnotherECSCluster" ] }
有关更多信息,请参阅《亚马逊ECS开发者指南》中的亚马逊ECS集群。
-
有关API详细信息,请参阅 “ListClusters AWS CLI
命令参考”。
-
以下代码示例显示了如何使用list-container-instances
。
- AWS CLI
-
列出集群中的容器实例
以下
list-container-instances
示例列出了集群中所有可用的容器实例。aws ecs list-container-instances --cluster
MyCluster
输出:
{ "containerInstanceArns": [ "arn:aws:ecs:us-west-2:123456789012:container-instance/MyCluster/a1b2c3d4-5678-90ab-cdef-11111EXAMPLE", "arn:aws:ecs:us-west-2:123456789012:container-instance/MyCluster/a1b2c3d4-5678-90ab-cdef-22222EXAMPLE" ] }
有关更多信息,请参阅《亚马逊ECS开发者指南》中的亚马逊ECS容器实例。
-
有关API详细信息,请参阅 “ListContainerInstances AWS CLI
命令参考”。
-
以下代码示例显示了如何使用list-services-by-namespace
。
- AWS CLI
-
列出命名空间中的服务
以下
list-services-by-namespace
示例列出了在您的默认区域中为指定命名空间配置的所有服务。aws ecs list-services-by-namespace \ --namespace
service-connect
输出:
{ "serviceArns": [ "arn:aws:ecs:us-west-2:123456789012:service/MyCluster/MyService", "arn:aws:ecs:us-west-2:123456789012:service/tutorial/service-connect-nginx-service" ] }
有关更多信息,请参阅《亚马逊ECS开发者指南》中的 S ervice Connect。
-
有关API详细信息,请参阅 “ListServicesByNamespace AWS CLI
命令参考”。
-
以下代码示例显示了如何使用list-services
。
- AWS CLI
-
列出集群中的服务
以下
list-services
示例演示如何列出集群中运行的服务。aws ecs list-services --cluster
MyCluster
输出:
{ "serviceArns": [ "arn:aws:ecs:us-west-2:123456789012:service/MyCluster/MyService" ] }
有关更多信息,请参阅《Amazon ECS 开发者指南》中的服务。
-
有关API详细信息,请参阅 “ListServices AWS CLI
命令参考”。
-
以下代码示例显示了如何使用list-tags-for-resource
。
- AWS CLI
-
列出资源的标签
以下
list-tags-for-resource
示例列出了特定集群的标签。aws ecs list-tags-for-resource \ --resource-arn
arn:aws:ecs:us-west-2:123456789012:cluster/MyCluster
输出:
{ "tags": [ { "key": "key1", "value": "value1" }, { "key": "key2", "value": "value2" }, { "key": "key3", "value": "value3" } ] }
-
有关API详细信息,请参阅 “ListTagsForResource AWS CLI
命令参考”。
-
以下代码示例显示了如何使用list-task-definition-families
。
- AWS CLI
-
示例 1:列出已注册的任务定义系列
以下
list-task-definition-families
示例列出了所有已注册的任务定义系列。aws ecs list-task-definition-families
输出:
{ "families": [ "node-js-app", "web-timer", "hpcc", "hpcc-c4-8xlarge" ] }
示例 2:筛选已注册的任务定义系列
以下
list-task-definition-families
示例列出了以 “hpcc” 开头的任务定义修订版。aws ecs list-task-definition-families --family-prefix
hpcc
输出:
{ "families": [ "hpcc", "hpcc-c4-8xlarge" ] }
有关更多信息,请参阅 Amazon ECS 开发者指南中的任务定义参数。
-
有关API详细信息,请参阅 “ListTaskDefinitionFamilies AWS CLI
命令参考”。
-
以下代码示例显示了如何使用list-task-definitions
。
- AWS CLI
-
示例 1:列出已注册的任务定义
以下
list-task-definitions
示例列出了所有已注册的任务定义。aws ecs list-task-definitions
输出:
{ "taskDefinitionArns": [ "arn:aws:ecs:us-west-2:123456789012:task-definition/sleep300:2", "arn:aws:ecs:us-west-2:123456789012:task-definition/sleep360:1", "arn:aws:ecs:us-west-2:123456789012:task-definition/wordpress:3", "arn:aws:ecs:us-west-2:123456789012:task-definition/wordpress:4", "arn:aws:ecs:us-west-2:123456789012:task-definition/wordpress:5", "arn:aws:ecs:us-west-2:123456789012:task-definition/wordpress:6" ] }
示例 2:列出家族中已注册的任务定义
以下 list-task-definitions示例列出了指定系列的任务定义修订版。
aws ecs list-task-definitions --family-prefix
wordpress
输出:
{ "taskDefinitionArns": [ "arn:aws:ecs:us-west-2:123456789012:task-definition/wordpress:3", "arn:aws:ecs:us-west-2:123456789012:task-definition/wordpress:4", "arn:aws:ecs:us-west-2:123456789012:task-definition/wordpress:5", "arn:aws:ecs:us-west-2:123456789012:task-definition/wordpress:6" ] }
有关更多信息,请参阅《亚马逊ECS开发者指南》中的亚马逊ECS任务定义。
-
有关API详细信息,请参阅 “ListTaskDefinitions AWS CLI
命令参考”。
-
以下代码示例显示了如何使用list-tasks
。
- AWS CLI
-
示例 1:列出集群中的任务
以下
list-tasks
示例将列出集群中的所有任务。aws ecs list-tasks --cluster
default
输出:
{ "taskArns": [ "arn:aws:ecs:us-west-2:123456789012:task/a1b2c3d4-5678-90ab-cdef-11111EXAMPLE", "arn:aws:ecs:us-west-2:123456789012:task/a1b2c3d4-5678-90ab-cdef-22222EXAMPLE" ] }
示例 2:列出特定容器实例上的任务
以下
list-tasks
示例使用容器实例UUID作为筛选器列出了容器实例上的任务。aws ecs list-tasks --cluster
default
--container-instancea1b2c3d4-5678-90ab-cdef-33333EXAMPLE
输出:
{ "taskArns": [ "arn:aws:ecs:us-west-2:123456789012:task/a1b2c3d4-5678-90ab-cdef-44444EXAMPLE" ] }
有关更多信息,请参阅《亚马逊ECS开发者指南》中的亚马逊ECS任务定义。
-
有关API详细信息,请参阅 “ListTasks AWS CLI
命令参考”。
-
以下代码示例显示了如何使用put-account-setting-default
。
- AWS CLI
-
修改默认账户设置
以下
put-account-setting-default
示例修改了您账户中所有IAM用户或角色的默认账户设置。除非IAM用户或角色为自己明确覆盖这些设置,否则这些更改将适用于整个 AWS 账户。aws ecs put-account-setting-default --name
serviceLongArnFormat
--valueenabled
输出:
{ "setting": { "name": "serviceLongArnFormat", "value": "enabled", "principalArn": "arn:aws:iam::123456789012:root" } }
有关更多信息,请参阅亚马逊资源名称 (ARNs) 和IDs亚马逊ECS开发者指南。
-
有关API详细信息,请参阅 “PutAccountSettingDefault AWS CLI
命令参考”。
-
以下代码示例显示了如何使用put-account-setting
。
- AWS CLI
-
修改IAM用户账户的账户设置
以下
put-account-setting
示例为您的IAM用户serviceLongArnFormat
账户启用账户设置。aws ecs put-account-setting --name
serviceLongArnFormat
--valueenabled
输出:
{ "setting": { "name": "serviceLongArnFormat", "value": "enabled", "principalArn": "arn:aws:iam::130757420319:user/your_username" } }
有关更多信息,请参阅《Amazon ECS 开发者指南》中的修改账户设置。
-
有关API详细信息,请参阅 “PutAccountSetting AWS CLI
命令参考”。
-
以下代码示例显示了如何使用put-account-settings
。
- AWS CLI
-
修改IAM用户或IAM角色的帐户设置
以下
put-account-setting
示例修改了指定IAM用户或IAM角色的账户设置。aws ecs put-account-setting \ --name
serviceLongArnFormat
\ --valueenabled
\ --principal-arnarn:aws:iam::123456789012:user/MyUser
输出:
{ "setting": { "name": "serviceLongArnFormat", "value": "enabled", "principalArn": "arn:aws:iam::123456789012:user/MyUser" } }
-
有关API详细信息,请参阅 “PutAccountSettings AWS CLI
命令参考”。
-
以下代码示例显示了如何使用put-attributes
。
- AWS CLI
-
创建属性并将其与 Amazon ECS 资源关联
以下内容
put-attributes
将名称为 stack 且值为 production 的属性应用于容器实例。aws ecs put-attributes \ --attributes
name=stack,value=production,targetId=arn:aws:ecs:us-west-2:130757420319:container-instance/1c3be8ed-df30-47b4-8f1e-6e68ebd01f34
输出:
{ "attributes": [ { "name": "stack", "targetId": "arn:aws:ecs:us-west-2:130757420319:container-instance/1c3be8ed-df30-47b4-8f1e-6e68ebd01f34", "value": "production" } ] }
-
有关API详细信息,请参阅 “PutAttributes AWS CLI
命令参考”。
-
以下代码示例显示了如何使用put-cluster-capacity-providers
。
- AWS CLI
-
示例 1:向集群添加现有容量提供商
以下
put-cluster-capacity-providers
示例将现有容量提供程序添加到集群。使用create-capacity-provider
命令创建容量提供程序。该describe-clusters
命令用于描述当前的容量提供商以及与集群相关的默认容量提供者策略。向集群添加新的容量提供商时,除了要与集群关联的新容量提供商外,还必须指定所有现有的容量提供商。您还必须指定要与集群关联的默认容量提供者策略。在此示例中,MyCluster
集群具有与之关联的MyCapacityProvider1
容量提供商,您希望添加MyCapacityProvider2
容量提供程序并将其包含在默认容量提供程序策略中,以便任务在两个容量提供商之间均匀分配。aws ecs put-cluster-capacity-providers \ --cluster
MyCluster
\ --capacity-providersMyCapacityProvider1
MyCapacityProvider2
\ --default-capacity-provider-strategycapacityProvider=MyCapacityProvider1,weight=1
capacityProvider=MyCapacityProvider2,weight=1
输出:
{ "cluster": { "clusterArn": "arn:aws:ecs:us-west-2:123456789012:cluster/MyCluster", "clusterName": "MyCluster", "status": "ACTIVE", "registeredContainerInstancesCount": 0, "runningTasksCount": 0, "pendingTasksCount": 0, "activeServicesCount": 0, "statistics": [], "tags": [], "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": "as_policy", "status": "ACTIVE", "details": [ { "name": "capacityProviderName", "value": "MyCapacityProvider1" }, { "name": "scalingPolicyName", "value": "ECSManagedAutoScalingPolicy-a1b2c3d4-5678-90ab-cdef-EXAMPLE11111" } ] }, { "id": "ae592060-2382-4663-9476-b015c685593c", "type": "as_policy", "status": "ACTIVE", "details": [ { "name": "capacityProviderName", "value": "MyCapacityProvider2" }, { "name": "scalingPolicyName", "value": "ECSManagedAutoScalingPolicy-a1b2c3d4-5678-90ab-cdef-EXAMPLE22222" } ] } ], "attachmentsStatus": "UPDATE_IN_PROGRESS" } }
有关更多信息,请参阅 Amazon ECS 开发者指南中的集群容量提供商。
示例 2:从集群中移除容量提供商
以下
put-cluster-capacity-providers
示例将容量提供程序从集群中移除。该describe-clusters
命令用于描述与集群关联的当前容量提供商。从集群中移除容量提供商时,必须指定要与集群保持关联的容量提供商,以及要与集群关联的默认容量提供者策略。在此示例中,集群具有与其关联的MyCapacityProvider1
和MyCapacityProvider2
容量提供程序,您想移除MyCapacityProvider2
容量提供商,因此您只能MyCapacityProvider1
在命令中以及更新的默认容量提供程序策略中指定。aws ecs put-cluster-capacity-providers \ --cluster
MyCluster
\ --capacity-providersMyCapacityProvider1
\ --default-capacity-provider-strategycapacityProvider=MyCapacityProvider1,weight=1,base=0
输出:
{ "cluster": { "clusterArn": "arn:aws:ecs:us-west-2:123456789012:cluster/MyCluster", "clusterName": "MyCluster", "status": "ACTIVE", "registeredContainerInstancesCount": 0, "runningTasksCount": 0, "pendingTasksCount": 0, "activeServicesCount": 0, "statistics": [], "tags": [], "settings": [ { "name": "containerInsights", "value": "enabled" } ], "capacityProviders": [ "MyCapacityProvider1" ], "defaultCapacityProviderStrategy": [ "capacityProvider": "MyCapacityProvider1", "weight": 1, "base": 0 ], "attachments": [ { "id": "0fb0c8f4-6edd-4de1-9b09-17e470ee1918", "type": "as_policy", "status": "ACTIVE", "details": [ { "name": "capacityProviderName", "value": "MyCapacityProvider1" }, { "name": "scalingPolicyName", "value": "ECSManagedAutoScalingPolicy-a1b2c3d4-5678-90ab-cdef-EXAMPLE11111" } ] }, { "id": "ae592060-2382-4663-9476-b015c685593c", "type": "as_policy", "status": "DELETING", "details": [ { "name": "capacityProviderName", "value": "MyCapacityProvider2" }, { "name": "scalingPolicyName", "value": "ECSManagedAutoScalingPolicy-a1b2c3d4-5678-90ab-cdef-EXAMPLE22222" } ] } ], "attachmentsStatus": "UPDATE_IN_PROGRESS" } }
有关更多信息,请参阅 Amazon ECS 开发者指南中的集群容量提供商。
示例 3:从集群中移除所有容量提供商
以下
put-cluster-capacity-providers
示例将所有现有的容量提供程序从集群中移除。aws ecs put-cluster-capacity-providers \ --cluster
MyCluster
\ --capacity-providers[]
\ --default-capacity-provider-strategy[]
输出:
{ "cluster": { "clusterArn": "arn:aws:ecs:us-west-2:123456789012:cluster/MyCluster", "clusterName": "MyCluster", "status": "ACTIVE", "registeredContainerInstancesCount": 0, "runningTasksCount": 0, "pendingTasksCount": 0, "activeServicesCount": 0, "statistics": [], "tags": [], "settings": [ { "name": "containerInsights", "value": "enabled" } ], "capacityProviders": [], "defaultCapacityProviderStrategy": [], "attachments": [ { "id": "0fb0c8f4-6edd-4de1-9b09-17e470ee1918", "type": "as_policy", "status": "DELETING", "details": [ { "name": "capacityProviderName", "value": "MyCapacityProvider1" }, { "name": "scalingPolicyName", "value": "ECSManagedAutoScalingPolicy-a1b2c3d4-5678-90ab-cdef-EXAMPLE11111" } ] }, { "id": "ae592060-2382-4663-9476-b015c685593c", "type": "as_policy", "status": "DELETING", "details": [ { "name": "capacityProviderName", "value": "MyCapacityProvider2" }, { "name": "scalingPolicyName", "value": "ECSManagedAutoScalingPolicy-a1b2c3d4-5678-90ab-cdef-EXAMPLE22222" } ] } ], "attachmentsStatus": "UPDATE_IN_PROGRESS" } }
有关更多信息,请参阅 Amazon ECS 开发者指南中的集群容量提供商。
-
有关API详细信息,请参阅 “PutClusterCapacityProviders AWS CLI
命令参考”。
-
以下代码示例显示了如何使用register-task-definition
。
- AWS CLI
-
示例 1:向JSON文件注册任务定义
以下
register-task-definition
示例将任务定义注册到指定的家族。容器定义以JSON格式保存在指定的文件位置。aws ecs register-task-definition \ --cli-input-json
file://<path_to_json_file>/sleep360.json
sleep360.json
的内容:{ "containerDefinitions": [ { "name": "sleep", "image": "busybox", "cpu": 10, "command": [ "sleep", "360" ], "memory": 10, "essential": true } ], "family": "sleep360" }
输出:
{ "taskDefinition": { "status": "ACTIVE", "family": "sleep360", "placementConstraints": [], "compatibilities": [ "EXTERNAL", "EC2" ], "volumes": [], "taskDefinitionArn": "arn:aws:ecs:us-east-1:123456789012:task-definition/sleep360:1", "containerDefinitions": [ { "environment": [], "name": "sleep", "mountPoints": [], "image": "busybox", "cpu": 10, "portMappings": [], "command": [ "sleep", "360" ], "memory": 10, "essential": true, "volumesFrom": [] } ], "revision": 1 } }
有关更多信息,请参阅《Amazon ECS 开发者指南》中的任务定义示例。
示例 2:使用JSON字符串参数注册任务定义
以下
register-task-definition
示例使用容器定义注册任务定义,该容器定义作为JSON字符串参数提供,带有转义的双引号。aws ecs register-task-definition \ --family
sleep360
\ --container-definitions "[{\"name\":\"sleep\",\"image\":\"busybox\",\"cpu\":10,\"command\":[\"sleep\",\"360\"],\"memory\":10,\"essential\":true}]"输出与前面的示例相同。
有关更多信息,请参阅《Amazon ECS 开发者指南》中的创建任务定义。
-
有关API详细信息,请参阅 “RegisterTaskDefinition AWS CLI
命令参考”。
-
以下代码示例显示了如何使用run-task
。
- AWS CLI
-
在默认集群上运行任务
以下
run-task
示例在默认集群上运行任务并使用客户端令牌。aws ecs run-task \ --cluster
default
\ --task-definitionsleep360:1
\ --client-token550e8400-e29b-41d4-a716-446655440000
输出:
{ "tasks": [ { "attachments": [], "attributes": [ { "name": "ecs.cpu-architecture", "value": "x86_64" } ], "availabilityZone": "us-east-1b", "capacityProviderName": "example-capacity-provider", "clusterArn": "arn:aws:ecs:us-east-1:123456789012:cluster/default", "containerInstanceArn": "arn:aws:ecs:us-east-1:123456789012:container-instance/default/bc4d2ec611d04bb7bb97e83ceEXAMPLE", "containers": [ { "containerArn": "arn:aws:ecs:us-east-1:123456789012:container/default/d6f51cc5bbc94a47969c92035e9f66f8/75853d2d-711e-458a-8362-0f0aEXAMPLE", "taskArn": "arn:aws:ecs:us-east-1:123456789012:task/default/d6f51cc5bbc94a47969c9203EXAMPLE", "name": "sleep", "image": "busybox", "lastStatus": "PENDING", "networkInterfaces": [], "cpu": "10", "memory": "10" } ], "cpu": "10", "createdAt": "2023-11-21T16:59:34.403000-05:00", "desiredStatus": "RUNNING", "enableExecuteCommand": false, "group": "family:sleep360", "lastStatus": "PENDING", "launchType": "EC2", "memory": "10", "overrides": { "containerOverrides": [ { "name": "sleep" } ], "inferenceAcceleratorOverrides": [] }, "tags": [], "taskArn": "arn:aws:ecs:us-east-1:123456789012:task/default/d6f51cc5bbc94a47969c9203EXAMPLE", "taskDefinitionArn": "arn:aws:ecs:us-east-1:123456789012:task-definition/sleep360:1", "version": 1 } ], "failures": [] }
有关更多信息,请参阅 Amazon ECS 开发者指南中的运行任务。
-
有关API详细信息,请参阅 “RunTask AWS CLI
命令参考”。
-
以下代码示例显示了如何使用start-task
。
- AWS CLI
-
开始一项新任务
以下内容使用默认集群中指定容器实例上
sleep360
任务定义的最新版本start-task
启动任务。aws ecs start-task \ --task-definition
sleep360
\ --container-instances765936fadbdd46b5991a4bd70c2a43d4
输出:
{ "tasks": [ { "taskArn": "arn:aws:ecs:us-west-2:130757420319:task/default/666fdccc2e2d4b6894dd422f4eeee8f8", "clusterArn": "arn:aws:ecs:us-west-2:130757420319:cluster/default", "taskDefinitionArn": "arn:aws:ecs:us-west-2:130757420319:task-definition/sleep360:3", "containerInstanceArn": "arn:aws:ecs:us-west-2:130757420319:container-instance/default/765936fadbdd46b5991a4bd70c2a43d4", "overrides": { "containerOverrides": [ { "name": "sleep" } ] }, "lastStatus": "PENDING", "desiredStatus": "RUNNING", "cpu": "128", "memory": "128", "containers": [ { "containerArn": "arn:aws:ecs:us-west-2:130757420319:container/75f11ed4-8a3d-4f26-a33b-ad1db9e02d41", "taskArn": "arn:aws:ecs:us-west-2:130757420319:task/default/666fdccc2e2d4b6894dd422f4eeee8f8", "name": "sleep", "lastStatus": "PENDING", "networkInterfaces": [], "cpu": "10", "memory": "10" } ], "version": 1, "createdAt": 1563421494.186, "group": "family:sleep360", "launchType": "EC2", "attachments": [], "tags": [] } ], "failures": [] }
-
有关API详细信息,请参阅 “StartTask AWS CLI
命令参考”。
-
以下代码示例显示了如何使用stop-task
。
- AWS CLI
-
停止任务
以下操作
stop-task
会阻止指定任务在默认集群中运行。aws ecs stop-task \ --task
666fdccc2e2d4b6894dd422f4eeee8f8
输出:
{ "task": { "taskArn": "arn:aws:ecs:us-west-2:130757420319:task/default/666fdccc2e2d4b6894dd422f4eeee8f8", "clusterArn": "arn:aws:ecs:us-west-2:130757420319:cluster/default", "taskDefinitionArn": "arn:aws:ecs:us-west-2:130757420319:task-definition/sleep360:3", "containerInstanceArn": "arn:aws:ecs:us-west-2:130757420319:container-instance/default/765936fadbdd46b5991a4bd70c2a43d4", "overrides": { "containerOverrides": [] }, "lastStatus": "STOPPED", "desiredStatus": "STOPPED", "cpu": "128", "memory": "128", "containers": [], "version": 2, "stoppedReason": "Taskfailedtostart", "stopCode": "TaskFailedToStart", "connectivity": "CONNECTED", "connectivityAt": 1563421494.186, "pullStartedAt": 1563421494.252, "pullStoppedAt": 1563421496.252, "executionStoppedAt": 1563421497, "createdAt": 1563421494.186, "stoppingAt": 1563421497.252, "stoppedAt": 1563421497.252, "group": "family:sleep360", "launchType": "EC2", "attachments": [], "tags": [] } }
-
有关API详细信息,请参阅 “StopTask AWS CLI
命令参考”。
-
以下代码示例显示了如何使用tag-resource
。
- AWS CLI
-
为资源添加标签
以下
tag-resource
示例向指定资源添加单个标签。aws ecs tag-resource \ --resource-arn
arn:aws:ecs:us-west-2:123456789012:cluster/MyCluster
--tagskey=key1,value=value1
此命令不生成任何输出。
向资源添加多个标签
以下
tag-resource
示例向指定资源添加多个标签。aws ecs tag-resource \ --resource-arn
arn:aws:ecs:us-west-2:123456789012:cluster/MyCluster
\ --tagskey=key1,value=value1
key=key2,value=value2
key=key3,value=value3
此命令不生成任何输出。
-
有关API详细信息,请参阅 “TagResource AWS CLI
命令参考”。
-
以下代码示例显示了如何使用untag-resource
。
- AWS CLI
-
从资源中移除标签
以下
untag-resource
示例从指定资源中删除列出的标签。aws ecs untag-resource \ --resource-arn
arn:aws:ecs:us-west-2:123456789012:cluster/MyCluster
\ --tag-keyskey1,key2
此命令不生成任何输出。
-
有关API详细信息,请参阅 “UntagResource AWS CLI
命令参考”。
-
以下代码示例显示了如何使用update-cluster-settings
。
- AWS CLI
-
修改集群的设置
以下
update-cluster-settings
示例为集default
群启用 CloudWatch 容器见解。aws ecs update-cluster-settings \ --cluster
default
\ --settingsname=containerInsights,value=enabled
输出:
{ "cluster": { "clusterArn": "arn:aws:ecs:us-west-2:123456789012:cluster/MyCluster", "clusterName": "default", "status": "ACTIVE", "registeredContainerInstancesCount": 0, "runningTasksCount": 0, "pendingTasksCount": 0, "activeServicesCount": 0, "statistics": [], "tags": [], "settings": [ { "name": "containerInsights", "value": "enabled" } ] } }
有关更多信息,请参阅《Amazon ECS 开发者指南》中的修改账户设置。
-
有关API详细信息,请参阅 “UpdateClusterSettings AWS CLI
命令参考”。
-
以下代码示例显示了如何使用update-container-agent
。
- AWS CLI
-
更新 Amazon 容器实例上的ECS容器代理
以下
update-container-agent
示例更新默认集群中指定容器实例上的容器代理。aws ecs update-container-agent --cluster
default
--container-instancea1b2c3d4-5678-90ab-cdef-11111EXAMPLE
输出:
{ "containerInstance": { "status": "ACTIVE", ... "agentUpdateStatus": "PENDING", "versionInfo": { "agentVersion": "1.0.0", "agentHash": "4023248", "dockerVersion": "DockerVersion: 1.5.0" } } }
有关更多信息,请参阅《亚马逊ECS开发者指南》中的更新亚马逊ECS容器代理。
-
有关API详细信息,请参阅 “UpdateContainerAgent AWS CLI
命令参考”。
-
以下代码示例显示了如何使用update-container-instances-state
。
- AWS CLI
-
更新容器实例的状态
以下内容
update-container-instances-state
更新了指定容器实例的状态,如果已注册到DRAINING
该容器实例,则会将其从集群中移除。aws ecs update-container-instances-state \ --container-instances
765936fadbdd46b5991a4bd70c2a43d4
\ --statusDRAINING
输出:
{ "containerInstances": [ { "containerInstanceArn": "arn:aws:ecs:us-west-2:130757420319:container-instance/default/765936fadbdd46b5991a4bd70c2a43d4", "ec2InstanceId": "i-013d87ffbb4d513bf", "version": 4390, "versionInfo": { "agentVersion": "1.29.0", "agentHash": "a190a73f", "dockerVersion": "DockerVersion:18.06.1-ce" }, "remainingResources": [ { "name": "CPU", "type": "INTEGER", "doubleValue": 0, "longValue": 0, "integerValue": 1536 }, { "name": "MEMORY", "type": "INTEGER", "doubleValue": 0, "longValue": 0, "integerValue": 2681 }, { "name": "PORTS", "type": "STRINGSET", "doubleValue": 0, "longValue": 0, "integerValue": 0, "stringSetValue": [ "22", "2376", "2375", "51678", "51679" ] }, { "name": "PORTS_UDP", "type": "STRINGSET", "doubleValue": 0, "longValue": 0, "integerValue": 0, "stringSetValue": [] } ], "registeredResources": [ { "name": "CPU", "type": "INTEGER", "doubleValue": 0, "longValue": 0, "integerValue": 2048 }, { "name": "MEMORY", "type": "INTEGER", "doubleValue": 0, "longValue": 0, "integerValue": 3705 }, { "name": "PORTS", "type": "STRINGSET", "doubleValue": 0, "longValue": 0, "integerValue": 0, "stringSetValue": [ "22", "2376", "2375", "51678", "51679" ] }, { "name": "PORTS_UDP", "type": "STRINGSET", "doubleValue": 0, "longValue": 0, "integerValue": 0, "stringSetValue": [] } ], "status": "DRAINING", "agentConnected": true, "runningTasksCount": 2, "pendingTasksCount": 0, "attributes": [ { "name": "ecs.capability.secrets.asm.environment-variables" }, { "name": "ecs.capability.branch-cni-plugin-version", "value": "e0703516-" }, { "name": "ecs.ami-id", "value": "ami-00e0090ac21971297" }, { "name": "ecs.capability.secrets.asm.bootstrap.log-driver" }, { "name": "com.amazonaws.ecs.capability.logging-driver.none" }, { "name": "ecs.capability.ecr-endpoint" }, { "name": "ecs.capability.docker-plugin.local" }, { "name": "ecs.capability.task-cpu-mem-limit" }, { "name": "ecs.capability.secrets.ssm.bootstrap.log-driver" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.30" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.31" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.32" }, { "name": "ecs.availability-zone", "value": "us-west-2c" }, { "name": "ecs.capability.aws-appmesh" }, { "name": "com.amazonaws.ecs.capability.logging-driver.awslogs" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.24" }, { "name": "ecs.capability.task-eni-trunking" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.25" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.26" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.27" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.28" }, { "name": "com.amazonaws.ecs.capability.privileged-container" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.29" }, { "name": "ecs.cpu-architecture", "value": "x86_64" }, { "name": "com.amazonaws.ecs.capability.ecr-auth" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.20" }, { "name": "ecs.os-type", "value": "linux" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.21" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.22" }, { "name": "ecs.capability.task-eia" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.23" }, { "name": "ecs.capability.private-registry-authentication.secretsmanager" }, { "name": "com.amazonaws.ecs.capability.logging-driver.syslog" }, { "name": "com.amazonaws.ecs.capability.logging-driver.json-file" }, { "name": "ecs.capability.execution-role-awslogs" }, { "name": "ecs.vpc-id", "value": "vpc-1234" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.17" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.18" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.19" }, { "name": "ecs.capability.task-eni" }, { "name": "ecs.capability.execution-role-ecr-pull" }, { "name": "ecs.capability.container-health-check" }, { "name": "ecs.subnet-id", "value": "subnet-1234" }, { "name": "ecs.instance-type", "value": "c5.large" }, { "name": "com.amazonaws.ecs.capability.task-iam-role-network-host" }, { "name": "ecs.capability.container-ordering" }, { "name": "ecs.capability.cni-plugin-version", "value": "91ccefc8-2019.06.0" }, { "name": "ecs.capability.pid-ipc-namespace-sharing" }, { "name": "ecs.capability.secrets.ssm.environment-variables" }, { "name": "com.amazonaws.ecs.capability.task-iam-role" } ], "registeredAt": 1560788724.507, "attachments": [], "tags": [] } ], "failures": [] }
-
有关API详细信息,请参阅 “UpdateContainerInstancesState AWS CLI
命令参考”。
-
以下代码示例显示了如何使用update-service-primary-task-set
。
- AWS CLI
-
更新服务的主任务集
以下
update-service-primary-task-set
示例更新了指定服务的主任务集。aws ecs update-service-primary-task-set \ --cluster
MyCluster
\ --serviceMyService
\ --primary-task-setarn:aws:ecs:us-west-2:123456789012:task-set/MyCluster/MyService/ecs-svc/1234567890123456789
输出:
{ "taskSet": { "id": "ecs-svc/1234567890123456789", "taskSetArn": "arn:aws:ecs:us-west-2:123456789012:task-set/MyCluster/MyService/ecs-svc/1234567890123456789", "status": "PRIMARY", "taskDefinition": "arn:aws:ecs:us-west-2:123456789012:task-definition/sample-fargate:2", "computedDesiredCount": 1, "pendingCount": 0, "runningCount": 0, "createdAt": 1557128360.711, "updatedAt": 1557129412.653, "launchType": "EC2", "networkConfiguration": { "awsvpcConfiguration": { "subnets": [ "subnet-12344321" ], "securityGroups": [ "sg-12344312" ], "assignPublicIp": "DISABLED" } }, "loadBalancers": [], "serviceRegistries": [], "scale": { "value": 50.0, "unit": "PERCENT" }, "stabilityStatus": "STABILIZING", "stabilityStatusAt": 1557129279.914 } }
-
有关API详细信息,请参阅 “UpdateServicePrimaryTaskSet AWS CLI
命令参考”。
-
以下代码示例显示了如何使用update-service
。
- AWS CLI
-
示例 1:更改服务中使用的任务定义
以下
update-service
示例将my-http-service
服务更新为使用amazon-ecs-sample
任务定义。aws ecs update-service --service
my-http-service
--task-definitionamazon-ecs-sample
示例 2:更改服务中的任务数
以下
update-service
示例将服务my-http-service
所需的任务计数更新为 3。aws ecs update-service --service
my-http-service
--desired-count3
有关更多信息,请参阅《Amazon ECS 开发者指南》中的更新服务。
-
有关API详细信息,请参阅 “UpdateService AWS CLI
命令参考”。
-
以下代码示例显示了如何使用update-task-set
。
- AWS CLI
-
更新任务集
以下
update-task-set
示例更新任务集以调整比例。aws ecs update-task-set \ --cluster
MyCluster
\ --serviceMyService
\ --task-setarn:aws:ecs:us-west-2:123456789012:task-set/MyCluster/MyService/ecs-svc/1234567890123456789
\ --scalevalue=50,unit=PERCENT
输出:
{ "taskSet": { "id": "ecs-svc/1234567890123456789", "taskSetArn": "arn:aws:ecs:us-west-2:123456789012:task-set/MyCluster/MyService/ecs-svc/1234567890123456789", "status": "ACTIVE", "taskDefinition": "arn:aws:ecs:us-west-2:123456789012:task-definition/sample-fargate:2", "computedDesiredCount": 0, "pendingCount": 0, "runningCount": 0, "createdAt": 1557128360.711, "updatedAt": 1557129279.914, "launchType": "EC2", "networkConfiguration": { "awsvpcConfiguration": { "subnets": [ "subnet-12344321" ], "securityGroups": [ "sg-12344321" ], "assignPublicIp": "DISABLED" } }, "loadBalancers": [], "serviceRegistries": [], "scale": { "value": 50.0, "unit": "PERCENT" }, "stabilityStatus": "STABILIZING", "stabilityStatusAt": 1557129279.914 } }
-
有关API详细信息,请参阅 “UpdateTaskSet AWS CLI
命令参考”。
-