Skip to content

Commit

Permalink
moved task definition into image
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwittig committed Feb 28, 2017
1 parent c678ad2 commit 5256f01
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 168 deletions.
29 changes: 4 additions & 25 deletions ecs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,11 @@ This template describes a fault tolerant and scalable ECS cluster on AWS. The cl
## ECS service
This template describes a fault tolerant and scalable ECS service on AWS. The service scales based on CPU utilization.

### Creating an ECS task definition
Before you can start with the ECS service, you need to create a task definition. The task definition references your Docker image from Docker Hub or ECR.
> The image needs to expose port 80 or the `AWS::ECS::TaskDefinition` needs to be adjusted!
In the [container-definitions.json](./container-definitions.json) file, replace:
* `$Image` with your published Docker image (e.g. `nginx:1.11.5` or `123456789012.dkr.ecr.us-east-1.amazonaws.com/demo:1.0.0`)
* `$AWSRegion` with the region your ECS cluster runs in (e.g. `eu-west-1`)
* `$ClusterLogGroup` with the `LogGroup` output from the `ecs-cluster` stack (e.g. via the CLI `aws cloudformation describe-stacks --stack-name $ClusterName --query "Stacks[0].Outputs[?OutputKey=='LogGroup'].OutputValue" --output text`)
* `$ServiceName` with the name of the service (e.g. `demo`)

Other options can be found in the AWS docs: http://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html

The following CLI command creates a task definition and outputs the unique ARN that you will need later when you create the service:

```
aws ecs register-task-definition --family $ServiceName --network-mode bridge --container-definitions file://container-definitions.json --query "taskDefinition.taskDefinitionArn" --output text
```

#### Updating an ECS task definition

If you want to update your task definition because you want to deploy a new version of your image, just re run the `aws ecs register-task-definition` command from above. This will create a new task definition because you can not change them. Take a note of the new ARN that the command returns.


### Choosing a service template flavour
We provide two service templates.
The first one (`service-cluster-alb.yaml`) uses the cluster's load balancer and path based routing. If you want to run multiple services on the same cluster they all will use the same domain name but start with different paths (e.g. `https://yourdomain.com/service1/` and `https://yourdomain.com/service2/`).
The second one (`service-dedicated-alb.yaml`) includes a dedicated load balancer (ALB). You can then use a separate domain name for each service.
We provide two service templates:
* `service-cluster-alb.yaml` uses the cluster's load balancer and path based routing. If you want to run multiple services on the same cluster they all will use the same domain name but start with different paths (e.g. `https://yourdomain.com/service1/` and `https://yourdomain.com/service2/`).
* `service-dedicated-alb.yaml` includes a dedicated load balancer (ALB). You can then use a separate domain name for each service.

### Using the cluster's load balancer and path based routing
This template describes a fault tolerant and scalable ECS service that uses the cluster's load balancer and path based routing.
Expand Down
22 changes: 0 additions & 22 deletions ecs/container-definitions.json

This file was deleted.

