AWS SDK Version 3 for .NET
API Reference

AWS services or capabilities described in AWS Documentation may vary by region/location. Click Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

Runs and maintains a desired number of tasks from a specified task definition. If the number of tasks running in a service drops below desiredCount, Amazon ECS spawns another copy of the task in the specified cluster. To update an existing service, see UpdateService.

In addition to maintaining the desired count of tasks in your service, you can optionally run your service behind a load balancer. The load balancer distributes traffic across the tasks that are associated with the service. For more information, see Service Load Balancing in the Amazon EC2 Container Service Developer Guide.

You can optionally specify a deployment configuration for your service. During a deployment (which is triggered by changing the task definition or the desired count of a service with an UpdateService operation), the service scheduler uses the minimumHealthyPercent and maximumPercent parameters to determine the deployment strategy.

The minimumHealthyPercent represents a lower limit on the number of your service's tasks that must remain in the RUNNING state during a deployment, as a percentage of the desiredCount (rounded up to the nearest integer). This parameter enables you to deploy without using additional cluster capacity. For example, if your service has a desiredCount of four tasks and a minimumHealthyPercent of 50%, the scheduler can stop two existing tasks to free up cluster capacity before starting two new tasks. Tasks for services that do not use a load balancer are considered healthy if they are in the RUNNING state. Tasks for services that do use a load balancer are considered healthy if they are in the RUNNING state and the container instance they are hosted on is reported as healthy by the load balancer. The default value for minimumHealthyPercent is 50% in the console and 100% for the AWS CLI, the AWS SDKs, and the APIs.

The maximumPercent parameter represents an upper limit on the number of your service's tasks that are allowed in the RUNNING or PENDING state during a deployment, as a percentage of the desiredCount (rounded down to the nearest integer). This parameter enables you to define the deployment batch size. For example, if your service has a desiredCount of four tasks and a maximumPercent value of 200%, the scheduler can start four new tasks before stopping the four older tasks (provided that the cluster resources required to do this are available). The default value for maximumPercent is 200%.

When the service scheduler launches new tasks, it determines task placement in your cluster using the following logic:

Note:

For PCL this operation is only available in asynchronous form. Please refer to CreateServiceAsync.

Namespace: Amazon.ECS
Assembly: AWSSDK.ECS.dll
Version: 3.x.y.z

Syntax

C#
public virtual CreateServiceResponse CreateService(
         CreateServiceRequest request
)
Parameters
request
Type: Amazon.ECS.Model.CreateServiceRequest

Container for the necessary parameters to execute the CreateService service method.

Return Value
The response from the CreateService service method, as returned by ECS.

Exceptions

ExceptionCondition
ClientException These errors are usually caused by a client action, such as using an action or resource on behalf of a user that doesn't have permission to use the action or resource, or specifying an identifier that is not valid.
ClusterNotFoundException The specified cluster could not be found. You can view your available clusters with ListClusters. Amazon ECS clusters are region-specific.
InvalidParameterException The specified parameter is invalid. Review the available parameters for the API request.
ServerException These errors are usually caused by a server issue.

Examples

This example creates a service in your default region called ``ecs-simple-service``. The service uses the ``hello_world`` task definition and it maintains 10 copies of that task.

To create a new service


var response = client.CreateService(new CreateServiceRequest 
{
    DesiredCount = 10,
    ServiceName = "ecs-simple-service",
    TaskDefinition = "hello_world"
});

Service service = response.Service;

            

This example creates a service in your default region called ``ecs-simple-service-elb``. The service uses the ``ecs-demo`` task definition and it maintains 10 copies of that task. You must reference an existing load balancer in the same region by its name.

To create a new service behind a load balancer


var response = client.CreateService(new CreateServiceRequest 
{
    DesiredCount = 10,
    LoadBalancers = new List {
        new LoadBalancer {
            ContainerName = "simple-app",
            ContainerPort = 80,
            LoadBalancerName = "EC2Contai-EcsElast-15DCDAURT3ZO2"
        }
    },
    Role = "ecsServiceRole",
    ServiceName = "ecs-simple-service-elb",
    TaskDefinition = "console-sample-app-static"
});

Service service = response.Service;

            

Version Information

.NET Framework:
Supported in: 4.5, 4.0, 3.5

Portable Class Library:
Supported in: Windows Store Apps
Supported in: Windows Phone 8.1
Supported in: Xamarin Android
Supported in: Xamarin iOS (Unified)
Supported in: Xamarin.Forms

See Also