文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
CreateService
搭配 a AWS SDK 或 CLI 使用
下列程式碼範例示範如何使用 CreateService
。
- CLI
-
- 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
範例示範如何建立ecs-simple-service
使用 EC2 啟動類型的任務呼叫的服務。服務使用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 詳細資訊,請參閱 AWS CLI 命令參考中的 CreateService
。
-
- Java
-
- Java 2.x 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ecs.EcsClient; import software.amazon.awssdk.services.ecs.model.AwsVpcConfiguration; import software.amazon.awssdk.services.ecs.model.NetworkConfiguration; import software.amazon.awssdk.services.ecs.model.CreateServiceRequest; import software.amazon.awssdk.services.ecs.model.LaunchType; import software.amazon.awssdk.services.ecs.model.CreateServiceResponse; import software.amazon.awssdk.services.ecs.model.EcsException; /** * 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 CreateService { public static void main(String[] args) { final String usage = """ Usage: <clusterName> <serviceName> <securityGroups> <subnets> <taskDefinition> Where: clusterName - The name of the ECS cluster. serviceName - The name of the ECS service to create. securityGroups - The name of the security group. subnets - The name of the subnet. taskDefinition - The name of the task definition. """; if (args.length != 5) { System.out.println(usage); System.exit(1); } String clusterName = args[0]; String serviceName = args[1]; String securityGroups = args[2]; String subnets = args[3]; String taskDefinition = args[4]; Region region = Region.US_EAST_1; EcsClient ecsClient = EcsClient.builder() .region(region) .build(); String serviceArn = createNewService(ecsClient, clusterName, serviceName, securityGroups, subnets, taskDefinition); System.out.println("The ARN of the service is " + serviceArn); ecsClient.close(); } public static String createNewService(EcsClient ecsClient, String clusterName, String serviceName, String securityGroups, String subnets, String taskDefinition) { try { AwsVpcConfiguration vpcConfiguration = AwsVpcConfiguration.builder() .securityGroups(securityGroups) .subnets(subnets) .build(); NetworkConfiguration configuration = NetworkConfiguration.builder() .awsvpcConfiguration(vpcConfiguration) .build(); CreateServiceRequest serviceRequest = CreateServiceRequest.builder() .cluster(clusterName) .networkConfiguration(configuration) .desiredCount(1) .launchType(LaunchType.FARGATE) .serviceName(serviceName) .taskDefinition(taskDefinition) .build(); CreateServiceResponse response = ecsClient.createService(serviceRequest); return response.service().serviceArn(); } catch (EcsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; } }
-
如需 API 詳細資訊,請參閱 CreateService AWS SDK for Java 2.x 參考中的 API。
-
- PowerShell
-
- for PowerShell 工具
-
範例 1:此範例命令會在名為 `ecs-simple-service` 的預設叢集中建立服務。服務使用 `ecs-demo` 任務定義,並維持該任務的 10 個實例。
New-ECSService -ServiceName ecs-simple-service -TaskDefinition ecs-demo -DesiredCount 10
範例 2:此範例命令會在您預設叢集中名為 `ecs-simple-service` 的負載平衡器後方建立服務。服務使用 `ecs-demo` 任務定義,並維持該任務的 10 個實例。
$lb = @{ LoadBalancerName = "EC2Contai-EcsElast-S06278JGSJCM" ContainerName = "simple-demo" ContainerPort = 80 } New-ECSService -ServiceName ecs-simple-service -TaskDefinition ecs-demo -DesiredCount 10 -LoadBalancer $lb
-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 CreateService。
-