Skip to content

Commit

Permalink
fix(ecs): FargateTaskDefinition supports Tokens for cpu and memory (
Browse files Browse the repository at this point in the history
aws#4224)

* support lazy values in fargate task definition

* reduce test case
  • Loading branch information
parisholley authored and mergify[bot] committed Sep 26, 2019
1 parent 84a1b45 commit c9529f9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
17 changes: 14 additions & 3 deletions packages/@aws-cdk/aws-ecs/lib/fargate/fargate-task-definition.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Construct, Resource } from '@aws-cdk/core';
import { Construct, Lazy, Resource, Token } from '@aws-cdk/core';
import { CommonTaskDefinitionProps, Compatibility, ITaskDefinition, NetworkMode, TaskDefinition } from '../base/task-definition';

/**
Expand Down Expand Up @@ -77,10 +77,21 @@ export class FargateTaskDefinition extends TaskDefinition implements IFargateTas
constructor(scope: Construct, id: string, props: FargateTaskDefinitionProps = {}) {
super(scope, id, {
...props,
cpu: props.cpu !== undefined ? String(props.cpu) : '256',
memoryMiB: props.memoryLimitMiB !== undefined ? String(props.memoryLimitMiB) : '512',
cpu: props.cpu !== undefined ? stringifyNumber(props.cpu) : '256',
memoryMiB: props.memoryLimitMiB !== undefined ? stringifyNumber(props.memoryLimitMiB) : '512',
compatibility: Compatibility.FARGATE,
networkMode: NetworkMode.AWS_VPC,
});
}
}

/**
* Stringify a number directly or lazily if it's a Token
*/
function stringifyNumber(x: number) {
if (Token.isUnresolved(x)) {
return Lazy.stringValue({ produce: context => `${context.resolve(x)}` });
} else {
return `${x}`;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ export = {
test.done();
},

"support lazy cpu and memory values"(test: Test) {
// GIVEN
const stack = new cdk.Stack();

new ecs.FargateTaskDefinition(stack, 'FargateTaskDef', {
cpu: cdk.Lazy.numberValue({produce: () => 128}),
memoryLimitMiB: cdk.Lazy.numberValue({produce: () => 1024})
});

// THEN
expect(stack).to(haveResourceLike("AWS::ECS::TaskDefinition", {
Cpu: "128",
Memory: "1024"
}));

test.done();
},

"with all properties set"(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down

0 comments on commit c9529f9

Please sign in to comment.