From 228f7b98293221a61aceb7f764411a8b1e5d90fd Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Mon, 10 Jun 2024 16:43:53 -0700 Subject: [PATCH 1/6] fix for fn-if tags --- .../test/test-templates/tags-with-fn-if.json | 29 +++++++++++++++++++ .../test/valid-templates.test.ts | 8 +++++ .../core/lib/helpers-internal/cfn-parse.ts | 3 +- packages/aws-cdk-lib/core/lib/runtime.ts | 1 + 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 packages/aws-cdk-lib/cloudformation-include/test/test-templates/tags-with-fn-if.json diff --git a/packages/aws-cdk-lib/cloudformation-include/test/test-templates/tags-with-fn-if.json b/packages/aws-cdk-lib/cloudformation-include/test/test-templates/tags-with-fn-if.json new file mode 100644 index 0000000000000..8dd5cf4b3ed00 --- /dev/null +++ b/packages/aws-cdk-lib/cloudformation-include/test/test-templates/tags-with-fn-if.json @@ -0,0 +1,29 @@ +{ + "Conditions": { + "ShouldIncludeTag": { + "Fn::Equals": [ 2, 2 ] + } + }, + "Resources": { + "Bucket": { + "Type": "AWS::S3::Bucket", + "Properties": { + "BucketName": "bucket2", + "Tags": [ + { + "Fn::If": [ + "ShouldIncludeTag", + { + "Key": "TagName", + "Value": "TagValue" + }, + { + "Ref": "AWS::NoValue" + } + ] + } + ] + } + } + } +} \ No newline at end of file diff --git a/packages/aws-cdk-lib/cloudformation-include/test/valid-templates.test.ts b/packages/aws-cdk-lib/cloudformation-include/test/valid-templates.test.ts index 70a26179415d0..3530283e24834 100644 --- a/packages/aws-cdk-lib/cloudformation-include/test/valid-templates.test.ts +++ b/packages/aws-cdk-lib/cloudformation-include/test/valid-templates.test.ts @@ -1121,6 +1121,14 @@ describe('CDK Include', () => { loadTestFileToJsObject('fn-select-with-novalue.json'), ); }); + + test('Fn::If can be used in Tags', () => { + includeTestTemplate(stack, 'tags-with-fn-if.json'); + + Template.fromStack(stack).templateMatches( + loadTestFileToJsObject('tags-with-fn-if.json'), + ); + }); }); interface IncludeTestTemplateProps { diff --git a/packages/aws-cdk-lib/core/lib/helpers-internal/cfn-parse.ts b/packages/aws-cdk-lib/core/lib/helpers-internal/cfn-parse.ts index 2dce8dad956e9..fbbb26c0c566e 100644 --- a/packages/aws-cdk-lib/core/lib/helpers-internal/cfn-parse.ts +++ b/packages/aws-cdk-lib/core/lib/helpers-internal/cfn-parse.ts @@ -216,7 +216,8 @@ export class FromCloudFormation { }; } - public static getCfnTag(tag: any): FromCloudFormationResult { + public static getCfnTag(tag: any): FromCloudFormationResult { + if (isResolvableObject(tag)) { return new FromCloudFormationResult(tag); } return tag == null ? new FromCloudFormationResult({ } as any) // break the type system - this should be detected at runtime by a tag validator : new FromCloudFormationResult({ diff --git a/packages/aws-cdk-lib/core/lib/runtime.ts b/packages/aws-cdk-lib/core/lib/runtime.ts index 2da488d6805c2..11c067d319cd3 100644 --- a/packages/aws-cdk-lib/core/lib/runtime.ts +++ b/packages/aws-cdk-lib/core/lib/runtime.ts @@ -49,6 +49,7 @@ function pad(x: number) { * Turn a tag object into the proper CloudFormation representation */ export function cfnTagToCloudFormation(x: any): any { + if (!canInspect(x)) { return x; } return { Key: x.key, Value: x.value, From 2e8675013a6b5d1b0c753777f82cf37b84f36b88 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Tue, 11 Jun 2024 08:26:24 -0700 Subject: [PATCH 2/6] test --- ...efaultTestDeployAssert069A8F1A.assets.json | 19 ++ ...aultTestDeployAssert069A8F1A.template.json | 36 ++++ .../Stack.assets.json | 19 ++ .../Stack.template.json | 66 ++++++ .../cdk.out | 1 + .../integ.json | 12 ++ .../manifest.json | 125 ++++++++++++ .../tree.json | 189 ++++++++++++++++++ .../integ.resource-tags-wtih-intrinsics.ts | 15 ++ .../test/test-templates/tags-with-fn-if.json | 29 +++ 10 files changed, 511 insertions(+) create mode 100644 packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/ResourceTagIntrinsicStackDefaultTestDeployAssert069A8F1A.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/ResourceTagIntrinsicStackDefaultTestDeployAssert069A8F1A.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/Stack.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/Stack.template.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/cdk.out create mode 100644 packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/integ.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/manifest.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/tree.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.ts create mode 100644 packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/test-templates/tags-with-fn-if.json diff --git a/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/ResourceTagIntrinsicStackDefaultTestDeployAssert069A8F1A.assets.json b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/ResourceTagIntrinsicStackDefaultTestDeployAssert069A8F1A.assets.json new file mode 100644 index 0000000000000..ee152a8b62fae --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/ResourceTagIntrinsicStackDefaultTestDeployAssert069A8F1A.assets.json @@ -0,0 +1,19 @@ +{ + "version": "36.0.0", + "files": { + "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { + "source": { + "path": "ResourceTagIntrinsicStackDefaultTestDeployAssert069A8F1A.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/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/ResourceTagIntrinsicStackDefaultTestDeployAssert069A8F1A.template.json b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/ResourceTagIntrinsicStackDefaultTestDeployAssert069A8F1A.template.json new file mode 100644 index 0000000000000..ad9d0fb73d1dd --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/ResourceTagIntrinsicStackDefaultTestDeployAssert069A8F1A.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/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/Stack.assets.json b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/Stack.assets.json new file mode 100644 index 0000000000000..649379c65e6da --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/Stack.assets.json @@ -0,0 +1,19 @@ +{ + "version": "36.0.0", + "files": { + "b533462d7b7ac7cdc460e96a0e3f7e7b589268c4549ee25ad4cab16f3be4c9c1": { + "source": { + "path": "Stack.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "b533462d7b7ac7cdc460e96a0e3f7e7b589268c4549ee25ad4cab16f3be4c9c1.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/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/Stack.template.json b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/Stack.template.json new file mode 100644 index 0000000000000..b108369ba7bde --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/Stack.template.json @@ -0,0 +1,66 @@ +{ + "Conditions": { + "ShouldIncludeTag": { + "Fn::Equals": [ + 2, + 2 + ] + } + }, + "Resources": { + "Bucket": { + "Type": "AWS::S3::Bucket", + "Properties": { + "BucketName": "cdk-integ-cfn-include-bucket2", + "Tags": [ + { + "Fn::If": [ + "ShouldIncludeTag", + { + "Key": "TagName", + "Value": "TagValue" + }, + { + "Ref": "AWS::NoValue" + } + ] + } + ] + } + } + }, + "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/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/cdk.out b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/cdk.out new file mode 100644 index 0000000000000..1f0068d32659a --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.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/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/integ.json b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/integ.json new file mode 100644 index 0000000000000..d6e49c7d11c4b --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/integ.json @@ -0,0 +1,12 @@ +{ + "version": "36.0.0", + "testCases": { + "ResourceTagIntrinsicStack/DefaultTest": { + "stacks": [ + "Stack" + ], + "assertionStack": "ResourceTagIntrinsicStack/DefaultTest/DeployAssert", + "assertionStackName": "ResourceTagIntrinsicStackDefaultTestDeployAssert069A8F1A" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/manifest.json new file mode 100644 index 0000000000000..7be7ae9b56762 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/manifest.json @@ -0,0 +1,125 @@ +{ + "version": "36.0.0", + "artifacts": { + "Stack.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "Stack.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "Stack": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "Stack.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}/b533462d7b7ac7cdc460e96a0e3f7e7b589268c4549ee25ad4cab16f3be4c9c1.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "Stack.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": [ + "Stack.assets" + ], + "metadata": { + "/Stack/Stack": [ + { + "type": "aws:cdk:logicalId", + "data": "Stack" + } + ], + "/Stack/Stack/$Conditions/ShouldIncludeTag": [ + { + "type": "aws:cdk:logicalId", + "data": "ShouldIncludeTag" + } + ], + "/Stack/Stack/Bucket": [ + { + "type": "aws:cdk:logicalId", + "data": "Bucket" + } + ], + "/Stack/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/Stack/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "Stack" + }, + "ResourceTagIntrinsicStackDefaultTestDeployAssert069A8F1A.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "ResourceTagIntrinsicStackDefaultTestDeployAssert069A8F1A.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "ResourceTagIntrinsicStackDefaultTestDeployAssert069A8F1A": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "ResourceTagIntrinsicStackDefaultTestDeployAssert069A8F1A.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": [ + "ResourceTagIntrinsicStackDefaultTestDeployAssert069A8F1A.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": [ + "ResourceTagIntrinsicStackDefaultTestDeployAssert069A8F1A.assets" + ], + "metadata": { + "/ResourceTagIntrinsicStack/DefaultTest/DeployAssert/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/ResourceTagIntrinsicStack/DefaultTest/DeployAssert/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "ResourceTagIntrinsicStack/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/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/tree.json new file mode 100644 index 0000000000000..1b0b0c38be93c --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/tree.json @@ -0,0 +1,189 @@ +{ + "version": "tree-0.1", + "tree": { + "id": "App", + "path": "", + "children": { + "Stack": { + "id": "Stack", + "path": "Stack", + "children": { + "Stack": { + "id": "Stack", + "path": "Stack/Stack", + "children": { + "$Mappings": { + "id": "$Mappings", + "path": "Stack/Stack/$Mappings", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "$Conditions": { + "id": "$Conditions", + "path": "Stack/Stack/$Conditions", + "children": { + "ShouldIncludeTag": { + "id": "ShouldIncludeTag", + "path": "Stack/Stack/$Conditions/ShouldIncludeTag", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnCondition", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "$Rules": { + "id": "$Rules", + "path": "Stack/Stack/$Rules", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "Bucket": { + "id": "Bucket", + "path": "Stack/Stack/Bucket", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::S3::Bucket", + "aws:cdk:cloudformation:props": { + "bucketName": "cdk-integ-cfn-include-bucket2", + "tags": [ + { + "Fn::If": [ + "ShouldIncludeTag", + { + "Key": "TagName", + "Value": "TagValue" + }, + { + "Ref": "AWS::NoValue" + } + ] + } + ] + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_s3.CfnBucket", + "version": "0.0.0" + } + }, + "$Hooks": { + "id": "$Hooks", + "path": "Stack/Stack/$Hooks", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "$Outputs": { + "id": "$Outputs", + "path": "Stack/Stack/$Outputs", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.cloudformation_include.CfnInclude", + "version": "0.0.0" + } + }, + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "Stack/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "Stack/CheckBootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + }, + "ResourceTagIntrinsicStack": { + "id": "ResourceTagIntrinsicStack", + "path": "ResourceTagIntrinsicStack", + "children": { + "DefaultTest": { + "id": "DefaultTest", + "path": "ResourceTagIntrinsicStack/DefaultTest", + "children": { + "Default": { + "id": "Default", + "path": "ResourceTagIntrinsicStack/DefaultTest/Default", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, + "DeployAssert": { + "id": "DeployAssert", + "path": "ResourceTagIntrinsicStack/DefaultTest/DeployAssert", + "children": { + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "ResourceTagIntrinsicStack/DefaultTest/DeployAssert/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "ResourceTagIntrinsicStack/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/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.ts b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.ts new file mode 100644 index 0000000000000..f4c4778be3810 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.ts @@ -0,0 +1,15 @@ +import * as core from 'aws-cdk-lib'; +import * as inc from 'aws-cdk-lib/cloudformation-include'; +import * as integ from '@aws-cdk/integ-tests-alpha'; + +const app = new core.App(); + +const stack = new core.Stack(app, 'Stack'); + +new inc.CfnInclude(stack, 'Stack', { + templateFile: 'test-templates/tags-with-fn-if.json', +}); + +new integ.IntegTest(app, 'ResourceTagIntrinsicStack', { + testCases: [stack], +}); diff --git a/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/test-templates/tags-with-fn-if.json b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/test-templates/tags-with-fn-if.json new file mode 100644 index 0000000000000..433ac23f06e6e --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/test-templates/tags-with-fn-if.json @@ -0,0 +1,29 @@ +{ + "Conditions": { + "ShouldIncludeTag": { + "Fn::Equals": [ 2, 2 ] + } + }, + "Resources": { + "Bucket": { + "Type": "AWS::S3::Bucket", + "Properties": { + "BucketName": "cdk-integ-cfn-include-bucket2", + "Tags": [ + { + "Fn::If": [ + "ShouldIncludeTag", + { + "Key": "TagName", + "Value": "TagValue" + }, + { + "Ref": "AWS::NoValue" + } + ] + } + ] + } + } + } +} \ No newline at end of file From 5f78ef98c56bb5f2f80e5180ecc1d2a42c883a4a Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Tue, 11 Jun 2024 14:59:15 -0700 Subject: [PATCH 3/6] snaps --- .../Stack.assets.json | 4 +- .../Stack.template.json | 27 ++++++--- .../manifest.json | 8 ++- .../tree.json | 57 ++++++++++++------- .../integ.resource-tags-wtih-intrinsics.ts | 2 +- ...h-fn-if.json => tags-with-intrinsics.json} | 17 +++++- ...h-fn-if.json => tags-with-intrinsics.json} | 19 ++++++- .../test/valid-templates.test.ts | 15 ++++- 8 files changed, 109 insertions(+), 40 deletions(-) rename packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/test-templates/{tags-with-fn-if.json => tags-with-intrinsics.json} (58%) rename packages/aws-cdk-lib/cloudformation-include/test/test-templates/{tags-with-fn-if.json => tags-with-intrinsics.json} (52%) diff --git a/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/Stack.assets.json b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/Stack.assets.json index 649379c65e6da..c4ebf26bf97cd 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/Stack.assets.json +++ b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/Stack.assets.json @@ -1,7 +1,7 @@ { "version": "36.0.0", "files": { - "b533462d7b7ac7cdc460e96a0e3f7e7b589268c4549ee25ad4cab16f3be4c9c1": { + "65183510cc1fe133ea25e9f8b474aa5adca1ac83c5e18072e74a62c2bb570cc2": { "source": { "path": "Stack.template.json", "packaging": "file" @@ -9,7 +9,7 @@ "destinations": { "current_account-current_region": { "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", - "objectKey": "b533462d7b7ac7cdc460e96a0e3f7e7b589268c4549ee25ad4cab16f3be4c9c1.json", + "objectKey": "65183510cc1fe133ea25e9f8b474aa5adca1ac83c5e18072e74a62c2bb570cc2.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/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/Stack.template.json b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/Stack.template.json index b108369ba7bde..fee4da18ca069 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/Stack.template.json +++ b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/Stack.template.json @@ -1,4 +1,15 @@ { + "Parameters": { + "Param": { + "Type": "CommaDelimitedList", + "Default": "key,value" + }, + "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]" + } + }, "Conditions": { "ShouldIncludeTag": { "Fn::Equals": [ @@ -17,7 +28,14 @@ "Fn::If": [ "ShouldIncludeTag", { - "Key": "TagName", + "Key": { + "Fn::Select": [ + 0, + { + "Ref": "Param" + } + ] + }, "Value": "TagValue" }, { @@ -29,13 +47,6 @@ } } }, - "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": [ diff --git a/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/manifest.json index 7be7ae9b56762..e640cbfd06fcc 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/manifest.json +++ b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.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}/b533462d7b7ac7cdc460e96a0e3f7e7b589268c4549ee25ad4cab16f3be4c9c1.json", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/65183510cc1fe133ea25e9f8b474aa5adca1ac83c5e18072e74a62c2bb570cc2.json", "requiresBootstrapStackVersion": 6, "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", "additionalDependencies": [ @@ -40,6 +40,12 @@ "data": "Stack" } ], + "/Stack/Stack/Param": [ + { + "type": "aws:cdk:logicalId", + "data": "Param" + } + ], "/Stack/Stack/$Conditions/ShouldIncludeTag": [ { "type": "aws:cdk:logicalId", diff --git a/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/tree.json index 1b0b0c38be93c..7b8ecf8423488 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/tree.json +++ b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.js.snapshot/tree.json @@ -20,6 +20,14 @@ "version": "10.3.0" } }, + "Param": { + "id": "Param", + "path": "Stack/Stack/Param", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.3.0" + } + }, "$Conditions": { "id": "$Conditions", "path": "Stack/Stack/$Conditions", @@ -28,8 +36,8 @@ "id": "ShouldIncludeTag", "path": "Stack/Stack/$Conditions/ShouldIncludeTag", "constructInfo": { - "fqn": "aws-cdk-lib.CfnCondition", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } } }, @@ -58,7 +66,14 @@ "Fn::If": [ "ShouldIncludeTag", { - "Key": "TagName", + "Key": { + "Fn::Select": [ + 0, + { + "Ref": "Param" + } + ] + }, "Value": "TagValue" }, { @@ -70,8 +85,8 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.aws_s3.CfnBucket", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } }, "$Hooks": { @@ -92,30 +107,30 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.cloudformation_include.CfnInclude", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } }, "BootstrapVersion": { "id": "BootstrapVersion", "path": "Stack/BootstrapVersion", "constructInfo": { - "fqn": "aws-cdk-lib.CfnParameter", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } }, "CheckBootstrapVersion": { "id": "CheckBootstrapVersion", "path": "Stack/CheckBootstrapVersion", "constructInfo": { - "fqn": "aws-cdk-lib.CfnRule", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.Stack", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } }, "ResourceTagIntrinsicStack": { @@ -142,22 +157,22 @@ "id": "BootstrapVersion", "path": "ResourceTagIntrinsicStack/DefaultTest/DeployAssert/BootstrapVersion", "constructInfo": { - "fqn": "aws-cdk-lib.CfnParameter", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } }, "CheckBootstrapVersion": { "id": "CheckBootstrapVersion", "path": "ResourceTagIntrinsicStack/DefaultTest/DeployAssert/CheckBootstrapVersion", "constructInfo": { - "fqn": "aws-cdk-lib.CfnRule", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } } }, "constructInfo": { - "fqn": "aws-cdk-lib.Stack", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } } }, @@ -182,8 +197,8 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.App", - "version": "0.0.0" + "fqn": "constructs.Construct", + "version": "10.3.0" } } } \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.ts b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.ts index f4c4778be3810..62e697ad5d0d4 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.ts +++ b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/integ.resource-tags-wtih-intrinsics.ts @@ -7,7 +7,7 @@ const app = new core.App(); const stack = new core.Stack(app, 'Stack'); new inc.CfnInclude(stack, 'Stack', { - templateFile: 'test-templates/tags-with-fn-if.json', + templateFile: 'test-templates/tags-with-intrinsics.json', }); new integ.IntegTest(app, 'ResourceTagIntrinsicStack', { diff --git a/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/test-templates/tags-with-fn-if.json b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/test-templates/tags-with-intrinsics.json similarity index 58% rename from packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/test-templates/tags-with-fn-if.json rename to packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/test-templates/tags-with-intrinsics.json index 433ac23f06e6e..886560576e751 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/test-templates/tags-with-fn-if.json +++ b/packages/@aws-cdk-testing/framework-integ/test/cloudformation-include/test/test-templates/tags-with-intrinsics.json @@ -1,7 +1,13 @@ { + "Parameters": { + "Param": { + "Type": "CommaDelimitedList", + "Default": "key,value" + } + }, "Conditions": { "ShouldIncludeTag": { - "Fn::Equals": [ 2, 2 ] + "Fn::Equals": [2, 2] } }, "Resources": { @@ -14,7 +20,14 @@ "Fn::If": [ "ShouldIncludeTag", { - "Key": "TagName", + "Key": { + "Fn::Select": [ + 0, + { + "Ref": "Param" + } + ] + }, "Value": "TagValue" }, { diff --git a/packages/aws-cdk-lib/cloudformation-include/test/test-templates/tags-with-fn-if.json b/packages/aws-cdk-lib/cloudformation-include/test/test-templates/tags-with-intrinsics.json similarity index 52% rename from packages/aws-cdk-lib/cloudformation-include/test/test-templates/tags-with-fn-if.json rename to packages/aws-cdk-lib/cloudformation-include/test/test-templates/tags-with-intrinsics.json index 8dd5cf4b3ed00..886560576e751 100644 --- a/packages/aws-cdk-lib/cloudformation-include/test/test-templates/tags-with-fn-if.json +++ b/packages/aws-cdk-lib/cloudformation-include/test/test-templates/tags-with-intrinsics.json @@ -1,20 +1,33 @@ { + "Parameters": { + "Param": { + "Type": "CommaDelimitedList", + "Default": "key,value" + } + }, "Conditions": { "ShouldIncludeTag": { - "Fn::Equals": [ 2, 2 ] + "Fn::Equals": [2, 2] } }, "Resources": { "Bucket": { "Type": "AWS::S3::Bucket", "Properties": { - "BucketName": "bucket2", + "BucketName": "cdk-integ-cfn-include-bucket2", "Tags": [ { "Fn::If": [ "ShouldIncludeTag", { - "Key": "TagName", + "Key": { + "Fn::Select": [ + 0, + { + "Ref": "Param" + } + ] + }, "Value": "TagValue" }, { diff --git a/packages/aws-cdk-lib/cloudformation-include/test/valid-templates.test.ts b/packages/aws-cdk-lib/cloudformation-include/test/valid-templates.test.ts index 3530283e24834..7fcb3bf0fee0e 100644 --- a/packages/aws-cdk-lib/cloudformation-include/test/valid-templates.test.ts +++ b/packages/aws-cdk-lib/cloudformation-include/test/valid-templates.test.ts @@ -1123,10 +1123,21 @@ describe('CDK Include', () => { }); test('Fn::If can be used in Tags', () => { - includeTestTemplate(stack, 'tags-with-fn-if.json'); + includeTestTemplate(stack, 'tags-with-intrinsics.json'); Template.fromStack(stack).templateMatches( - loadTestFileToJsObject('tags-with-fn-if.json'), + loadTestFileToJsObject('tags-with-intrinsics.json'), + /*Resources: { + "Bucket": { + "Type": "AWS::S3::Bucket", + "Properties": { + "BucketName": "cdk-integ-cfn-include-bucket2", + "Tags": [ + ], + }, + }, + }, + */ ); }); }); From 53ac93d603fb47364fd885a28e537329c52f4ea6 Mon Sep 17 00:00:00 2001 From: Calvin Combs Date: Wed, 12 Jun 2024 08:46:44 -0700 Subject: [PATCH 4/6] woowoo --- .../test/valid-templates.test.ts | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/packages/aws-cdk-lib/cloudformation-include/test/valid-templates.test.ts b/packages/aws-cdk-lib/cloudformation-include/test/valid-templates.test.ts index 7fcb3bf0fee0e..1082df241f89f 100644 --- a/packages/aws-cdk-lib/cloudformation-include/test/valid-templates.test.ts +++ b/packages/aws-cdk-lib/cloudformation-include/test/valid-templates.test.ts @@ -1127,17 +1127,6 @@ describe('CDK Include', () => { Template.fromStack(stack).templateMatches( loadTestFileToJsObject('tags-with-intrinsics.json'), - /*Resources: { - "Bucket": { - "Type": "AWS::S3::Bucket", - "Properties": { - "BucketName": "cdk-integ-cfn-include-bucket2", - "Tags": [ - ], - }, - }, - }, - */ ); }); }); From ae3b9b1ae517fd27da2f2bd4528f71dc97211af3 Mon Sep 17 00:00:00 2001 From: mikewrighton Date: Mon, 9 Sep 2024 14:41:57 -0400 Subject: [PATCH 5/6] Adding a test to cover invalid intrinsics. --- .../test/test-templates/tags-with-intrinsics.json | 4 ++++ .../cloudformation-include/test/valid-templates.test.ts | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/packages/aws-cdk-lib/cloudformation-include/test/test-templates/tags-with-intrinsics.json b/packages/aws-cdk-lib/cloudformation-include/test/test-templates/tags-with-intrinsics.json index 886560576e751..3ba70c35443de 100644 --- a/packages/aws-cdk-lib/cloudformation-include/test/test-templates/tags-with-intrinsics.json +++ b/packages/aws-cdk-lib/cloudformation-include/test/test-templates/tags-with-intrinsics.json @@ -16,6 +16,10 @@ "Properties": { "BucketName": "cdk-integ-cfn-include-bucket2", "Tags": [ + { + "Key": "Key1", + "Value": "Value1" + }, { "Fn::If": [ "ShouldIncludeTag", diff --git a/packages/aws-cdk-lib/cloudformation-include/test/valid-templates.test.ts b/packages/aws-cdk-lib/cloudformation-include/test/valid-templates.test.ts index 1082df241f89f..8876e5c3fe50a 100644 --- a/packages/aws-cdk-lib/cloudformation-include/test/valid-templates.test.ts +++ b/packages/aws-cdk-lib/cloudformation-include/test/valid-templates.test.ts @@ -1129,6 +1129,12 @@ describe('CDK Include', () => { loadTestFileToJsObject('tags-with-intrinsics.json'), ); }); + + test('throws an exception if Tags contains invalid intrinsics', () => { + expect(() => { + includeTestTemplate(stack, 'tags-with-invalid-intrinsics.json'); + }).toThrow(/expression does not exist in the template/); + }); }); interface IncludeTestTemplateProps { From cab830073fef106d6ee9dcbaf15b7090de1e7858 Mon Sep 17 00:00:00 2001 From: mikewrighton Date: Mon, 9 Sep 2024 15:05:22 -0400 Subject: [PATCH 6/6] Add missing file. --- .../tags-with-invalid-intrinsics.json | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 packages/aws-cdk-lib/cloudformation-include/test/test-templates/tags-with-invalid-intrinsics.json diff --git a/packages/aws-cdk-lib/cloudformation-include/test/test-templates/tags-with-invalid-intrinsics.json b/packages/aws-cdk-lib/cloudformation-include/test/test-templates/tags-with-invalid-intrinsics.json new file mode 100644 index 0000000000000..81c8368f88521 --- /dev/null +++ b/packages/aws-cdk-lib/cloudformation-include/test/test-templates/tags-with-invalid-intrinsics.json @@ -0,0 +1,46 @@ +{ + "Parameters": { + "Param": { + "Type": "CommaDelimitedList", + "Default": "key,value" + } + }, + "Conditions": { + "ShouldIncludeTag": { + "Fn::Equals": [2, 2] + } + }, + "Resources": { + "Bucket": { + "Type": "AWS::S3::Bucket", + "Properties": { + "BucketName": "cdk-integ-cfn-include-bucket2", + "Tags": [ + { + "Key": "Key1", + "Value": "Value1" + }, + { + "Fn::If": [ + "NonExistentCondition", + { + "Key": { + "Fn::Select": [ + 0, + { + "Ref": "Param" + } + ] + }, + "Value": "TagValue" + }, + { + "Ref": "AWS::NoValue" + } + ] + } + ] + } + } + } +} \ No newline at end of file