From f8a94d8fc3a43711c4079d83d1abf0817209fc42 Mon Sep 17 00:00:00 2001 From: Romain Marcadier Date: Tue, 27 Jun 2023 14:56:38 +0200 Subject: [PATCH 1/2] chore: fixup @aws-cdk/cdk-lib-alpha bundling (#26133) The bundle needs to contain the correct WASM embed at the right path in order to be able to load cleanly. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- packages/@aws-cdk/cli-lib-alpha/.gitignore | 2 +- packages/@aws-cdk/cli-lib-alpha/THIRD_PARTY_LICENSES | 8 ++++---- packages/@aws-cdk/cli-lib-alpha/generate.sh | 0 packages/@aws-cdk/cli-lib-alpha/package.json | 5 +++-- 4 files changed, 8 insertions(+), 7 deletions(-) mode change 100644 => 100755 packages/@aws-cdk/cli-lib-alpha/generate.sh diff --git a/packages/@aws-cdk/cli-lib-alpha/.gitignore b/packages/@aws-cdk/cli-lib-alpha/.gitignore index b0ccd70040ce3..9e76cb9fe4569 100644 --- a/packages/@aws-cdk/cli-lib-alpha/.gitignore +++ b/packages/@aws-cdk/cli-lib-alpha/.gitignore @@ -24,4 +24,4 @@ npm-shrinkwrap.json junit.xml - +lib/*.wasm diff --git a/packages/@aws-cdk/cli-lib-alpha/THIRD_PARTY_LICENSES b/packages/@aws-cdk/cli-lib-alpha/THIRD_PARTY_LICENSES index bc8b656dba023..d2983c79d9d47 100644 --- a/packages/@aws-cdk/cli-lib-alpha/THIRD_PARTY_LICENSES +++ b/packages/@aws-cdk/cli-lib-alpha/THIRD_PARTY_LICENSES @@ -37,7 +37,7 @@ THE SOFTWARE. ---------------- -** acorn@8.8.2 - https://www.npmjs.com/package/acorn/v/8.8.2 | MIT +** acorn@8.9.0 - https://www.npmjs.com/package/acorn/v/8.9.0 | MIT MIT License Copyright (C) 2012-2022 by various contributors (see AUTHORS) @@ -268,7 +268,7 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH RE ---------------- -** aws-sdk@2.1379.0 - https://www.npmjs.com/package/aws-sdk/v/2.1379.0 | Apache-2.0 +** aws-sdk@2.1401.0 - https://www.npmjs.com/package/aws-sdk/v/2.1401.0 | Apache-2.0 AWS SDK for JavaScript Copyright 2012-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. @@ -2951,7 +2951,7 @@ License, as follows: ---------------- -** semver@7.5.1 - https://www.npmjs.com/package/semver/v/7.5.1 | ISC +** semver@7.5.2 - https://www.npmjs.com/package/semver/v/7.5.2 | ISC The ISC License Copyright (c) Isaac Z. Schlueter and Contributors @@ -3402,7 +3402,7 @@ SOFTWARE. ---------------- -** tslib@2.5.0 - https://www.npmjs.com/package/tslib/v/2.5.0 | 0BSD +** tslib@2.5.3 - https://www.npmjs.com/package/tslib/v/2.5.3 | 0BSD Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any diff --git a/packages/@aws-cdk/cli-lib-alpha/generate.sh b/packages/@aws-cdk/cli-lib-alpha/generate.sh old mode 100644 new mode 100755 diff --git a/packages/@aws-cdk/cli-lib-alpha/package.json b/packages/@aws-cdk/cli-lib-alpha/package.json index 26af086a3ba67..ed388495c9a2f 100644 --- a/packages/@aws-cdk/cli-lib-alpha/package.json +++ b/packages/@aws-cdk/cli-lib-alpha/package.json @@ -68,8 +68,9 @@ "cdk-build": { "post": [ "yarn attribute", - "cp ../../../node_modules/vm2/lib/bridge.js ../../../node_modules/vm2/lib/setup-sandbox.js ./lib", - "yarn bundle" + "cp ../../../node_modules/vm2/lib/bridge.js ../../../node_modules/vm2/lib/setup-sandbox.js ../../aws-cdk/lib/vendor/noctilucent/index_bg.wasm ./lib/", + "yarn bundle", + "node ./lib/main.js >/dev/null 2>/dev/null Date: Wed, 28 Jun 2023 00:06:05 +0900 Subject: [PATCH 2/2] fix(ecs): validation for task definition fails when task-level memory is defined but container-level memory and memoryReservation are not defined with EC2 compatibility (#26027) Currently, validation for ECS task definition fails when task-level memory is defined but container-level memory and memoryReservation are not defined with EC2 compatibility. On the other hand, if we specify task-level memory, we can omit container-level memory and memoryReservation parameters from ECS API perspective. This PR solves the issue by skipping the validation when task-level memory is defined. Closes #25275 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- ...efaultTestDeployAssert5CA048EE.assets.json | 19 ++ ...aultTestDeployAssert5CA048EE.template.json | 36 ++++ ...without-container-level-memory.assets.json | 19 ++ ...thout-container-level-memory.template.json | 80 +++++++ .../cdk.out | 1 + .../integ.json | 12 ++ .../manifest.json | 117 +++++++++++ .../tree.json | 195 ++++++++++++++++++ ...finition-without-container-level-memory.ts | 23 +++ .../aws-ecs/lib/base/task-definition.ts | 11 +- .../aws-ecs/test/task-definition.test.ts | 42 +++- 11 files changed, 551 insertions(+), 4 deletions(-) create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/TaskDefinitionWithoutContainerLevelMemoryDefaultTestDeployAssert5CA048EE.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/TaskDefinitionWithoutContainerLevelMemoryDefaultTestDeployAssert5CA048EE.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/aws-ecs-task-definition-without-container-level-memory.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/aws-ecs-task-definition-without-container-level-memory.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/cdk.out create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/integ.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/manifest.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/tree.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.ts diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/TaskDefinitionWithoutContainerLevelMemoryDefaultTestDeployAssert5CA048EE.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/TaskDefinitionWithoutContainerLevelMemoryDefaultTestDeployAssert5CA048EE.assets.json new file mode 100644 index 0000000000000..146f43944d502 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/TaskDefinitionWithoutContainerLevelMemoryDefaultTestDeployAssert5CA048EE.assets.json @@ -0,0 +1,19 @@ +{ + "version": "32.0.0", + "files": { + "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { + "source": { + "path": "TaskDefinitionWithoutContainerLevelMemoryDefaultTestDeployAssert5CA048EE.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/ec2/integ.task-definition-without-container-level-memory.js.snapshot/TaskDefinitionWithoutContainerLevelMemoryDefaultTestDeployAssert5CA048EE.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/TaskDefinitionWithoutContainerLevelMemoryDefaultTestDeployAssert5CA048EE.template.json new file mode 100644 index 0000000000000..ad9d0fb73d1dd --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/TaskDefinitionWithoutContainerLevelMemoryDefaultTestDeployAssert5CA048EE.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/ec2/integ.task-definition-without-container-level-memory.js.snapshot/aws-ecs-task-definition-without-container-level-memory.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/aws-ecs-task-definition-without-container-level-memory.assets.json new file mode 100644 index 0000000000000..6b56b0a974806 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/aws-ecs-task-definition-without-container-level-memory.assets.json @@ -0,0 +1,19 @@ +{ + "version": "32.0.0", + "files": { + "f0418187a9bb20f94c9c34694a64572ac4e7cb61962e83798b4266d0351e1f69": { + "source": { + "path": "aws-ecs-task-definition-without-container-level-memory.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "f0418187a9bb20f94c9c34694a64572ac4e7cb61962e83798b4266d0351e1f69.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/ec2/integ.task-definition-without-container-level-memory.js.snapshot/aws-ecs-task-definition-without-container-level-memory.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/aws-ecs-task-definition-without-container-level-memory.template.json new file mode 100644 index 0000000000000..d38507bf6e3b2 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/aws-ecs-task-definition-without-container-level-memory.template.json @@ -0,0 +1,80 @@ +{ + "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": "public.ecr.aws/ecs-sample-image/amazon-ecs-sample:latest", + "Name": "Container" + } + ], + "Cpu": "256", + "Family": "app", + "Memory": "512", + "NetworkMode": "bridge", + "RequiresCompatibilities": [ + "EC2" + ], + "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/ec2/integ.task-definition-without-container-level-memory.js.snapshot/cdk.out b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/cdk.out new file mode 100644 index 0000000000000..f0b901e7c06e5 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/cdk.out @@ -0,0 +1 @@ +{"version":"32.0.0"} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/integ.json b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/integ.json new file mode 100644 index 0000000000000..8a9cb94183985 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/integ.json @@ -0,0 +1,12 @@ +{ + "version": "32.0.0", + "testCases": { + "TaskDefinitionWithoutContainerLevelMemory/DefaultTest": { + "stacks": [ + "aws-ecs-task-definition-without-container-level-memory" + ], + "assertionStack": "TaskDefinitionWithoutContainerLevelMemory/DefaultTest/DeployAssert", + "assertionStackName": "TaskDefinitionWithoutContainerLevelMemoryDefaultTestDeployAssert5CA048EE" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/manifest.json new file mode 100644 index 0000000000000..98505004658be --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/manifest.json @@ -0,0 +1,117 @@ +{ + "version": "32.0.0", + "artifacts": { + "aws-ecs-task-definition-without-container-level-memory.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "aws-ecs-task-definition-without-container-level-memory.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "aws-ecs-task-definition-without-container-level-memory": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "aws-ecs-task-definition-without-container-level-memory.template.json", + "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}/f0418187a9bb20f94c9c34694a64572ac4e7cb61962e83798b4266d0351e1f69.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "aws-ecs-task-definition-without-container-level-memory.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-definition-without-container-level-memory.assets" + ], + "metadata": { + "/aws-ecs-task-definition-without-container-level-memory/TaskDef/TaskRole/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "TaskDefTaskRole1EDB4A67" + } + ], + "/aws-ecs-task-definition-without-container-level-memory/TaskDef/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "TaskDef54694570" + } + ], + "/aws-ecs-task-definition-without-container-level-memory/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/aws-ecs-task-definition-without-container-level-memory/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "aws-ecs-task-definition-without-container-level-memory" + }, + "TaskDefinitionWithoutContainerLevelMemoryDefaultTestDeployAssert5CA048EE.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "TaskDefinitionWithoutContainerLevelMemoryDefaultTestDeployAssert5CA048EE.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "TaskDefinitionWithoutContainerLevelMemoryDefaultTestDeployAssert5CA048EE": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "TaskDefinitionWithoutContainerLevelMemoryDefaultTestDeployAssert5CA048EE.template.json", + "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": [ + "TaskDefinitionWithoutContainerLevelMemoryDefaultTestDeployAssert5CA048EE.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": [ + "TaskDefinitionWithoutContainerLevelMemoryDefaultTestDeployAssert5CA048EE.assets" + ], + "metadata": { + "/TaskDefinitionWithoutContainerLevelMemory/DefaultTest/DeployAssert/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/TaskDefinitionWithoutContainerLevelMemory/DefaultTest/DeployAssert/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "TaskDefinitionWithoutContainerLevelMemory/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/ec2/integ.task-definition-without-container-level-memory.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/tree.json new file mode 100644 index 0000000000000..e4570cc76c379 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.js.snapshot/tree.json @@ -0,0 +1,195 @@ +{ + "version": "tree-0.1", + "tree": { + "id": "App", + "path": "", + "children": { + "aws-ecs-task-definition-without-container-level-memory": { + "id": "aws-ecs-task-definition-without-container-level-memory", + "path": "aws-ecs-task-definition-without-container-level-memory", + "children": { + "TaskDef": { + "id": "TaskDef", + "path": "aws-ecs-task-definition-without-container-level-memory/TaskDef", + "children": { + "TaskRole": { + "id": "TaskRole", + "path": "aws-ecs-task-definition-without-container-level-memory/TaskDef/TaskRole", + "children": { + "ImportTaskRole": { + "id": "ImportTaskRole", + "path": "aws-ecs-task-definition-without-container-level-memory/TaskDef/TaskRole/ImportTaskRole", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "aws-ecs-task-definition-without-container-level-memory/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-definition-without-container-level-memory/TaskDef/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::ECS::TaskDefinition", + "aws:cdk:cloudformation:props": { + "containerDefinitions": [ + { + "essential": true, + "image": "public.ecr.aws/ecs-sample-image/amazon-ecs-sample:latest", + "name": "Container" + } + ], + "cpu": "256", + "family": "app", + "memory": "512", + "networkMode": "bridge", + "requiresCompatibilities": [ + "EC2" + ], + "taskRoleArn": { + "Fn::GetAtt": [ + "TaskDefTaskRole1EDB4A67", + "Arn" + ] + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_ecs.CfnTaskDefinition", + "version": "0.0.0" + } + }, + "Container": { + "id": "Container", + "path": "aws-ecs-task-definition-without-container-level-memory/TaskDef/Container", + "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-definition-without-container-level-memory/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "aws-ecs-task-definition-without-container-level-memory/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + }, + "TaskDefinitionWithoutContainerLevelMemory": { + "id": "TaskDefinitionWithoutContainerLevelMemory", + "path": "TaskDefinitionWithoutContainerLevelMemory", + "children": { + "DefaultTest": { + "id": "DefaultTest", + "path": "TaskDefinitionWithoutContainerLevelMemory/DefaultTest", + "children": { + "Default": { + "id": "Default", + "path": "TaskDefinitionWithoutContainerLevelMemory/DefaultTest/Default", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.2.26" + } + }, + "DeployAssert": { + "id": "DeployAssert", + "path": "TaskDefinitionWithoutContainerLevelMemory/DefaultTest/DeployAssert", + "children": { + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "TaskDefinitionWithoutContainerLevelMemory/DefaultTest/DeployAssert/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "TaskDefinitionWithoutContainerLevelMemory/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.2.26" + } + } + }, + "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/ec2/integ.task-definition-without-container-level-memory.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.ts new file mode 100644 index 0000000000000..22b4d6ef937dd --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-ecs/test/ec2/integ.task-definition-without-container-level-memory.ts @@ -0,0 +1,23 @@ +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-definition-without-container-level-memory'); + +const taskDefinition = new ecs.TaskDefinition(stack, 'TaskDef', { + compatibility: ecs.Compatibility.EC2, + cpu: '256', + family: 'app', + memoryMiB: '512', +}); + +taskDefinition.addContainer('Container', { + image: ecs.ContainerImage.fromRegistry('public.ecr.aws/ecs-sample-image/amazon-ecs-sample:latest'), +}); + +new IntegTest(app, 'TaskDefinitionWithoutContainerLevelMemory', { + testCases: [stack], +}); + +app.synth(); \ No newline at end of file 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 78fd14f3ce95e..1280af3e5cd09 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 @@ -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. */ @@ -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 }), @@ -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`); + } } } } 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 5f50ffe44a59b..571bf6ba61f6d 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 @@ -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', () => { @@ -476,4 +516,4 @@ describe('task definition', () => { }); }); }); -}); +}); \ No newline at end of file