I have a working CloudFormation template that creates an AWS Elastic Container Service cluster. Here is a sanitized version of one of the TaskDefinitions:
Resources:
MyTaskDefinition:
Properties:
ContainerDefinitions:
- Environment:
- Name: ENV_VAR_1
Value: !Ref EnvVar1
- Name: ENV_VAR_2
Value: !Ref EnvVar2
Image: !Ref Image1
LogConfiguration: !Ref MyLogConfiguration
Memory: 2048
Name: my-container-1
- Environment:
- Name: ENV_VAR_1
Value: !Ref EnvVar1
- Name: ENV_VAR_2
Value: !Ref EnvVar2
Image: !Ref Image2
LogConfiguration: !Ref MyLogConfiguration
Memory: 2048
Name: my-container-2
Type: AWS::ECS::TaskDefinition
As this TaskDefinition grows to define several containers, each of which have multiple environment variables, it becomes difficult to read. Instead I would like to do the following:
Resources:
ContainerDefinition1:
Properties:
Environment:
- Name: ENV_VAR_1
Value: !Ref EnvVar1
- Name: ENV_VAR_2
Value: !Ref EnvVar2
Image: !Ref Image1
LogConfiguration: !Ref MyLogConfiguration
Memory: 2048
Name: my-container-1
Type: AWS::ECS::TaskDefinition::ContainerDefinition
ContainerDefinition2:
Properties:
Environment:
- Name: ENV_VAR_1
Value: !Ref EnvVar1
- Name: ENV_VAR_2
Value: !Ref EnvVar2
Image: !Ref Image2
LogConfiguration: !Ref MyLogConfiguration
Memory: 2048
Name: my-container-2
Type: AWS::ECS::TaskDefinition::ContainerDefinition
MyTaskDefinition:
Properties:
ContainerDefinitions:
- !Ref ContainerDefinition1
- !Ref ContainerDefinition2
Type: AWS::ECS::TaskDefinition
This doesn't work because Type: AWS::ECS::TaskDefinition::ContainerDefinition
is not a valid Resource type in CloudFormation.
Is there another way to modularize the body of a Resource in CloudFormation? If not, how do you recommend keeping CF templates readable as they grow to specify large systems?
I am familiar with the AWS::Include
Transform, but I would like to keep the entire template under version control, rather than posting snippets to S3 where they will be untracked.