Creating Amazon ECS resources with Amazon CloudFormation - Amazon Elastic Container Service
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China.

Creating Amazon ECS resources with Amazon CloudFormation

Amazon ECS is integrated with Amazon CloudFormation, a service that you can use to model and set up Amazon resources with templates that you define. This way, you can spend less time creating and managing your resources and infrastructure. Using Amazon CloudFormation, you can create a template that describes all the Amazon resources that you want, such as specific Amazon ECS clusters. Then, Amazon CloudFormation takes care of provisioning and configuring those resources for you.

When you use Amazon CloudFormation, you can reuse your template to set up your Amazon ECS resources in a consistent and repeatable manner. You describe your resources one time, and then provision the same resources again across multiple Amazon Web Services accounts and Amazon Web Services Regions.

Amazon ECS and Amazon CloudFormation templates

To provision and configure resources for Amazon ECS and related services, make sure that you're familiar with Amazon CloudFormation templates. Amazon CloudFormation templates are text files in the JSON or YAML format that describe the resources that you want to provision in your Amazon CloudFormation stacks. If you're unfamiliar with either the JSON or YAML format, or both, you can use Amazon CloudFormation Designer to get started using Amazon CloudFormation templates. For more information, see What is Amazon CloudFormation Designer? in the Amazon CloudFormation User Guide.

Amazon ECS supports creating clusters, task definitions, services, and task sets in Amazon CloudFormation. The following examples demonstrate how to create resources with these templates using the Amazon CLI. You can also create these resources using the Amazon CloudFormation console. For more information about how to create resources using the Amazon CloudFormation console, see the Amazon CloudFormation User Guide.

Example templates

Creating Amazon ECS resources using separate stacks

The following examples show how to create Amazon ECS resources by using separate stacks for each resource.

Amazon ECS task definitions

You can use the following template to create a Fargate Linux task.

JSON

{ "AWSTemplateFormatVersion": "2010-09-09", "Resources": { "taskdefinition": { "Type": "AWS::ECS::TaskDefinition", "Properties": { "containerDefinitions": [ { "command": [ "/bin/sh -c \"echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' > /usr/local/apache2/htdocs/index.html && httpd-foreground\"" ], "entryPoint": [ "sh", "-c" ], "essential": true, "image": "httpd:2.4", "logConfiguration": { "logDriver": "awslogs", "options": { "awslogs-group" : "/ecs/fargate-task-definition", "awslogs-region": "us-east-1", "awslogs-stream-prefix": "ecs" } }, "name": "sample-fargate-app", "portMappings": [ { "containerPort": 80, "hostPort": 80, "protocol": "tcp" } ] } ], "cpu":"256", "executionRoleArn": "arn:aws:iam::aws_account_id:role/ecsTaskExecutionRole", "family": "fargate-task-definition-cfn", "memory":"512", "networkMode": "awsvpc", "requiresCompatibilities": [ "FARGATE" ], "runtimePlatform": { "OperatingSystemFamily": "LINUX" } } } } }

YAML

AWSTemplateFormatVersion: 2010-09-09 Resources: taskdefinition: Type: 'AWS::ECS::TaskDefinition' Properties: containerDefinitions: - command: - >- /bin/sh -c "echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' > /usr/local/apache2/htdocs/index.html && httpd-foreground" entryPoint: - sh - '-c' essential: true image: 'httpd:2.4' logConfiguration: logDriver: awslogs options: awslogs-group: /ecs/fargate-task-definition awslogs-region: us-east-1 awslogs-stream-prefix: ecs name: sample-fargate-app portMappings: - containerPort: 80 hostPort: 80 protocol: tcp cpu: '256' executionRoleArn: 'arn:aws:iam::aws_account_id:role/ecsTaskExecutionRole' family: fargate-task-definition-cfn memory: '512' networkMode: awsvpc requiresCompatibilities: - FARGATE runtimePlatform: OperatingSystemFamily: LINUX

Amazon ECS clusters

You can use the following template to create an empty cluster.

JSON

{ "AWSTemplateFormatVersion": "2010-09-09", "Resources": { "ECSCluster": { "Type": "AWS::ECS::Cluster", "Properties": { "ClusterName": "MyEmptyCluster" } } } }

YAML

AWSTemplateFormatVersion: 2010-09-09 Resources: ECSCluster: Type: 'AWS::ECS::Cluster' Properties: ClusterName: MyEmptyCluster

Creating multiple Amazon ECS resources in one stack

