Deploying ECS services

With your ECS cluster, ECS task definition, and various supporting resources in place, you can now define an ECS service that will deploy your container application as defined in the ECS task definition to your ECS cluster. 

The following example demonstrates adding an ECS service resource to your CloudFormation template, which has a resource type of AWS::ECS::Service:

...
...
Resources:
ApplicationService:
Type: AWS::ECS::Service
DependsOn:
- ApplicationAutoscaling
- ApplicationLogGroup
- ApplicationLoadBalancerHttpListener
Properties:
TaskDefinition: !Ref ApplicationTaskDefinition
Cluster: !Ref ApplicationCluster
DesiredCount: !Ref ApplicationDesiredCount
LoadBalancers:
- ContainerName: todobackend
ContainerPort: 8000
TargetGroupArn: !Ref ApplicationServiceTargetGroup
Role: !Sub arn:aws:iam::${AWS::AccountId}:role/aws-service-role/ecs.amazonaws.com/AWSServiceRoleForECS
      DeploymentConfiguration:
MaximumPercent: 200
MinimumHealthyPercent: 100

ApplicationTaskDefinition:
Type: AWS::ECS::TaskDefinition
...
...
Creating an ECS service

One interesting aspect of the configuration in the preceding example is the DependsOn parameter, which defines other resources in the stack that must be created or updated before the ECS service resource can be created or updated. Although CloudFormation automatically creates dependencies when a resource directly references another resource, a resource may have dependencies on other resources that don't have a direct relationship to that resource. The ECS service resource is a good example of this—the service can't operate without a functional ECS cluster and associated ECS container instances (this is represented by the ApplicationAutoscaling resource) and can't write logs without the ApplicationLogGroup resource. A more subtle dependency is the ApplicationLoadBalancerHttpListener resource, which must be functional before the target group associated with the ECS service will register targets.

The various properties that are configured for the ECS service are described here: