Skip to content

Commit

Permalink
fix(ecs): validation for task definition fails when task-level memory…
Browse files Browse the repository at this point in the history
… is defined but container-level memory and memoryReservation are not defined with EC2 compatibility
  • Loading branch information
tam0ri committed Jun 17, 2023
1 parent 0737b4e commit 1c9c5a7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
11 changes: 8 additions & 3 deletions packages/aws-cdk-lib/aws-ecs/lib/base/task-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,8 @@ export class TaskDefinition extends TaskDefinitionBase {

private readonly _cpu?: string;

private readonly _memory?: string;

/**
* Constructs a new instance of the TaskDefinition class.
*/
Expand Down Expand Up @@ -461,6 +463,7 @@ export class TaskDefinition extends TaskDefinitionBase {

this.runtimePlatform = props.runtimePlatform;
this._cpu = props.cpu;
this._memory = props.memoryMiB;

const taskDef = new CfnTaskDefinition(this, 'Resource', {
containerDefinitions: Lazy.any({ produce: () => this.renderContainers() }, { omitEmptyArray: true }),
Expand Down Expand Up @@ -736,9 +739,11 @@ export class TaskDefinition extends TaskDefinitionBase {
// EC2 mode validations

// Container sizes
for (const container of this.containers) {
if (!container.memoryLimitSpecified) {
ret.push(`ECS Container ${container.containerName} must have at least one of 'memoryLimitMiB' or 'memoryReservationMiB' specified`);
if (!this._memory) {
for (const container of this.containers) {
if (!container.memoryLimitSpecified) {
ret.push(`ECS Container ${container.containerName} must have at least one of 'memoryLimitMiB' or 'memoryReservationMiB' specified`);
}
}
}
}
Expand Down
42 changes: 41 additions & 1 deletion packages/aws-cdk-lib/aws-ecs/test/task-definition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,46 @@ describe('task definition', () => {
}],
});
});

test('You can omit container-level memory and memoryReservation parameters with EC2 compatibilities if task-level memory parameter is defined', () => {
// GIVEN
const stack = new cdk.Stack();

// WHEN
const taskDef = new ecs.TaskDefinition(stack, 'TD', {
cpu: '512',
memoryMiB: '512',
compatibility: ecs.Compatibility.EC2,
});
taskDef.addContainer('Container', {
image: ecs.ContainerImage.fromRegistry('/aws/aws-example-app'),
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::ECS::TaskDefinition', {
Memory: '512',
});

});

test('A task definition where task-level memory, container-level memory and memoryReservation are not defined throws an error', () => {
// GIVEN
const stack = new cdk.Stack();

// WHEN
const taskDef = new ecs.TaskDefinition(stack, 'TD', {
cpu: '512',
compatibility: ecs.Compatibility.EC2,
});
taskDef.addContainer('Container', {
image: ecs.ContainerImage.fromRegistry('/aws/aws-example-app'),
});

// THEN
expect(() => {
Template.fromStack(stack);
}).toThrow("ECS Container Container must have at least one of 'memoryLimitMiB' or 'memoryReservationMiB' specified");
});
});

describe('When importing from an existing Task definition', () => {
Expand Down Expand Up @@ -476,4 +516,4 @@ describe('task definition', () => {
});
});
});
});
});

0 comments on commit 1c9c5a7

Please sign in to comment.