Use UpdateService with an AWS SDK or CLI - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Use UpdateService with an AWS SDK or CLI

The following code examples show how to use UpdateService.

CLI
AWS CLI

Example 1: To change the task definition used in a service

The following update-service example updates the my-http-service service to use the amazon-ecs-sample task definition.

aws ecs update-service --service my-http-service --task-definition amazon-ecs-sample

Example 2: To change the number of tasks in a service

The following update-service example updates the desired task count of the service my-http-service to 3.

aws ecs update-service --service my-http-service --desired-count 3

For more information, see Updating a Service in the Amazon ECS Developer Guide.

  • For API details, see UpdateService in AWS CLI Command Reference.

Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ecs.EcsClient; import software.amazon.awssdk.services.ecs.model.EcsException; import software.amazon.awssdk.services.ecs.model.UpdateServiceRequest; /** * 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 UpdateService { public static void main(String[] args) { final String usage = """ Usage: <clusterName> <serviceArn>\s Where: clusterName - The cluster name. serviceArn - The service ARN value. """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String clusterName = args[0]; String serviceArn = args[1]; Region region = Region.US_EAST_1; EcsClient ecsClient = EcsClient.builder() .region(region) .build(); updateSpecificService(ecsClient, clusterName, serviceArn); ecsClient.close(); } public static void updateSpecificService(EcsClient ecsClient, String clusterName, String serviceArn) { try { UpdateServiceRequest serviceRequest = UpdateServiceRequest.builder() .cluster(clusterName) .service(serviceArn) .desiredCount(0) .build(); ecsClient.updateService(serviceRequest); System.out.println("The service was modified"); } catch (EcsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • For API details, see UpdateService in AWS SDK for Java 2.x API Reference.

PowerShell
Tools for PowerShell

Example 1: This example command updates the `my-http-service` service to use the `amazon-ecs-sample` task definition.

Update-ECSService -Service my-http-service -TaskDefinition amazon-ecs-sample

Example 2: This example command updates the desired count of the `my-http-service` service to 10.

Update-ECSService -Service my-http-service -DesiredCount 10
  • For API details, see UpdateService in AWS Tools for PowerShell Cmdlet Reference.