45 changes: 26 additions & 19 deletions ecs/service-cluster-alb.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ Metadata:
- Label:
default: 'Task Parameters'
Parameters:
- TaskDefinitionArn
- Image
- DesiredCount
- MaxCapacity
- MinCapacity
- ContainerPort
- ContainerName
Parameters:
ParentClusterStack:
Description: 'Stack name of parent Cluster stack based on ecs/cluster.yaml template.'
Expand All @@ -47,8 +45,8 @@ Parameters:
AllowedValues:
- true
- false
TaskDefinitionArn:
Description: 'The ARN of the task definition (including the revision number) that you want to run on the cluster, such as arn:aws:ecs:us-east-1:123456789012:task-definition/mytask:3.'
Image:
Description: 'The image to use for a container, which is passed directly to the Docker daemon. You can use images in the Docker Hub registry or specify other repositories (repository-url/image:tag).'
Type: String
DesiredCount:
Description: 'The number of simultaneous tasks, that you want to run on the cluster.'
Expand All @@ -68,21 +66,30 @@ Parameters:
Default: 2
ConstraintDescription: 'Must be >= 1'
MinValue: 1
ContainerPort:
Description: 'The port number on the container to direct load balancer traffic to. Your container instances must allow ingress traffic on this port. The container definition must match with this value.'
Type: Number
Default: 80
ConstraintDescription: 'Must be in the range [0-65535]'
MinValue: 0
MaxValue: 65535
ContainerName:
Description: 'The name of a container to use with the load balancer. The container definition must match with this value.'
Type: String
Default: main
Mappings: {}
Conditions:
HasLoadBalancerHttps: !Equals [!Ref LoadBalancerHttps, 'true']
Resources:
TaskDefinition:
Type: 'AWS::ECS::TaskDefinition'
Properties:
Family: !Ref 'AWS::StackName'
NetworkMode: bridge
ContainerDefinitions:
- Name: main # if you change this, you also must change the AWS::ECS::Service
Image: !Ref Image
Memory: 128
PortMappings:
- ContainerPort: 80 # if you change this, you also must change the AWS::ECS::Service
Protocol: tcp
Essential: true
LogConfiguration:
LogDriver: awslogs
Options:
'awslogs-region': !Ref 'AWS::Region'
'awslogs-group':
'Fn::ImportValue': !Sub '${ParentClusterStack}-LogGroup'
'awslogs-stream-prefix': !Ref 'AWS::StackName'
LoadBalancerTargetGroup:
Type: 'AWS::ElasticLoadBalancingV2::TargetGroup'
Properties:
Expand Down Expand Up @@ -147,11 +154,11 @@ Resources:
MinimumHealthyPercent: 50
DesiredCount: !Ref DesiredCount
LoadBalancers:
- ContainerName: !Ref ContainerName
ContainerPort: !Ref ContainerPort
- ContainerName: main
ContainerPort: 80
TargetGroupArn: !Ref LoadBalancerTargetGroup
Role: !GetAtt 'ServiceRole.Arn'
TaskDefinition: !Ref TaskDefinitionArn
TaskDefinition: !Ref TaskDefinition
ScalableTargetRole: # based on http://docs.aws.amazon.com/AmazonECS/latest/developerguide/autoscale_IAM_role.html
Type: 'AWS::IAM::Role'
Properties:
Expand Down
45 changes: 26 additions & 19 deletions ecs/service-dedicated-alb.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ Metadata:
- Label:
default: 'Task Parameters'
Parameters:
- TaskDefinitionArn
- Image
- DesiredCount
- MaxCapacity
- MinCapacity
- ContainerPort
- ContainerName
Parameters:
ParentVPCStack:
Description: 'Stack name of parent VPC stack based on vpc/vpc-*azs.yaml template.'
Expand All @@ -46,8 +44,8 @@ Parameters:
Description: 'Optional Amazon Resource Name (ARN) of the certificate to associate with the load balancer.'
Type: String
Default: ''
TaskDefinitionArn:
Description: 'The ARN of the task definition (including the revision number) that you want to run on the cluster, such as arn:aws:ecs:us-east-1:123456789012:task-definition/mytask:3.'
Image:
Description: 'The image to use for a container, which is passed directly to the Docker daemon. You can use images in the Docker Hub registry or specify other repositories (repository-url/image:tag).'
Type: String
DesiredCount:
Description: 'The number of simultaneous tasks, which you specify by using the TaskDefinition property, that you want to run on the cluster.'
Expand All @@ -67,17 +65,6 @@ Parameters:
Default: 2
ConstraintDescription: 'Must be >= 1'
MinValue: 1
ContainerPort:
Description: 'The port number on the container to direct load balancer traffic to. Your container instances must allow ingress traffic on this port. The container definition must match with this value.'
Type: Number
Default: 80
ConstraintDescription: 'Must be in the range [0-65535]'
MinValue: 0
MaxValue: 65535
ContainerName:
Description: 'The name of a container to use with the load balancer. The container definition must match with this value.'
Type: String
Default: main
Mappings: {}
Conditions:
HasAuthProxySecurityGroup: !Not [!Equals [!Ref ParentAuthProxyStack, '']]
Expand All @@ -87,6 +74,26 @@ Conditions:
HasAuthProxySecurityGroupAndLoadBalancerCertificateArn: !And [!Condition HasAuthProxySecurityGroup, !Condition HasLoadBalancerCertificateArn]
HasNotAuthProxySecurityGroupAndLoadBalancerCertificateArn: !And [!Condition HasNotAuthProxySecurityGroup, !Condition HasLoadBalancerCertificateArn]
Resources:
TaskDefinition:
Type: 'AWS::ECS::TaskDefinition'
Properties:
Family: !Ref 'AWS::StackName'
NetworkMode: bridge
ContainerDefinitions:
- Name: main # if you change this, you also must change the AWS::ECS::Service
Image: !Ref Image
Memory: 128
PortMappings:
- ContainerPort: 80 # if you change this, you also must change the AWS::ECS::Service
Protocol: tcp
Essential: true
LogConfiguration:
LogDriver: awslogs
Options:
'awslogs-region': !Ref 'AWS::Region'
'awslogs-group':
'Fn::ImportValue': !Sub '${ParentClusterStack}-LogGroup'
'awslogs-stream-prefix': !Ref 'AWS::StackName'
ALBSecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
Expand Down Expand Up @@ -211,11 +218,11 @@ Resources:
MinimumHealthyPercent: 50
DesiredCount: !Ref DesiredCount
LoadBalancers:
- ContainerName: !Ref ContainerName
ContainerPort: !Ref ContainerPort
- ContainerName: main
ContainerPort: 80
TargetGroupArn: !Ref DefaultTargetGroup
Role: !GetAtt 'ServiceRole.Arn'
TaskDefinition: !Ref TaskDefinitionArn
TaskDefinition: !Ref TaskDefinition
ScalableTargetRole: # based on http://docs.aws.amazon.com/AmazonECS/latest/developerguide/autoscale_IAM_role.html
Type: 'AWS::IAM::Role'
Properties:
Expand Down
Loading

0 comments on commit 5256f01

Please sign in to comment.