From a6055a2b87d036e4bcda6c671d99e4fcda157f61 Mon Sep 17 00:00:00 2001 From: S Murali Krishnan <110023377+mrlikl@users.noreply.github.com> Date: Sat, 11 May 2024 02:30:08 +0530 Subject: [PATCH 1/8] ecs fargate cpu-mem validation --- .../load-balanced-fargate-service-v2.test.ts | 3 +- .../load-balanced-fargate-service.test.ts | 4 +- .../fargate/scheduled-fargate-task.test.ts | 2 +- .../aws-ecs/lib/base/task-definition.ts | 35 ++++++++++++++ .../fargate/fargate-task-definition.test.ts | 46 +++++++++++++------ .../aws-ecs/test/task-definition.test.ts | 40 +++++++++++++--- 6 files changed, 106 insertions(+), 24 deletions(-) diff --git a/packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/load-balanced-fargate-service-v2.test.ts b/packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/load-balanced-fargate-service-v2.test.ts index b7417f4ce962f..586fb1abcdfa5 100644 --- a/packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/load-balanced-fargate-service-v2.test.ts +++ b/packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/load-balanced-fargate-service-v2.test.ts @@ -642,7 +642,8 @@ describe('Application Load Balancer', () => { // WHEN new ApplicationMultipleTargetGroupsFargateService(stack, 'myService', { cluster: new ecs.Cluster(stack, 'EcsCluster', { vpc }), - memoryLimitMiB: 256, + cpu: 256, + memoryLimitMiB: 512, taskImageOptions: { image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'), }, diff --git a/packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/load-balanced-fargate-service.test.ts b/packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/load-balanced-fargate-service.test.ts index 18b6b929f76f6..bf60714822fcd 100644 --- a/packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/load-balanced-fargate-service.test.ts +++ b/packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/load-balanced-fargate-service.test.ts @@ -554,7 +554,7 @@ describe('ApplicationLoadBalancedFargateService', () => { // WHEN const taskDef = new ecs.FargateTaskDefinition(stack1, 'TaskDef', { cpu: 1024, - memoryLimitMiB: 1024, + memoryLimitMiB: 2048, }); const container = taskDef.addContainer('Container', { image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'), @@ -1816,7 +1816,7 @@ describe('NetworkLoadBalancedFargateService', () => { }); const taskDef = new ecs.FargateTaskDefinition(stack2, 'TaskDef', { cpu: 1024, - memoryLimitMiB: 1024, + memoryLimitMiB: 2048, }); const container = taskDef.addContainer('myContainer', { image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'), diff --git a/packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/scheduled-fargate-task.test.ts b/packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/scheduled-fargate-task.test.ts index 3e98cac7e023e..1545a94a0cdf7 100644 --- a/packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/scheduled-fargate-task.test.ts +++ b/packages/aws-cdk-lib/aws-ecs-patterns/test/fargate/scheduled-fargate-task.test.ts @@ -91,7 +91,7 @@ test('Can create a scheduled Fargate Task - with optional props', () => { scheduledFargateTaskImageOptions: { image: ecs.ContainerImage.fromRegistry('henk'), memoryLimitMiB: 512, - cpu: 2, + cpu: 256, ephemeralStorageGiB: 100, environment: { TRIGGER: 'CloudWatch Events' }, }, diff --git a/packages/aws-cdk-lib/aws-ecs/lib/base/task-definition.ts b/packages/aws-cdk-lib/aws-ecs/lib/base/task-definition.ts index c079f1aae02a3..2c2c1cd370db6 100644 --- a/packages/aws-cdk-lib/aws-ecs/lib/base/task-definition.ts +++ b/packages/aws-cdk-lib/aws-ecs/lib/base/task-definition.ts @@ -473,6 +473,12 @@ export class TaskDefinition extends TaskDefinitionBase { this.checkFargateWindowsBasedTasksSize(props.cpu!, props.memoryMiB!, props.runtimePlatform!); } + // Validate CPU and memory combinations if fargate compatible + if (isFargateCompatible(props.compatibility)) { + // Check the combination as per doc https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-cpu-memory-error.html + this.validateFargateTaskDefinitionMemoryCpu(props.cpu!, props.memoryMiB!); + } + this.runtimePlatform = props.runtimePlatform; this._cpu = props.cpu; this._memory = props.memoryMiB; @@ -894,6 +900,35 @@ export class TaskDefinition extends TaskDefinitionBase { throw new Error(`If operatingSystemFamily is ${runtimePlatform.operatingSystemFamily!._operatingSystemFamily}, then cpu must be in 1024 (1 vCPU), 2048 (2 vCPU), or 4096 (4 vCPU). Provided value was: ${cpu}`); } }; + + private validateFargateTaskDefinitionMemoryCpu(cpu: string, memory: string) { + const validCpuMemoryCombinations = [ + { cpu: 256, memory: [512, 1024, 2048] }, + { cpu: 512, memory: this.range(1024, 4096, 1024) }, + { cpu: 1024, memory: this.range(2048, 8192, 1024) }, + { cpu: 2048, memory: this.range(4096, 16384, 1024) }, + { cpu: 4096, memory: this.range(8192, 30720, 1024) }, + { cpu: 8192, memory: this.range(16384, 61440, 4096) }, + { cpu: 16384, memory: this.range(32768, 122880, 8192) }, + ]; + + const isValidCombination = validCpuMemoryCombinations.some((combo) => { + return combo.cpu === Number(cpu) && combo.memory.includes(Number(memory)); + }); + + if (!isValidCombination) { + throw new Error('Invalid CPU and memory combinations for FARGATE compatible task definition - https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-cpu-memory-error.html'); + } + } + + private range(start: number, end: number, step: number): number[] { + const result = []; + for (let i = start; i <= end; i += step) { + result.push(i); + } + return result; + } + } /** diff --git a/packages/aws-cdk-lib/aws-ecs/test/fargate/fargate-task-definition.test.ts b/packages/aws-cdk-lib/aws-ecs/test/fargate/fargate-task-definition.test.ts index def9ba61aaea8..2403ee728b887 100644 --- a/packages/aws-cdk-lib/aws-ecs/test/fargate/fargate-task-definition.test.ts +++ b/packages/aws-cdk-lib/aws-ecs/test/fargate/fargate-task-definition.test.ts @@ -21,20 +21,16 @@ describe('fargate task definition', () => { }); - test('support lazy cpu and memory values', () => { + test('does not support lazy cpu and memory values', () => { // GIVEN const stack = new cdk.Stack(); - new ecs.FargateTaskDefinition(stack, 'FargateTaskDef', { - cpu: cdk.Lazy.number({ produce: () => 128 }), - memoryLimitMiB: cdk.Lazy.number({ produce: () => 1024 }), - }); - - // THEN - Template.fromStack(stack).hasResourceProperties('AWS::ECS::TaskDefinition', { - Cpu: '128', - Memory: '1024', - }); + expect(() => { + new ecs.FargateTaskDefinition(stack, 'FargateTaskDef', { + cpu: cdk.Lazy.number({ produce: () => 256 }), + memoryLimitMiB: cdk.Lazy.number({ produce: () => 1024 }), + }); + }).toThrow(/Invalid CPU and memory combinations for FARGATE compatible task definition/); }); @@ -42,7 +38,7 @@ describe('fargate task definition', () => { // GIVEN const stack = new cdk.Stack(); const taskDefinition = new ecs.FargateTaskDefinition(stack, 'FargateTaskDef', { - cpu: 128, + cpu: 256, executionRole: new iam.Role(stack, 'ExecutionRole', { path: '/', assumedBy: new iam.CompositePrincipal( @@ -72,7 +68,7 @@ describe('fargate task definition', () => { // THEN Template.fromStack(stack).hasResourceProperties('AWS::ECS::TaskDefinition', { - Cpu: '128', + Cpu: '256', ExecutionRoleArn: { 'Fn::GetAtt': [ 'ExecutionRole605A040B', @@ -195,6 +191,30 @@ describe('fargate task definition', () => { }); }).toThrow(/'pidMode' can only be set to 'host' for Fargate containers, got: 'task'./); }); + + test('throws error when invalid CPU and memory combination is provided', () => { + const stack = new cdk.Stack(); + + expect(() => { + new ecs.FargateTaskDefinition(stack, 'FargateTaskDef', { + cpu: 256, + memoryLimitMiB: 125, + }); + }).toThrow(/Invalid CPU and memory combinations for FARGATE compatible task definition/); + }); + + test('succesfull when valid CPU and memory combination is provided', () => { + const stack = new cdk.Stack(); + new ecs.FargateTaskDefinition(stack, 'FargateTaskDef', { + cpu: 256, + memoryLimitMiB: 512, + }); + + Template.fromStack(stack).hasResourceProperties('AWS::ECS::TaskDefinition', { + Cpu: '256', + Memory: '512', + }); + }); }); describe('When configuredAtLaunch in the Volume', ()=> { test('do not throw when configuredAtLaunch is false', () => { diff --git a/packages/aws-cdk-lib/aws-ecs/test/task-definition.test.ts b/packages/aws-cdk-lib/aws-ecs/test/task-definition.test.ts index f098b3e89afcb..9df7d81036b6c 100644 --- a/packages/aws-cdk-lib/aws-ecs/test/task-definition.test.ts +++ b/packages/aws-cdk-lib/aws-ecs/test/task-definition.test.ts @@ -14,7 +14,7 @@ describe('task definition', () => { // WHEN new ecs.TaskDefinition(stack, 'TD', { - cpu: '512', + cpu: '256', memoryMiB: '512', compatibility: ecs.Compatibility.EC2_AND_FARGATE, }); @@ -51,7 +51,7 @@ describe('task definition', () => { assumedBy: new iam.AccountRootPrincipal(), }); const taskDef = new ecs.TaskDefinition(stack, 'TD', { - cpu: '512', + cpu: '256', memoryMiB: '512', compatibility: ecs.Compatibility.EC2_AND_FARGATE, }); @@ -96,7 +96,7 @@ describe('task definition', () => { assumedBy: new iam.ServicePrincipal('ecs-tasks.amazonaws.com'), }); const taskDef = new ecs.TaskDefinition(stack, 'TD', { - cpu: '512', + cpu: '256', memoryMiB: '512', compatibility: ecs.Compatibility.EC2_AND_FARGATE, executionRole: executionRole, @@ -154,7 +154,7 @@ describe('task definition', () => { }, ); const taskDef = new ecs.TaskDefinition(stack, 'TD', { - cpu: '512', + cpu: '256', memoryMiB: '512', compatibility: ecs.Compatibility.EC2_AND_FARGATE, }); @@ -387,7 +387,7 @@ describe('task definition', () => { }, ); const taskDef = new ecs.TaskDefinition(stack, 'TD', { - cpu: '512', + cpu: '256', memoryMiB: '512', compatibility: ecs.Compatibility.EC2_AND_FARGATE, }); @@ -457,6 +457,32 @@ describe('task definition', () => { Template.fromStack(stack); }).toThrow("ECS Container Container must have at least one of 'memoryLimitMiB' or 'memoryReservationMiB' specified"); }); + + test('throws error when invalid CPU and memory combination is provided with Fargate compatibilities', () => { + const stack = new cdk.Stack(); + + expect(() => { + new ecs.TaskDefinition(stack, 'TaskDef', { + compatibility: ecs.Compatibility.EC2_AND_FARGATE, + cpu: '122', + memoryMiB: '513', + }); + }).toThrow(/Invalid CPU and memory combinations for FARGATE compatible task definition/); + }); + + test('succesfull when valid CPU and memory combination is provided with Fargate compatibilities', () => { + const stack = new cdk.Stack(); + new ecs.TaskDefinition(stack, 'TaskDef', { + compatibility: ecs.Compatibility.EC2_AND_FARGATE, + cpu: '256', + memoryMiB: '512', + }); + + Template.fromStack(stack).hasResourceProperties('AWS::ECS::TaskDefinition', { + Cpu: '256', + Memory: '512', + }); + }); }); describe('When importing from an existing Task definition', () => { @@ -560,7 +586,7 @@ describe('task definition', () => { const stack = new cdk.Stack(); const taskDefinition = new ecs.TaskDefinition(stack, 'TaskDef', { cpu: '512', - memoryMiB: '512', + memoryMiB: '1024', compatibility: ecs.Compatibility.FARGATE, }); @@ -610,7 +636,7 @@ describe('task definition', () => { const stack = new cdk.Stack(); const taskDefinition = new ecs.TaskDefinition(stack, 'TaskDef', { cpu: '512', - memoryMiB: '512', + memoryMiB: '1024', compatibility: ecs.Compatibility.FARGATE, }); From f8abbd8a1ac9e462391c9454669dc792de688279 Mon Sep 17 00:00:00 2001 From: S Murali Krishnan <110023377+mrlikl@users.noreply.github.com> Date: Mon, 13 May 2024 00:51:25 +0530 Subject: [PATCH 2/8] fix example in readme --- packages/aws-cdk-lib/aws-ecs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/aws-cdk-lib/aws-ecs/README.md b/packages/aws-cdk-lib/aws-ecs/README.md index 3dcad8549d0a0..d7e95c2e9c685 100644 --- a/packages/aws-cdk-lib/aws-ecs/README.md +++ b/packages/aws-cdk-lib/aws-ecs/README.md @@ -504,7 +504,7 @@ To grant a principal permission to run your `TaskDefinition`, you can use the `T ```ts declare const role: iam.IGrantable; const taskDef = new ecs.TaskDefinition(this, 'TaskDef', { - cpu: '512', + cpu: '256', memoryMiB: '512', compatibility: ecs.Compatibility.EC2_AND_FARGATE, }); From 9bad34e7c1bff91629be13ef88b193938259531b Mon Sep 17 00:00:00 2001 From: S Murali Krishnan <110023377+mrlikl@users.noreply.github.com> Date: Mon, 13 May 2024 01:06:21 +0530 Subject: [PATCH 3/8] integ tests --- ...efaultTestDeployAssertD76B1D35.assets.json | 19 ++ ...aultTestDeployAssertD76B1D35.template.json | 36 ++++ .../aws-ecs-fargate-task-def.assets.json | 19 ++ .../aws-ecs-fargate-task-def.template.json | 88 ++++++++ .../cdk.out | 1 + .../integ.json | 12 ++ .../manifest.json | 137 ++++++++++++ .../tree.json | 203 ++++++++++++++++++ .../aws-ecs/test/integ.fargate-task-def.ts | 26 +++ 9 files changed, 541 insertions(+) create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/FargateTaskDefinitionDefaultTestDeployAssertD76B1D35.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/FargateTaskDefinitionDefaultTestDeployAssertD76B1D35.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/aws-ecs-fargate-task-def.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/aws-ecs-fargate-task-def.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/cdk.out create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/integ.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/manifest.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/tree.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.ts diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/FargateTaskDefinitionDefaultTestDeployAssertD76B1D35.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/FargateTaskDefinitionDefaultTestDeployAssertD76B1D35.assets.json new file mode 100644 index 0000000000000..a2587e6d528bd --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/FargateTaskDefinitionDefaultTestDeployAssertD76B1D35.assets.json @@ -0,0 +1,19 @@ +{ + "version": "36.0.0", + "files": { + "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { + "source": { + "path": "FargateTaskDefinitionDefaultTestDeployAssertD76B1D35.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/FargateTaskDefinitionDefaultTestDeployAssertD76B1D35.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/FargateTaskDefinitionDefaultTestDeployAssertD76B1D35.template.json new file mode 100644 index 0000000000000..ad9d0fb73d1dd --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/FargateTaskDefinitionDefaultTestDeployAssertD76B1D35.template.json @@ -0,0 +1,36 @@ +{ + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/aws-ecs-fargate-task-def.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/aws-ecs-fargate-task-def.assets.json new file mode 100644 index 0000000000000..28e9f385462a0 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/aws-ecs-fargate-task-def.assets.json @@ -0,0 +1,19 @@ +{ + "version": "36.0.0", + "files": { + "30b6428c0aa0eeac7afde451165bbfe8dc8751c3879127230ca21af40f396d3a": { + "source": { + "path": "aws-ecs-fargate-task-def.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "30b6428c0aa0eeac7afde451165bbfe8dc8751c3879127230ca21af40f396d3a.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/aws-ecs-fargate-task-def.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/aws-ecs-fargate-task-def.template.json new file mode 100644 index 0000000000000..ef75302c0dbd5 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/aws-ecs-fargate-task-def.template.json @@ -0,0 +1,88 @@ +{ + "Resources": { + "TaskDefTaskRole1EDB4A67": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "ecs-tasks.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + } + } + }, + "TaskDef54694570": { + "Type": "AWS::ECS::TaskDefinition", + "Properties": { + "ContainerDefinitions": [ + { + "Essential": true, + "Image": "amazon/amazon-ecs-sample", + "Name": "SampleContainer", + "PortMappings": [ + { + "ContainerPort": 80, + "HostPort": 80, + "Protocol": "tcp" + } + ] + } + ], + "Cpu": "256", + "Family": "awsecsfargatetaskdefTaskDef69F258AC", + "Memory": "512", + "NetworkMode": "awsvpc", + "RequiresCompatibilities": [ + "EC2", + "FARGATE" + ], + "TaskRoleArn": { + "Fn::GetAtt": [ + "TaskDefTaskRole1EDB4A67", + "Arn" + ] + } + } + } + }, + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/cdk.out b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/cdk.out new file mode 100644 index 0000000000000..1f0068d32659a --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/cdk.out @@ -0,0 +1 @@ +{"version":"36.0.0"} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/integ.json b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/integ.json new file mode 100644 index 0000000000000..1a97105790686 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/integ.json @@ -0,0 +1,12 @@ +{ + "version": "36.0.0", + "testCases": { + "FargateTaskDefinition/DefaultTest": { + "stacks": [ + "aws-ecs-fargate-task-def" + ], + "assertionStack": "FargateTaskDefinition/DefaultTest/DeployAssert", + "assertionStackName": "FargateTaskDefinitionDefaultTestDeployAssertD76B1D35" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/manifest.json new file mode 100644 index 0000000000000..97d6ac95c2b5a --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/manifest.json @@ -0,0 +1,137 @@ +{ + "version": "36.0.0", + "artifacts": { + "aws-ecs-fargate-task-def.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "aws-ecs-fargate-task-def.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "aws-ecs-fargate-task-def": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "aws-ecs-fargate-task-def.template.json", + "terminationProtection": false, + "validateOnSynth": false, + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/30b6428c0aa0eeac7afde451165bbfe8dc8751c3879127230ca21af40f396d3a.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "aws-ecs-fargate-task-def.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "aws-ecs-fargate-task-def.assets" + ], + "metadata": { + "/aws-ecs-fargate-task-def/TaskDef/TaskRole/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "TaskDefTaskRole1EDB4A67" + } + ], + "/aws-ecs-fargate-task-def/TaskDef/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "TaskDef54694570" + } + ], + "/aws-ecs-fargate-task-def/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/aws-ecs-fargate-task-def/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ], + "TaskDefGraviton2TaskRole32C7B421": [ + { + "type": "aws:cdk:logicalId", + "data": "TaskDefGraviton2TaskRole32C7B421", + "trace": [ + "!!DESTRUCTIVE_CHANGES: WILL_DESTROY" + ] + } + ], + "TaskDefGraviton21BE43931": [ + { + "type": "aws:cdk:logicalId", + "data": "TaskDefGraviton21BE43931", + "trace": [ + "!!DESTRUCTIVE_CHANGES: WILL_DESTROY" + ] + } + ] + }, + "displayName": "aws-ecs-fargate-task-def" + }, + "FargateTaskDefinitionDefaultTestDeployAssertD76B1D35.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "FargateTaskDefinitionDefaultTestDeployAssertD76B1D35.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "FargateTaskDefinitionDefaultTestDeployAssertD76B1D35": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "FargateTaskDefinitionDefaultTestDeployAssertD76B1D35.template.json", + "terminationProtection": false, + "validateOnSynth": false, + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "FargateTaskDefinitionDefaultTestDeployAssertD76B1D35.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "FargateTaskDefinitionDefaultTestDeployAssertD76B1D35.assets" + ], + "metadata": { + "/FargateTaskDefinition/DefaultTest/DeployAssert/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/FargateTaskDefinition/DefaultTest/DeployAssert/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "FargateTaskDefinition/DefaultTest/DeployAssert" + }, + "Tree": { + "type": "cdk:tree", + "properties": { + "file": "tree.json" + } + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/tree.json new file mode 100644 index 0000000000000..9564eb723a9b2 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/tree.json @@ -0,0 +1,203 @@ +{ + "version": "tree-0.1", + "tree": { + "id": "App", + "path": "", + "children": { + "aws-ecs-fargate-task-def": { + "id": "aws-ecs-fargate-task-def", + "path": "aws-ecs-fargate-task-def", + "children": { + "TaskDef": { + "id": "TaskDef", + "path": "aws-ecs-fargate-task-def/TaskDef", + "children": { + "TaskRole": { + "id": "TaskRole", + "path": "aws-ecs-fargate-task-def/TaskDef/TaskRole", + "children": { + "ImportTaskRole": { + "id": "ImportTaskRole", + "path": "aws-ecs-fargate-task-def/TaskDef/TaskRole/ImportTaskRole", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "aws-ecs-fargate-task-def/TaskDef/TaskRole/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::IAM::Role", + "aws:cdk:cloudformation:props": { + "assumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "ecs-tasks.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.CfnRole", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.Role", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "aws-ecs-fargate-task-def/TaskDef/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::ECS::TaskDefinition", + "aws:cdk:cloudformation:props": { + "containerDefinitions": [ + { + "essential": true, + "image": "amazon/amazon-ecs-sample", + "name": "SampleContainer", + "portMappings": [ + { + "containerPort": 80, + "hostPort": 80, + "protocol": "tcp" + } + ] + } + ], + "cpu": "256", + "family": "awsecsfargatetaskdefTaskDef69F258AC", + "memory": "512", + "networkMode": "awsvpc", + "requiresCompatibilities": [ + "EC2", + "FARGATE" + ], + "taskRoleArn": { + "Fn::GetAtt": [ + "TaskDefTaskRole1EDB4A67", + "Arn" + ] + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ecs.CfnTaskDefinition", + "version": "0.0.0" + } + }, + "SampleContainer": { + "id": "SampleContainer", + "path": "aws-ecs-fargate-task-def/TaskDef/SampleContainer", + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ecs.ContainerDefinition", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ecs.TaskDefinition", + "version": "0.0.0" + } + }, + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "aws-ecs-fargate-task-def/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "aws-ecs-fargate-task-def/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + }, + "FargateTaskDefinition": { + "id": "FargateTaskDefinition", + "path": "FargateTaskDefinition", + "children": { + "DefaultTest": { + "id": "DefaultTest", + "path": "FargateTaskDefinition/DefaultTest", + "children": { + "Default": { + "id": "Default", + "path": "FargateTaskDefinition/DefaultTest/Default", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "DeployAssert": { + "id": "DeployAssert", + "path": "FargateTaskDefinition/DefaultTest/DeployAssert", + "children": { + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "FargateTaskDefinition/DefaultTest/DeployAssert/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "FargateTaskDefinition/DefaultTest/DeployAssert/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTestCase", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTest", + "version": "0.0.0" + } + }, + "Tree": { + "id": "Tree", + "path": "Tree", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.App", + "version": "0.0.0" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.ts new file mode 100644 index 0000000000000..e8698b0b8a865 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.ts @@ -0,0 +1,26 @@ +import * as cdk from 'aws-cdk-lib'; +import * as ecs from 'aws-cdk-lib/aws-ecs'; +import { IntegTest } from '@aws-cdk/integ-tests-alpha'; + +const app = new cdk.App(); +const stack = new cdk.Stack(app, 'aws-ecs-fargate-task-def'); + +const taskDefinition = new ecs.TaskDefinition(stack, 'TaskDef', { + compatibility: ecs.Compatibility.EC2_AND_FARGATE, + cpu: '256', + memoryMiB: '512', +}); + +taskDefinition.addContainer('SampleContainer', { + image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'), + essential: true, + portMappings: [ + { containerPort: 80, hostPort: 80, protocol: ecs.Protocol.TCP }, + ], +}); + +new IntegTest(app, 'FargateTaskDefinition', { + testCases: [stack], +}); + +app.synth(); \ No newline at end of file From 3ef0622169c959f34af7f83c56d72749942f09a2 Mon Sep 17 00:00:00 2001 From: S Murali Krishnan <110023377+mrlikl@users.noreply.github.com> Date: Mon, 17 Jun 2024 18:42:29 +0530 Subject: [PATCH 4/8] remove typecasting, combine checks --- .../aws-ecs/lib/base/task-definition.ts | 74 +++++++++---------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/packages/aws-cdk-lib/aws-ecs/lib/base/task-definition.ts b/packages/aws-cdk-lib/aws-ecs/lib/base/task-definition.ts index 388ca38e94e9b..045d55cdd5fe0 100644 --- a/packages/aws-cdk-lib/aws-ecs/lib/base/task-definition.ts +++ b/packages/aws-cdk-lib/aws-ecs/lib/base/task-definition.ts @@ -432,23 +432,9 @@ export class TaskDefinition extends TaskDefinitionBase { } this.networkMode = props.networkMode ?? (this.isFargateCompatible ? NetworkMode.AWS_VPC : NetworkMode.BRIDGE); - if (this.isFargateCompatible && this.networkMode !== NetworkMode.AWS_VPC) { - throw new Error(`Fargate tasks can only have AwsVpc network mode, got: ${this.networkMode}`); - } if (props.proxyConfiguration && this.networkMode !== NetworkMode.AWS_VPC) { throw new Error(`ProxyConfiguration can only be used with AwsVpc network mode, got: ${this.networkMode}`); } - if (props.placementConstraints && props.placementConstraints.length > 0 && this.isFargateCompatible) { - throw new Error('Cannot set placement constraints on tasks that run on Fargate'); - } - - if (this.isFargateCompatible && (!props.cpu || !props.memoryMiB)) { - throw new Error(`Fargate-compatible tasks require both CPU (${props.cpu}) and memory (${props.memoryMiB}) specifications`); - } - - if (props.inferenceAccelerators && props.inferenceAccelerators.length > 0 && this.isFargateCompatible) { - throw new Error('Cannot use inference accelerators on tasks that run on Fargate'); - } if (this.isExternalCompatible && ![NetworkMode.BRIDGE, NetworkMode.HOST, NetworkMode.NONE].includes(this.networkMode)) { throw new Error(`External tasks can only have Bridge, Host or None network mode, got: ${this.networkMode}`); @@ -458,6 +444,33 @@ export class TaskDefinition extends TaskDefinitionBase { throw new Error('Cannot specify runtimePlatform in non-Fargate compatible tasks'); } + //FARGATE compatible tasks pre-checks + if (this.isFargateCompatible) { + if (this.networkMode !== NetworkMode.AWS_VPC) { + throw new Error(`Fargate tasks can only have AwsVpc network mode, got: ${this.networkMode}`); + } + + if (props.placementConstraints && props.placementConstraints.length > 0) { + throw new Error('Cannot set placement constraints on tasks that run on Fargate'); + } + + if (!props.cpu || !props.memoryMiB) { + throw new Error(`Fargate-compatible tasks require both CPU (${props.cpu}) and memory (${props.memoryMiB}) specifications`); + } + + if (props.inferenceAccelerators && props.inferenceAccelerators.length > 0) { + throw new Error('Cannot use inference accelerators on tasks that run on Fargate'); + } + + // Validate the CPU and memory size for the Windows operation system family. + if (props.runtimePlatform?.operatingSystemFamily?.isWindows()) { + this.checkFargateWindowsBasedTasksSize(props.cpu, props.memoryMiB, props.runtimePlatform); + } + + // Check the combination as per doc https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-cpu-memory-error.html + this.validateFargateTaskDefinitionMemoryCpu(props.cpu, props.memoryMiB); + } + this._executionRole = props.executionRole; this.taskRole = props.taskRole || new iam.Role(this, 'TaskRole', { @@ -471,19 +484,6 @@ export class TaskDefinition extends TaskDefinitionBase { this.ephemeralStorageGiB = props.ephemeralStorageGiB; this.pidMode = props.pidMode; - // validate the cpu and memory size for the Windows operation system family. - if (props.runtimePlatform?.operatingSystemFamily?.isWindows()) { - // We know that props.cpu and props.memoryMiB are defined because an error would have been thrown previously if they were not. - // But, typescript is not able to figure this out, so using the `!` operator here to let the type-checker know they are defined. - this.checkFargateWindowsBasedTasksSize(props.cpu!, props.memoryMiB!, props.runtimePlatform!); - } - - // Validate CPU and memory combinations if fargate compatible - if (isFargateCompatible(props.compatibility)) { - // Check the combination as per doc https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-cpu-memory-error.html - this.validateFargateTaskDefinitionMemoryCpu(props.cpu!, props.memoryMiB!); - } - this.runtimePlatform = props.runtimePlatform; this._cpu = props.cpu; this._memory = props.memoryMiB; @@ -908,17 +908,17 @@ export class TaskDefinition extends TaskDefinitionBase { private validateFargateTaskDefinitionMemoryCpu(cpu: string, memory: string) { const validCpuMemoryCombinations = [ - { cpu: 256, memory: [512, 1024, 2048] }, - { cpu: 512, memory: this.range(1024, 4096, 1024) }, - { cpu: 1024, memory: this.range(2048, 8192, 1024) }, - { cpu: 2048, memory: this.range(4096, 16384, 1024) }, - { cpu: 4096, memory: this.range(8192, 30720, 1024) }, - { cpu: 8192, memory: this.range(16384, 61440, 4096) }, - { cpu: 16384, memory: this.range(32768, 122880, 8192) }, + { cpu: '256', memory: ['512', '1024', '2048'] }, + { cpu: '512', memory: this.range(1024, 4096, 1024) }, + { cpu: '1024', memory: this.range(2048, 8192, 1024) }, + { cpu: '2048', memory: this.range(4096, 16384, 1024) }, + { cpu: '4096', memory: this.range(8192, 30720, 1024) }, + { cpu: '8192', memory: this.range(16384, 61440, 4096) }, + { cpu: '16384', memory: this.range(32768, 122880, 8192) }, ]; const isValidCombination = validCpuMemoryCombinations.some((combo) => { - return combo.cpu === Number(cpu) && combo.memory.includes(Number(memory)); + return combo.cpu === cpu && combo.memory.includes(memory); }); if (!isValidCombination) { @@ -926,10 +926,10 @@ export class TaskDefinition extends TaskDefinitionBase { } } - private range(start: number, end: number, step: number): number[] { + private range(start: number, end: number, step: number): string[] { const result = []; for (let i = start; i <= end; i += step) { - result.push(i); + result.push(String(i)); } return result; } From 928efc27a7efc87a7ad98a5584c836c2a8567fac Mon Sep 17 00:00:00 2001 From: S Murali Krishnan <110023377+mrlikl@users.noreply.github.com> Date: Tue, 18 Jun 2024 23:07:24 +0530 Subject: [PATCH 5/8] support lazy loading --- .../aws-ecs/lib/base/task-definition.ts | 11 +++++++--- .../fargate/fargate-task-definition.test.ts | 22 +++++++++++-------- .../aws-ecs/test/task-definition.test.ts | 12 +++++----- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/packages/aws-cdk-lib/aws-ecs/lib/base/task-definition.ts b/packages/aws-cdk-lib/aws-ecs/lib/base/task-definition.ts index 045d55cdd5fe0..8d5e35fe7abeb 100644 --- a/packages/aws-cdk-lib/aws-ecs/lib/base/task-definition.ts +++ b/packages/aws-cdk-lib/aws-ecs/lib/base/task-definition.ts @@ -468,7 +468,9 @@ export class TaskDefinition extends TaskDefinitionBase { } // Check the combination as per doc https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-cpu-memory-error.html - this.validateFargateTaskDefinitionMemoryCpu(props.cpu, props.memoryMiB); + this.node.addValidation({ + validate: () => this.validateFargateTaskDefinitionMemoryCpu(props.cpu!, props.memoryMiB!), + }); } this._executionRole = props.executionRole; @@ -906,7 +908,8 @@ export class TaskDefinition extends TaskDefinitionBase { } }; - private validateFargateTaskDefinitionMemoryCpu(cpu: string, memory: string) { + private validateFargateTaskDefinitionMemoryCpu(cpu: string, memory: string): string[] { + const ret = new Array(); const validCpuMemoryCombinations = [ { cpu: '256', memory: ['512', '1024', '2048'] }, { cpu: '512', memory: this.range(1024, 4096, 1024) }, @@ -922,8 +925,10 @@ export class TaskDefinition extends TaskDefinitionBase { }); if (!isValidCombination) { - throw new Error('Invalid CPU and memory combinations for FARGATE compatible task definition - https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-cpu-memory-error.html'); + ret.push('Invalid CPU and memory combinations for FARGATE compatible task definition - https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-cpu-memory-error.html'); } + + return ret; } private range(start: number, end: number, step: number): string[] { diff --git a/packages/aws-cdk-lib/aws-ecs/test/fargate/fargate-task-definition.test.ts b/packages/aws-cdk-lib/aws-ecs/test/fargate/fargate-task-definition.test.ts index 20e0ce290fb5f..3d1eaf19b59db 100644 --- a/packages/aws-cdk-lib/aws-ecs/test/fargate/fargate-task-definition.test.ts +++ b/packages/aws-cdk-lib/aws-ecs/test/fargate/fargate-task-definition.test.ts @@ -21,15 +21,17 @@ describe('fargate task definition', () => { }); - test('does not support lazy cpu and memory values', () => { + test('support lazy cpu and memory values', () => { // GIVEN const stack = new cdk.Stack(); + new ecs.FargateTaskDefinition(stack, 'FargateTaskDef', { + cpu: cdk.Lazy.number({ produce: () => 256 }), + memoryLimitMiB: cdk.Lazy.number({ produce: () => 1024 }), + }); + expect(() => { - new ecs.FargateTaskDefinition(stack, 'FargateTaskDef', { - cpu: cdk.Lazy.number({ produce: () => 256 }), - memoryLimitMiB: cdk.Lazy.number({ produce: () => 1024 }), - }); + Template.fromStack(stack); }).toThrow(/Invalid CPU and memory combinations for FARGATE compatible task definition/); }); @@ -216,11 +218,13 @@ describe('fargate task definition', () => { test('throws error when invalid CPU and memory combination is provided', () => { const stack = new cdk.Stack(); + new ecs.FargateTaskDefinition(stack, 'FargateTaskDef', { + cpu: 256, + memoryLimitMiB: 125, + }); + expect(() => { - new ecs.FargateTaskDefinition(stack, 'FargateTaskDef', { - cpu: 256, - memoryLimitMiB: 125, - }); + Template.fromStack(stack); }).toThrow(/Invalid CPU and memory combinations for FARGATE compatible task definition/); }); diff --git a/packages/aws-cdk-lib/aws-ecs/test/task-definition.test.ts b/packages/aws-cdk-lib/aws-ecs/test/task-definition.test.ts index 9df7d81036b6c..728086b77e13c 100644 --- a/packages/aws-cdk-lib/aws-ecs/test/task-definition.test.ts +++ b/packages/aws-cdk-lib/aws-ecs/test/task-definition.test.ts @@ -461,12 +461,14 @@ describe('task definition', () => { test('throws error when invalid CPU and memory combination is provided with Fargate compatibilities', () => { const stack = new cdk.Stack(); + new ecs.TaskDefinition(stack, 'TaskDef', { + compatibility: ecs.Compatibility.EC2_AND_FARGATE, + cpu: '122', + memoryMiB: '513', + }); + expect(() => { - new ecs.TaskDefinition(stack, 'TaskDef', { - compatibility: ecs.Compatibility.EC2_AND_FARGATE, - cpu: '122', - memoryMiB: '513', - }); + Template.fromStack(stack); }).toThrow(/Invalid CPU and memory combinations for FARGATE compatible task definition/); }); From e0978bd2786e57dfbe0f4bbe73d41b9a38b7e696 Mon Sep 17 00:00:00 2001 From: S Murali Krishnan <110023377+mrlikl@users.noreply.github.com> Date: Mon, 29 Jul 2024 10:40:38 +0530 Subject: [PATCH 6/8] spelling+add integ tests --- .../aws-ecs-fargate-task-def.assets.json | 4 +- .../aws-ecs-fargate-task-def.template.json | 1 - .../manifest.json | 25 +-- .../tree.json | 3 +- .../aws-ecs/test/integ.fargate-task-def.ts | 7 +- ...efaultTestDeployAssertF13B2133.assets.json | 19 ++ ...aultTestDeployAssertF13B2133.template.json | 36 ++++ .../aws-ecs-task-def.assets.json | 19 ++ .../aws-ecs-task-def.template.json | 88 ++++++++ .../test/integ.task-def.js.snapshot/cdk.out | 1 + .../integ.task-def.js.snapshot/integ.json | 12 ++ .../integ.task-def.js.snapshot/manifest.json | 119 ++++++++++ .../test/integ.task-def.js.snapshot/tree.json | 203 ++++++++++++++++++ .../test/aws-ecs/test/integ.task-def.ts | 26 +++ ...servicecatalog-deploy-action-beta1.test.ts | 2 +- .../fargate/fargate-task-definition.test.ts | 2 +- .../aws-ecs/test/task-definition.test.ts | 2 +- .../api/cloudformation-deployments.test.ts | 2 +- 18 files changed, 538 insertions(+), 33 deletions(-) create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/TaskDefinitionDefaultTestDeployAssertF13B2133.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/TaskDefinitionDefaultTestDeployAssertF13B2133.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/aws-ecs-task-def.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/aws-ecs-task-def.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/cdk.out create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/integ.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/manifest.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/tree.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.ts diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/aws-ecs-fargate-task-def.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/aws-ecs-fargate-task-def.assets.json index 28e9f385462a0..47920c3666a13 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/aws-ecs-fargate-task-def.assets.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/aws-ecs-fargate-task-def.assets.json @@ -1,7 +1,7 @@ { "version": "36.0.0", "files": { - "30b6428c0aa0eeac7afde451165bbfe8dc8751c3879127230ca21af40f396d3a": { + "9cc4d79897c01e0d9dc06bb0648af4d1d360aea0fd4d556081bed713d96d2436": { "source": { "path": "aws-ecs-fargate-task-def.template.json", "packaging": "file" @@ -9,7 +9,7 @@ "destinations": { "current_account-current_region": { "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", - "objectKey": "30b6428c0aa0eeac7afde451165bbfe8dc8751c3879127230ca21af40f396d3a.json", + "objectKey": "9cc4d79897c01e0d9dc06bb0648af4d1d360aea0fd4d556081bed713d96d2436.json", "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" } } diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/aws-ecs-fargate-task-def.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/aws-ecs-fargate-task-def.template.json index ef75302c0dbd5..17d20d29f57ef 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/aws-ecs-fargate-task-def.template.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/aws-ecs-fargate-task-def.template.json @@ -39,7 +39,6 @@ "Memory": "512", "NetworkMode": "awsvpc", "RequiresCompatibilities": [ - "EC2", "FARGATE" ], "TaskRoleArn": { diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/manifest.json index 97d6ac95c2b5a..11f65944a28fc 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/manifest.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/manifest.json @@ -18,7 +18,7 @@ "validateOnSynth": false, "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", - "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/30b6428c0aa0eeac7afde451165bbfe8dc8751c3879127230ca21af40f396d3a.json", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/9cc4d79897c01e0d9dc06bb0648af4d1d360aea0fd4d556081bed713d96d2436.json", "requiresBootstrapStackVersion": 6, "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", "additionalDependencies": [ @@ -43,7 +43,10 @@ "/aws-ecs-fargate-task-def/TaskDef/Resource": [ { "type": "aws:cdk:logicalId", - "data": "TaskDef54694570" + "data": "TaskDef54694570", + "trace": [ + "!!DESTRUCTIVE_CHANGES: WILL_REPLACE" + ] } ], "/aws-ecs-fargate-task-def/BootstrapVersion": [ @@ -57,24 +60,6 @@ "type": "aws:cdk:logicalId", "data": "CheckBootstrapVersion" } - ], - "TaskDefGraviton2TaskRole32C7B421": [ - { - "type": "aws:cdk:logicalId", - "data": "TaskDefGraviton2TaskRole32C7B421", - "trace": [ - "!!DESTRUCTIVE_CHANGES: WILL_DESTROY" - ] - } - ], - "TaskDefGraviton21BE43931": [ - { - "type": "aws:cdk:logicalId", - "data": "TaskDefGraviton21BE43931", - "trace": [ - "!!DESTRUCTIVE_CHANGES: WILL_DESTROY" - ] - } ] }, "displayName": "aws-ecs-fargate-task-def" diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/tree.json index 9564eb723a9b2..063f35b7938f7 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/tree.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.js.snapshot/tree.json @@ -80,7 +80,6 @@ "memory": "512", "networkMode": "awsvpc", "requiresCompatibilities": [ - "EC2", "FARGATE" ], "taskRoleArn": { @@ -106,7 +105,7 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_ecs.TaskDefinition", + "fqn": "aws-cdk-lib.aws_ecs.FargateTaskDefinition", "version": "0.0.0" } }, diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.ts index e8698b0b8a865..46be63397f340 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.ts +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.fargate-task-def.ts @@ -5,10 +5,9 @@ import { IntegTest } from '@aws-cdk/integ-tests-alpha'; const app = new cdk.App(); const stack = new cdk.Stack(app, 'aws-ecs-fargate-task-def'); -const taskDefinition = new ecs.TaskDefinition(stack, 'TaskDef', { - compatibility: ecs.Compatibility.EC2_AND_FARGATE, - cpu: '256', - memoryMiB: '512', +const taskDefinition = new ecs.FargateTaskDefinition(stack, 'TaskDef', { + cpu: 256, + memoryLimitMiB: 512, }); taskDefinition.addContainer('SampleContainer', { diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/TaskDefinitionDefaultTestDeployAssertF13B2133.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/TaskDefinitionDefaultTestDeployAssertF13B2133.assets.json new file mode 100644 index 0000000000000..c36858976d347 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/TaskDefinitionDefaultTestDeployAssertF13B2133.assets.json @@ -0,0 +1,19 @@ +{ + "version": "36.0.0", + "files": { + "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { + "source": { + "path": "TaskDefinitionDefaultTestDeployAssertF13B2133.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/TaskDefinitionDefaultTestDeployAssertF13B2133.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/TaskDefinitionDefaultTestDeployAssertF13B2133.template.json new file mode 100644 index 0000000000000..ad9d0fb73d1dd --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/TaskDefinitionDefaultTestDeployAssertF13B2133.template.json @@ -0,0 +1,36 @@ +{ + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/aws-ecs-task-def.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/aws-ecs-task-def.assets.json new file mode 100644 index 0000000000000..0c6a62c1ef756 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/aws-ecs-task-def.assets.json @@ -0,0 +1,19 @@ +{ + "version": "36.0.0", + "files": { + "69e8cabd26b07a22fe937a35822c9447fa44ce785d99d44da971c9f953f701da": { + "source": { + "path": "aws-ecs-task-def.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "69e8cabd26b07a22fe937a35822c9447fa44ce785d99d44da971c9f953f701da.json", + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" + } + } + } + }, + "dockerImages": {} +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/aws-ecs-task-def.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/aws-ecs-task-def.template.json new file mode 100644 index 0000000000000..9b0e24807ea39 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/aws-ecs-task-def.template.json @@ -0,0 +1,88 @@ +{ + "Resources": { + "TaskDefTaskRole1EDB4A67": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "ecs-tasks.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + } + } + }, + "TaskDef54694570": { + "Type": "AWS::ECS::TaskDefinition", + "Properties": { + "ContainerDefinitions": [ + { + "Essential": true, + "Image": "amazon/amazon-ecs-sample", + "Name": "SampleContainer", + "PortMappings": [ + { + "ContainerPort": 80, + "HostPort": 80, + "Protocol": "tcp" + } + ] + } + ], + "Cpu": "256", + "Family": "awsecstaskdefTaskDefDBCEF036", + "Memory": "512", + "NetworkMode": "awsvpc", + "RequiresCompatibilities": [ + "EC2", + "FARGATE" + ], + "TaskRoleArn": { + "Fn::GetAtt": [ + "TaskDefTaskRole1EDB4A67", + "Arn" + ] + } + } + } + }, + "Parameters": { + "BootstrapVersion": { + "Type": "AWS::SSM::Parameter::Value", + "Default": "/cdk-bootstrap/hnb659fds/version", + "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]" + } + }, + "Rules": { + "CheckBootstrapVersion": { + "Assertions": [ + { + "Assert": { + "Fn::Not": [ + { + "Fn::Contains": [ + [ + "1", + "2", + "3", + "4", + "5" + ], + { + "Ref": "BootstrapVersion" + } + ] + } + ] + }, + "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI." + } + ] + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/cdk.out b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/cdk.out new file mode 100644 index 0000000000000..1f0068d32659a --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/cdk.out @@ -0,0 +1 @@ +{"version":"36.0.0"} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/integ.json b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/integ.json new file mode 100644 index 0000000000000..d996244d1508f --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/integ.json @@ -0,0 +1,12 @@ +{ + "version": "36.0.0", + "testCases": { + "TaskDefinition/DefaultTest": { + "stacks": [ + "aws-ecs-task-def" + ], + "assertionStack": "TaskDefinition/DefaultTest/DeployAssert", + "assertionStackName": "TaskDefinitionDefaultTestDeployAssertF13B2133" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/manifest.json new file mode 100644 index 0000000000000..ead60c3c0ea88 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/manifest.json @@ -0,0 +1,119 @@ +{ + "version": "36.0.0", + "artifacts": { + "aws-ecs-task-def.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "aws-ecs-task-def.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "aws-ecs-task-def": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "aws-ecs-task-def.template.json", + "terminationProtection": false, + "validateOnSynth": false, + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/69e8cabd26b07a22fe937a35822c9447fa44ce785d99d44da971c9f953f701da.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "aws-ecs-task-def.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "aws-ecs-task-def.assets" + ], + "metadata": { + "/aws-ecs-task-def/TaskDef/TaskRole/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "TaskDefTaskRole1EDB4A67" + } + ], + "/aws-ecs-task-def/TaskDef/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "TaskDef54694570" + } + ], + "/aws-ecs-task-def/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/aws-ecs-task-def/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "aws-ecs-task-def" + }, + "TaskDefinitionDefaultTestDeployAssertF13B2133.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "TaskDefinitionDefaultTestDeployAssertF13B2133.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "TaskDefinitionDefaultTestDeployAssertF13B2133": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "TaskDefinitionDefaultTestDeployAssertF13B2133.template.json", + "terminationProtection": false, + "validateOnSynth": false, + "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", + "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "TaskDefinitionDefaultTestDeployAssertF13B2133.assets" + ], + "lookupRole": { + "arn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-lookup-role-${AWS::AccountId}-${AWS::Region}", + "requiresBootstrapStackVersion": 8, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "dependencies": [ + "TaskDefinitionDefaultTestDeployAssertF13B2133.assets" + ], + "metadata": { + "/TaskDefinition/DefaultTest/DeployAssert/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/TaskDefinition/DefaultTest/DeployAssert/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "TaskDefinition/DefaultTest/DeployAssert" + }, + "Tree": { + "type": "cdk:tree", + "properties": { + "file": "tree.json" + } + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/tree.json new file mode 100644 index 0000000000000..e2a89e9602c30 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.js.snapshot/tree.json @@ -0,0 +1,203 @@ +{ + "version": "tree-0.1", + "tree": { + "id": "App", + "path": "", + "children": { + "aws-ecs-task-def": { + "id": "aws-ecs-task-def", + "path": "aws-ecs-task-def", + "children": { + "TaskDef": { + "id": "TaskDef", + "path": "aws-ecs-task-def/TaskDef", + "children": { + "TaskRole": { + "id": "TaskRole", + "path": "aws-ecs-task-def/TaskDef/TaskRole", + "children": { + "ImportTaskRole": { + "id": "ImportTaskRole", + "path": "aws-ecs-task-def/TaskDef/TaskRole/ImportTaskRole", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "aws-ecs-task-def/TaskDef/TaskRole/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::IAM::Role", + "aws:cdk:cloudformation:props": { + "assumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "ecs-tasks.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.CfnRole", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.Role", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "aws-ecs-task-def/TaskDef/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::ECS::TaskDefinition", + "aws:cdk:cloudformation:props": { + "containerDefinitions": [ + { + "essential": true, + "image": "amazon/amazon-ecs-sample", + "name": "SampleContainer", + "portMappings": [ + { + "containerPort": 80, + "hostPort": 80, + "protocol": "tcp" + } + ] + } + ], + "cpu": "256", + "family": "awsecstaskdefTaskDefDBCEF036", + "memory": "512", + "networkMode": "awsvpc", + "requiresCompatibilities": [ + "EC2", + "FARGATE" + ], + "taskRoleArn": { + "Fn::GetAtt": [ + "TaskDefTaskRole1EDB4A67", + "Arn" + ] + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ecs.CfnTaskDefinition", + "version": "0.0.0" + } + }, + "SampleContainer": { + "id": "SampleContainer", + "path": "aws-ecs-task-def/TaskDef/SampleContainer", + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ecs.ContainerDefinition", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ecs.TaskDefinition", + "version": "0.0.0" + } + }, + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "aws-ecs-task-def/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "aws-ecs-task-def/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + }, + "TaskDefinition": { + "id": "TaskDefinition", + "path": "TaskDefinition", + "children": { + "DefaultTest": { + "id": "DefaultTest", + "path": "TaskDefinition/DefaultTest", + "children": { + "Default": { + "id": "Default", + "path": "TaskDefinition/DefaultTest/Default", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "DeployAssert": { + "id": "DeployAssert", + "path": "TaskDefinition/DefaultTest/DeployAssert", + "children": { + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "TaskDefinition/DefaultTest/DeployAssert/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "TaskDefinition/DefaultTest/DeployAssert/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTestCase", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests-alpha.IntegTest", + "version": "0.0.0" + } + }, + "Tree": { + "id": "Tree", + "path": "Tree", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.App", + "version": "0.0.0" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.ts new file mode 100644 index 0000000000000..e9f4780343816 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/integ.task-def.ts @@ -0,0 +1,26 @@ +import * as cdk from 'aws-cdk-lib'; +import * as ecs from 'aws-cdk-lib/aws-ecs'; +import { IntegTest } from '@aws-cdk/integ-tests-alpha'; + +const app = new cdk.App(); +const stack = new cdk.Stack(app, 'aws-ecs-task-def'); + +const taskDefinition = new ecs.TaskDefinition(stack, 'TaskDef', { + compatibility: ecs.Compatibility.EC2_AND_FARGATE, + cpu: '256', + memoryMiB: '512', +}); + +taskDefinition.addContainer('SampleContainer', { + image: ecs.ContainerImage.fromRegistry('amazon/amazon-ecs-sample'), + essential: true, + portMappings: [ + { containerPort: 80, hostPort: 80, protocol: ecs.Protocol.TCP }, + ], +}); + +new IntegTest(app, 'TaskDefinition', { + testCases: [stack], +}); + +app.synth(); \ No newline at end of file diff --git a/packages/aws-cdk-lib/aws-codepipeline-actions/test/servicecatalog/servicecatalog-deploy-action-beta1.test.ts b/packages/aws-cdk-lib/aws-codepipeline-actions/test/servicecatalog/servicecatalog-deploy-action-beta1.test.ts index cee8f139e117f..30388c7801783 100644 --- a/packages/aws-cdk-lib/aws-codepipeline-actions/test/servicecatalog/servicecatalog-deploy-action-beta1.test.ts +++ b/packages/aws-cdk-lib/aws-codepipeline-actions/test/servicecatalog/servicecatalog-deploy-action-beta1.test.ts @@ -7,7 +7,7 @@ import * as cpactions from '../../lib'; /* eslint-disable quote-props */ describe('ServiceCatalog Deploy Action', () => { - test('addAction succesfully leads to creation of codepipeline service catalog action with properly formatted TemplateFilePath', () => { + test('addAction successfully leads to creation of codepipeline service catalog action with properly formatted TemplateFilePath', () => { // GIVEN const stack = new TestFixture(); // WHEN diff --git a/packages/aws-cdk-lib/aws-ecs/test/fargate/fargate-task-definition.test.ts b/packages/aws-cdk-lib/aws-ecs/test/fargate/fargate-task-definition.test.ts index 3d1eaf19b59db..a64570c368529 100644 --- a/packages/aws-cdk-lib/aws-ecs/test/fargate/fargate-task-definition.test.ts +++ b/packages/aws-cdk-lib/aws-ecs/test/fargate/fargate-task-definition.test.ts @@ -228,7 +228,7 @@ describe('fargate task definition', () => { }).toThrow(/Invalid CPU and memory combinations for FARGATE compatible task definition/); }); - test('succesfull when valid CPU and memory combination is provided', () => { + test('successful when valid CPU and memory combination is provided', () => { const stack = new cdk.Stack(); new ecs.FargateTaskDefinition(stack, 'FargateTaskDef', { cpu: 256, diff --git a/packages/aws-cdk-lib/aws-ecs/test/task-definition.test.ts b/packages/aws-cdk-lib/aws-ecs/test/task-definition.test.ts index 728086b77e13c..2347ded71b363 100644 --- a/packages/aws-cdk-lib/aws-ecs/test/task-definition.test.ts +++ b/packages/aws-cdk-lib/aws-ecs/test/task-definition.test.ts @@ -472,7 +472,7 @@ describe('task definition', () => { }).toThrow(/Invalid CPU and memory combinations for FARGATE compatible task definition/); }); - test('succesfull when valid CPU and memory combination is provided with Fargate compatibilities', () => { + test('successful when valid CPU and memory combination is provided with Fargate compatibilities', () => { const stack = new cdk.Stack(); new ecs.TaskDefinition(stack, 'TaskDef', { compatibility: ecs.Compatibility.EC2_AND_FARGATE, diff --git a/packages/aws-cdk/test/api/cloudformation-deployments.test.ts b/packages/aws-cdk/test/api/cloudformation-deployments.test.ts index cbaf7c3d8746c..7d6288ba10249 100644 --- a/packages/aws-cdk/test/api/cloudformation-deployments.test.ts +++ b/packages/aws-cdk/test/api/cloudformation-deployments.test.ts @@ -756,7 +756,7 @@ test('readCurrentTemplateWithNestedStacks() caches calls to listStackResources() expect(numberOfTimesListStackResourcesWasCalled).toEqual(1); }); -test('readCurrentTemplateWithNestedStacks() succesfully ignores stacks without metadata', async () => { +test('readCurrentTemplateWithNestedStacks() successfully ignores stacks without metadata', async () => { // GIVEN const cfnStack = new FakeCloudformationStack({ stackName: 'MetadataRoot', From 78f66a0c7640e6fef468c80d7ecee5ff849e3e5d Mon Sep 17 00:00:00 2001 From: S Murali Krishnan <110023377+mrlikl@users.noreply.github.com> Date: Mon, 29 Jul 2024 11:23:31 +0530 Subject: [PATCH 7/8] support lazy values --- .../aws-cdk-lib/aws-ecs/lib/base/task-definition.ts | 4 +++- .../test/fargate/fargate-task-definition.test.ts | 10 ++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/aws-cdk-lib/aws-ecs/lib/base/task-definition.ts b/packages/aws-cdk-lib/aws-ecs/lib/base/task-definition.ts index 8d5e35fe7abeb..ec8fdc8f232a5 100644 --- a/packages/aws-cdk-lib/aws-ecs/lib/base/task-definition.ts +++ b/packages/aws-cdk-lib/aws-ecs/lib/base/task-definition.ts @@ -910,6 +910,8 @@ export class TaskDefinition extends TaskDefinitionBase { private validateFargateTaskDefinitionMemoryCpu(cpu: string, memory: string): string[] { const ret = new Array(); + const resolvedCpu = this.stack.resolve(cpu) as string; + const resolvedMemoryMiB = this.stack.resolve(memory) as string; const validCpuMemoryCombinations = [ { cpu: '256', memory: ['512', '1024', '2048'] }, { cpu: '512', memory: this.range(1024, 4096, 1024) }, @@ -921,7 +923,7 @@ export class TaskDefinition extends TaskDefinitionBase { ]; const isValidCombination = validCpuMemoryCombinations.some((combo) => { - return combo.cpu === cpu && combo.memory.includes(memory); + return combo.cpu === resolvedCpu && combo.memory.includes(resolvedMemoryMiB); }); if (!isValidCombination) { diff --git a/packages/aws-cdk-lib/aws-ecs/test/fargate/fargate-task-definition.test.ts b/packages/aws-cdk-lib/aws-ecs/test/fargate/fargate-task-definition.test.ts index a64570c368529..9d7c03c4a877f 100644 --- a/packages/aws-cdk-lib/aws-ecs/test/fargate/fargate-task-definition.test.ts +++ b/packages/aws-cdk-lib/aws-ecs/test/fargate/fargate-task-definition.test.ts @@ -26,13 +26,15 @@ describe('fargate task definition', () => { const stack = new cdk.Stack(); new ecs.FargateTaskDefinition(stack, 'FargateTaskDef', { - cpu: cdk.Lazy.number({ produce: () => 256 }), + cpu: cdk.Lazy.number({ produce: () => 512 }), memoryLimitMiB: cdk.Lazy.number({ produce: () => 1024 }), }); - expect(() => { - Template.fromStack(stack); - }).toThrow(/Invalid CPU and memory combinations for FARGATE compatible task definition/); + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::ECS::TaskDefinition', { + Cpu: '512', + Memory: '1024', + }); }); From 62d09ae7c1bf992942dcef5fc8d059fd05141c80 Mon Sep 17 00:00:00 2001 From: S Murali Krishnan <110023377+mrlikl@users.noreply.github.com> Date: Tue, 30 Jul 2024 23:18:04 +0530 Subject: [PATCH 8/8] revert windows outside of fargate checks --- .../aws-cdk-lib/aws-ecs/lib/base/task-definition.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/aws-cdk-lib/aws-ecs/lib/base/task-definition.ts b/packages/aws-cdk-lib/aws-ecs/lib/base/task-definition.ts index ec8fdc8f232a5..c3896e1113811 100644 --- a/packages/aws-cdk-lib/aws-ecs/lib/base/task-definition.ts +++ b/packages/aws-cdk-lib/aws-ecs/lib/base/task-definition.ts @@ -462,11 +462,6 @@ export class TaskDefinition extends TaskDefinitionBase { throw new Error('Cannot use inference accelerators on tasks that run on Fargate'); } - // Validate the CPU and memory size for the Windows operation system family. - if (props.runtimePlatform?.operatingSystemFamily?.isWindows()) { - this.checkFargateWindowsBasedTasksSize(props.cpu, props.memoryMiB, props.runtimePlatform); - } - // Check the combination as per doc https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-cpu-memory-error.html this.node.addValidation({ validate: () => this.validateFargateTaskDefinitionMemoryCpu(props.cpu!, props.memoryMiB!), @@ -486,6 +481,13 @@ export class TaskDefinition extends TaskDefinitionBase { this.ephemeralStorageGiB = props.ephemeralStorageGiB; this.pidMode = props.pidMode; + // validate the cpu and memory size for the Windows operation system family. + if (props.runtimePlatform?.operatingSystemFamily?.isWindows()) { + // We know that props.cpu and props.memoryMiB are defined because an error would have been thrown previously if they were not. + // But, typescript is not able to figure this out, so using the `!` operator here to let the type-checker know they are defined. + this.checkFargateWindowsBasedTasksSize(props.cpu!, props.memoryMiB!, props.runtimePlatform!); + } + this.runtimePlatform = props.runtimePlatform; this._cpu = props.cpu; this._memory = props.memoryMiB;