You can use the following example template to create multiple Amazon ECS resources in one stack. The template creates an Amazon ECS cluster that's named CFNCluster. The cluster contains a Linux Fargate task definition. The template also creates a service that's named cfn-service that launches and maintains the Fargate task definition. Before you use this command, make sure that the subnet and security group IDs in the service's NetworkConfiguration all belong to the same VPC.

JSON

{ "AWSTemplateFormatVersion": "2010-09-09", "Resources": { "ECSCluster": { "Type": "AWS::ECS::Cluster", "Properties": { "ClusterName": "CFNCluster" } }, "taskdefinition": { "Type": "AWS::ECS::TaskDefinition", "Properties": { "containerDefinitions": [ { "command": [ "/bin/sh -c \"echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' > /usr/local/apache2/htdocs/index.html && httpd-foreground\"" ], "entryPoint": [ "sh", "-c" ], "essential": true, "image": "httpd:2.4", "logConfiguration": { "logDriver": "awslogs", "options": { "awslogs-group": "/ecs/fargate-task-definition", "awslogs-region": "us-east-1", "awslogs-stream-prefix": "ecs" } }, "name": "sample-fargate-app", "portMappings": [ { "containerPort": 80, "hostPort": 80, "protocol": "tcp" } ] } ], "cpu": "256", "executionRoleArn": "arn:aws:iam::aws_account_id:role/ecsTaskExecutionRole", "family": "task-definition-cfn", "memory": "512", "networkMode": "awsvpc", "requiresCompatibilities": [ "FARGATE" ], "runtimePlatform": { "OperatingSystemFamily": "LINUX" } } }, "ECSService": { "Type": "AWS::ECS::Service", "Properties": { "ServiceName": "cfn-service", "Cluster": { "Ref": "ECSCluster" }, "DesiredCount": 1, "LaunchType": "FARGATE", "NetworkConfiguration": { "AwsvpcConfiguration": { "AssignPublicIp": "ENABLED", "SecurityGroups": [ "sg-abcdef01234567890" ], "Subnets": [ "subnet-abcdef01234567890" ] } }, "TaskDefinition": { "Ref": "taskdefinition" } } } } }

YAML

AWSTemplateFormatVersion: 2010-09-09 Resources: ECSCluster: Type: 'AWS::ECS::Cluster' Properties: ClusterName: CFNCluster taskdefinition: Type: 'AWS::ECS::TaskDefinition' Properties: containerDefinitions: - command: - >- /bin/sh -c "echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' > /usr/local/apache2/htdocs/index.html && httpd-foreground" entryPoint: - sh - '-c' essential: true image: 'httpd:2.4' logConfiguration: logDriver: awslogs options: awslogs-group: /ecs/fargate-task-definition awslogs-region: us-east-1 awslogs-stream-prefix: ecs name: sample-fargate-app portMappings: - containerPort: 80 hostPort: 80 protocol: tcp cpu: '256' executionRoleArn: 'arn:aws:iam::aws_account_id:role/ecsTaskExecutionRole' family: task-definition-cfn memory: '512' networkMode: awsvpc requiresCompatibilities: - FARGATE runtimePlatform: OperatingSystemFamily: LINUX ECSService: Type: 'AWS::ECS::Service' Properties: ServiceName: cfn-service Cluster: !Ref ECSCluster DesiredCount: 1 LaunchType: FARGATE NetworkConfiguration: AwsvpcConfiguration: AssignPublicIp: ENABLED SecurityGroups: - sg-abcdef01234567890 Subnets: - subnet-abcdef01234567890 TaskDefinition: !Ref taskdefinition

Using the Amazon CLI to create resources from templates

The following command creates a stack that's named ecs-stack using a template body file that's named ecs-template-body.json. Ensure that the template body file is in the JSON or YAML format. The location of the file is specified in the --template-body parameter. In this case, the template body file is located in the current directory.

aws cloudformation create-stack \ --stack-name ecs-stack \ --template-body file://ecs-template-body.json

To ensure that resources are created correctly, check the Amazon ECS console or alternatively use the following commands:

  • The following command lists all task definitions.

    aws ecs list-task-definitions
  • The following command lists all clusters.

    aws ecs list-clusters
  • The following command lists all services defined in the cluster CFNCluster. Replace CFNCluster with the name of the cluster that you wanted to create the service in.

    aws ecs list-services \ --cluster CFNCluster

Learn more about Amazon CloudFormation

To learn more about Amazon CloudFormation, see the following resources: