Amazon ECS task definition across both EC2 and AWS Fargate
How to make a task definition that can deploy the same container either as a serverless application on AWS Fargate or hosted on EC2 instance capacity
Nathan Peck
Senior Developer Advocate at AWS
About
One of the convenient features of Amazon ECS is that it is agnostic when
it comes to capacity type. You can create an ECS task definition that deploys
to both AWS Fargate and Amazon EC2 instances at the same time.
This pattern shows the key parts of the task definition that make this possible.
CloudFormation template
The following template creates a task definition that is compatible across both ECS on EC2 and ECS on AWS Fargate. It then deploys the task definition on both capacity types.
Note the following settings, which enable cross compatability in the task definition:
RequiresCompatibilities - This is set to an array that has both EC2 and FARGATE
NetworkMode - In order to be compatible across both EC2 and Fargate the only valid value is awsvpc
Cpu and Memory - The requested task size much match a valid AWS Fargate task size
AWSTemplateFormatVersion:'2010-09-09'Description:An example task definition that can deployed onto bothAmazon EC2 and AWS FargateParameters:Cluster:Type:StringDescription:The name of the ECS cluster to deploy intoEc2CapacityProvider:Type:StringDescription:The name of an EC2 capacity provider in the cluster.ServiceName:Type:StringDefault:nginxDescription:Name of the serviceVpcId:Type:AWS::EC2::VPC::IdDescription:The virtual private network into which to launch all resourcesSubnetIds:Type:List<AWS::EC2::Subnet::Id>Description:List of subnet IDs where the EC2 instances will be launchedResources:# This task definition has settings which allow it to# be used on both AWS Fargate and Amazon EC2 capacitySampleTaskDefinition:Type:AWS::ECS::TaskDefinitionProperties:Family:nginxRequiresCompatibilities:- EC2- FARGATENetworkMode:awsvpcCpu:256Memory:512ContainerDefinitions:- Name:nginxImage:public.ecr.aws/nginx/nginx:mainline# Deploy the task definition as a service on EC2 capacityEc2Service:Type:AWS::ECS::ServiceProperties:ServiceName:!Sub '${ServiceName}-on-ec2'Cluster:!Ref 'Cluster'DeploymentConfiguration:MaximumPercent:200MinimumHealthyPercent:75CapacityProviderStrategy:- Base:0CapacityProvider:!Ref Ec2CapacityProviderWeight:1NetworkConfiguration:AwsvpcConfiguration:SecurityGroups:- !Ref ServiceSecurityGroupSubnets:- !Select [ 0, !Ref SubnetIds ]- !Select [ 1, !Ref SubnetIds ]DesiredCount:2TaskDefinition:!Ref 'SampleTaskDefinition'# Deploy the task definition as a service on AWS Fargate capacityFargateService:Type:AWS::ECS::ServiceProperties:ServiceName:!Sub '${ServiceName}-on-fargate'Cluster:!Ref 'Cluster'LaunchType:FARGATEDeploymentConfiguration:MaximumPercent:200MinimumHealthyPercent:75NetworkConfiguration:AwsvpcConfiguration:AssignPublicIp:ENABLEDSecurityGroups:- !Ref ServiceSecurityGroupSubnets:- !Select [ 0, !Ref SubnetIds ]- !Select [ 1, !Ref SubnetIds ]DesiredCount:2TaskDefinition:!Ref 'SampleTaskDefinition'# Security group that limits network access# to the taskServiceSecurityGroup:Type:AWS::EC2::SecurityGroupProperties:GroupDescription:Security group for serviceVpcId:!Ref VpcId
This template requires the following parameters:
Cluster - The name of an ECS cluster on this account. This cluster should have EC2 capacity available in it. All ECS clusters come with AWS Fargate support already built-in. For an example of how to deploy an ECS cluster with EC2 capacity there is a pattern for an ECS cluster using a EC2 capacity provider.
If you prefer to create the ECS task definition using JSON, then this snippet
is a minimal example of the properties necessary for a task definition to be compatible across both AWS Fargate and Amazon EC2: