Reputation: 1
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.
Upvotes: 0
Views: 158
Reputation: 2067
Unfortunately it's not so straightforward. CloudFormation can't handle complex structures as parameters. You have two options:
use Lambda-backed custom resources (if you have more cases like that it may be good choice, but for single use like here it seems to be an overkill) - custom resources are kind of exception: all of the params are just pased to them and also returned structures can be comples and still kept by CloudFormation;
upload partial templates to S3 in some CI/CD step (I recommend to version it there in URLs) before triggering CloudFormation.
Upvotes: 1