From 6f0c9c030a90e58399dfb2dfd404fa83d98dc0c3 Mon Sep 17 00:00:00 2001 From: Tietew Date: Fri, 9 Jun 2023 20:01:09 +0900 Subject: [PATCH 1/4] handle tokens on ttls --- .../aws-cloudfront/lib/cache-policy.ts | 13 ++++++++++--- .../aws-cloudfront/test/cache-policy.test.ts | 19 ++++++++++++++++++- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/packages/aws-cdk-lib/aws-cloudfront/lib/cache-policy.ts b/packages/aws-cdk-lib/aws-cloudfront/lib/cache-policy.ts index ffe335531ea7e..d60c486ae6952 100644 --- a/packages/aws-cdk-lib/aws-cloudfront/lib/cache-policy.ts +++ b/packages/aws-cdk-lib/aws-cloudfront/lib/cache-policy.ts @@ -1,6 +1,6 @@ import { Construct } from 'constructs'; import { CfnCachePolicy } from './cloudfront.generated'; -import { Duration, Names, Resource, Stack, Token } from '../../core'; +import { Duration, Names, Resource, Stack, Token, withResolved } from '../../core'; /** * Represents a Cache Policy @@ -140,8 +140,15 @@ export class CachePolicy extends Resource implements ICachePolicy { } const minTtl = (props.minTtl ?? Duration.seconds(0)).toSeconds(); - const defaultTtl = Math.max((props.defaultTtl ?? Duration.days(1)).toSeconds(), minTtl); - const maxTtl = Math.max((props.maxTtl ?? Duration.days(365)).toSeconds(), defaultTtl); + let defaultTtl = (props.defaultTtl ?? Duration.days(1)).toSeconds(); + let maxTtl = (props.maxTtl ?? Duration.days(365)).toSeconds(); + + withResolved(defaultTtl, minTtl, () => { + defaultTtl = Math.max(defaultTtl, minTtl); + }); + withResolved(maxTtl, defaultTtl, () => { + maxTtl = Math.max(maxTtl, defaultTtl); + }); const resource = new CfnCachePolicy(this, 'Resource', { cachePolicyConfig: { diff --git a/packages/aws-cdk-lib/aws-cloudfront/test/cache-policy.test.ts b/packages/aws-cdk-lib/aws-cloudfront/test/cache-policy.test.ts index 52c469beb44af..45d3e6eee4fe4 100644 --- a/packages/aws-cdk-lib/aws-cloudfront/test/cache-policy.test.ts +++ b/packages/aws-cdk-lib/aws-cloudfront/test/cache-policy.test.ts @@ -1,5 +1,5 @@ import { Template } from '../../assertions'; -import { App, Aws, Duration, Stack } from '../../core'; +import { App, Aws, Duration, Lazy, Stack } from '../../core'; import { CachePolicy, CacheCookieBehavior, CacheHeaderBehavior, CacheQueryStringBehavior } from '../lib'; describe('CachePolicy', () => { @@ -149,6 +149,23 @@ describe('CachePolicy', () => { }, }); }); + + test('accepts tokens', () => { + new CachePolicy(stack, 'CachePolicy', { + cachePolicyName: 'MyPolicy', + minTtl: Duration.seconds(Lazy.number({ produce: () => 30 })), + defaultTtl: Duration.seconds(Lazy.number({ produce: () => 20 })), + maxTtl: Duration.seconds(Lazy.number({ produce: () => 10 })), + }); + + Template.fromStack(stack).hasResourceProperties('AWS::CloudFront::CachePolicy', { + CachePolicyConfig: { + MinTTL: 30, + DefaultTTL: 20, + MaxTTL: 10, + }, + }); + }); }); }); From 85c60d391786253722e2298ace1200dc84f8ed3d Mon Sep 17 00:00:00 2001 From: Tietew Date: Fri, 9 Jun 2023 21:38:05 +0900 Subject: [PATCH 2/4] update text --- packages/aws-cdk-lib/aws-cloudfront/test/cache-policy.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/aws-cdk-lib/aws-cloudfront/test/cache-policy.test.ts b/packages/aws-cdk-lib/aws-cloudfront/test/cache-policy.test.ts index 45d3e6eee4fe4..b5ee22135141e 100644 --- a/packages/aws-cdk-lib/aws-cloudfront/test/cache-policy.test.ts +++ b/packages/aws-cdk-lib/aws-cloudfront/test/cache-policy.test.ts @@ -150,7 +150,7 @@ describe('CachePolicy', () => { }); }); - test('accepts tokens', () => { + test('sorting TTLs is not performed when using Tokens', () => { new CachePolicy(stack, 'CachePolicy', { cachePolicyName: 'MyPolicy', minTtl: Duration.seconds(Lazy.number({ produce: () => 30 })), From 35fea6472c746e0f94582f1029e03fe645a8b99b Mon Sep 17 00:00:00 2001 From: Tietew Date: Mon, 12 Jun 2023 20:15:05 +0900 Subject: [PATCH 3/4] add integ test --- ...efaultTestDeployAssert1B8724D9.assets.json | 19 ++ ...aultTestDeployAssert1B8724D9.template.json | 36 ++++ .../cdk.out | 2 +- .../integ-distribution-policies.assets.json | 6 +- .../integ-distribution-policies.template.json | 54 +++++- .../integ.json | 12 +- .../manifest.json | 75 +++++++- .../tree.json | 172 ++++++++++++++++-- .../test/integ.distribution-policies.ts | 25 ++- 9 files changed, 368 insertions(+), 33 deletions(-) create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/DistributionPoliciesDefaultTestDeployAssert1B8724D9.assets.json create mode 100644 packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/DistributionPoliciesDefaultTestDeployAssert1B8724D9.template.json diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/DistributionPoliciesDefaultTestDeployAssert1B8724D9.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/DistributionPoliciesDefaultTestDeployAssert1B8724D9.assets.json new file mode 100644 index 0000000000000..e54ad9e2b0a11 --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/DistributionPoliciesDefaultTestDeployAssert1B8724D9.assets.json @@ -0,0 +1,19 @@ +{ + "version": "32.0.0", + "files": { + "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { + "source": { + "path": "DistributionPoliciesDefaultTestDeployAssert1B8724D9.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-cloudfront/test/integ.distribution-policies.js.snapshot/DistributionPoliciesDefaultTestDeployAssert1B8724D9.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/DistributionPoliciesDefaultTestDeployAssert1B8724D9.template.json new file mode 100644 index 0000000000000..ad9d0fb73d1dd --- /dev/null +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/DistributionPoliciesDefaultTestDeployAssert1B8724D9.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-cloudfront/test/integ.distribution-policies.js.snapshot/cdk.out b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/cdk.out index 7925065efbcc4..f0b901e7c06e5 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/cdk.out +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/cdk.out @@ -1 +1 @@ -{"version":"31.0.0"} \ No newline at end of file +{"version":"32.0.0"} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/integ-distribution-policies.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/integ-distribution-policies.assets.json index 65e1abffbd1e4..e44559b37a911 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/integ-distribution-policies.assets.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/integ-distribution-policies.assets.json @@ -1,7 +1,7 @@ { - "version": "31.0.0", + "version": "32.0.0", "files": { - "b775626104dd72b1b3fc9a1fb6e652212a0a0aa05be2d07ce372eaf29589c146": { + "01042f10dd3272da413b201384cdf825a7467030c0db8a2d5bcfe10b45a30ced": { "source": { "path": "integ-distribution-policies.template.json", "packaging": "file" @@ -9,7 +9,7 @@ "destinations": { "current_account-current_region": { "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", - "objectKey": "b775626104dd72b1b3fc9a1fb6e652212a0a0aa05be2d07ce372eaf29589c146.json", + "objectKey": "01042f10dd3272da413b201384cdf825a7467030c0db8a2d5bcfe10b45a30ced.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-cloudfront/test/integ.distribution-policies.js.snapshot/integ-distribution-policies.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/integ-distribution-policies.template.json index 30e206fa0f8b9..ee81b98bb7ab4 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/integ-distribution-policies.template.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/integ-distribution-policies.template.json @@ -24,6 +24,46 @@ } } }, + "CachePolicyWithRefD7C98251": { + "Type": "AWS::CloudFront::CachePolicy", + "Properties": { + "CachePolicyConfig": { + "DefaultTTL": { + "Ref": "DefaultTtlParam" + }, + "MaxTTL": { + "Ref": "MaxTtlParam" + }, + "MinTTL": { + "Ref": "MinTtlParam" + }, + "Name": { + "Fn::Join": [ + "", + [ + "integdistributionpoliciesCachePolicyWithRef80C59B6E-", + { + "Ref": "AWS::Region" + } + ] + ] + }, + "ParametersInCacheKeyAndForwardedToOrigin": { + "CookiesConfig": { + "CookieBehavior": "none" + }, + "EnableAcceptEncodingBrotli": false, + "EnableAcceptEncodingGzip": false, + "HeadersConfig": { + "HeaderBehavior": "none" + }, + "QueryStringsConfig": { + "QueryStringBehavior": "none" + } + } + } + } + }, "OriginRequestPolicy3EFDB4FA": { "Type": "AWS::CloudFront::OriginRequestPolicy", "Properties": { @@ -130,7 +170,7 @@ "DistributionConfig": { "DefaultCacheBehavior": { "CachePolicyId": { - "Ref": "CachePolicy26D8A535" + "Ref": "CachePolicyWithRefD7C98251" }, "Compress": true, "OriginRequestPolicyId": "b689b0a8-53d0-40ab-baf2-68738e2966ac", @@ -157,6 +197,18 @@ } }, "Parameters": { + "MinTtlParam": { + "Type": "Number", + "Default": "1000" + }, + "DefaultTtlParam": { + "Type": "Number", + "Default": "2000" + }, + "MaxTtlParam": { + "Type": "Number", + "Default": "3000" + }, "BootstrapVersion": { "Type": "AWS::SSM::Parameter::Value", "Default": "/cdk-bootstrap/hnb659fds/version", diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/integ.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/integ.json index 0746ca552db50..efc4f920d868f 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/integ.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/integ.json @@ -1,14 +1,12 @@ { - "version": "31.0.0", + "version": "32.0.0", "testCases": { - "integ.distribution-policies": { + "DistributionPolicies/DefaultTest": { "stacks": [ "integ-distribution-policies" ], - "diffAssets": false, - "stackUpdateWorkflow": true + "assertionStack": "DistributionPolicies/DefaultTest/DeployAssert", + "assertionStackName": "DistributionPoliciesDefaultTestDeployAssert1B8724D9" } - }, - "synthContext": {}, - "enableLookups": false + } } \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/manifest.json index 33eaed8bea5bd..757a340fd7db4 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/manifest.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/manifest.json @@ -1,5 +1,5 @@ { - "version": "31.0.0", + "version": "32.0.0", "artifacts": { "integ-distribution-policies.assets": { "type": "cdk:asset-manifest", @@ -17,7 +17,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}/b775626104dd72b1b3fc9a1fb6e652212a0a0aa05be2d07ce372eaf29589c146.json", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/01042f10dd3272da413b201384cdf825a7467030c0db8a2d5bcfe10b45a30ced.json", "requiresBootstrapStackVersion": 6, "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", "additionalDependencies": [ @@ -39,6 +39,30 @@ "data": "CachePolicy26D8A535" } ], + "/integ-distribution-policies/MinTtlParam": [ + { + "type": "aws:cdk:logicalId", + "data": "MinTtlParam" + } + ], + "/integ-distribution-policies/DefaultTtlParam": [ + { + "type": "aws:cdk:logicalId", + "data": "DefaultTtlParam" + } + ], + "/integ-distribution-policies/MaxTtlParam": [ + { + "type": "aws:cdk:logicalId", + "data": "MaxTtlParam" + } + ], + "/integ-distribution-policies/CachePolicyWithRef/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "CachePolicyWithRefD7C98251" + } + ], "/integ-distribution-policies/OriginRequestPolicy/Resource": [ { "type": "aws:cdk:logicalId", @@ -78,6 +102,53 @@ }, "displayName": "integ-distribution-policies" }, + "DistributionPoliciesDefaultTestDeployAssert1B8724D9.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "DistributionPoliciesDefaultTestDeployAssert1B8724D9.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "DistributionPoliciesDefaultTestDeployAssert1B8724D9": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "DistributionPoliciesDefaultTestDeployAssert1B8724D9.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": [ + "DistributionPoliciesDefaultTestDeployAssert1B8724D9.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": [ + "DistributionPoliciesDefaultTestDeployAssert1B8724D9.assets" + ], + "metadata": { + "/DistributionPolicies/DefaultTest/DeployAssert/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/DistributionPolicies/DefaultTest/DeployAssert/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "DistributionPolicies/DefaultTest/DeployAssert" + }, "Tree": { "type": "cdk:tree", "properties": { diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/tree.json index 5372cbb6f77e7..4f286e7bc052b 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/tree.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.js.snapshot/tree.json @@ -40,13 +40,95 @@ } }, "constructInfo": { - "fqn": "@aws-cdk/aws-cloudfront.CfnCachePolicy", + "fqn": "aws-cdk-lib.aws_cloudfront.CfnCachePolicy", "version": "0.0.0" } } }, "constructInfo": { - "fqn": "@aws-cdk/aws-cloudfront.CachePolicy", + "fqn": "aws-cdk-lib.aws_cloudfront.CachePolicy", + "version": "0.0.0" + } + }, + "MinTtlParam": { + "id": "MinTtlParam", + "path": "integ-distribution-policies/MinTtlParam", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "DefaultTtlParam": { + "id": "DefaultTtlParam", + "path": "integ-distribution-policies/DefaultTtlParam", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "MaxTtlParam": { + "id": "MaxTtlParam", + "path": "integ-distribution-policies/MaxTtlParam", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CachePolicyWithRef": { + "id": "CachePolicyWithRef", + "path": "integ-distribution-policies/CachePolicyWithRef", + "children": { + "Resource": { + "id": "Resource", + "path": "integ-distribution-policies/CachePolicyWithRef/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::CloudFront::CachePolicy", + "aws:cdk:cloudformation:props": { + "cachePolicyConfig": { + "name": { + "Fn::Join": [ + "", + [ + "integdistributionpoliciesCachePolicyWithRef80C59B6E-", + { + "Ref": "AWS::Region" + } + ] + ] + }, + "minTtl": { + "Ref": "MinTtlParam" + }, + "maxTtl": { + "Ref": "MaxTtlParam" + }, + "defaultTtl": { + "Ref": "DefaultTtlParam" + }, + "parametersInCacheKeyAndForwardedToOrigin": { + "cookiesConfig": { + "cookieBehavior": "none" + }, + "headersConfig": { + "headerBehavior": "none" + }, + "enableAcceptEncodingGzip": false, + "enableAcceptEncodingBrotli": false, + "queryStringsConfig": { + "queryStringBehavior": "none" + } + } + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_cloudfront.CfnCachePolicy", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_cloudfront.CachePolicy", "version": "0.0.0" } }, @@ -78,13 +160,13 @@ } }, "constructInfo": { - "fqn": "@aws-cdk/aws-cloudfront.CfnOriginRequestPolicy", + "fqn": "aws-cdk-lib.aws_cloudfront.CfnOriginRequestPolicy", "version": "0.0.0" } } }, "constructInfo": { - "fqn": "@aws-cdk/aws-cloudfront.OriginRequestPolicy", + "fqn": "aws-cdk-lib.aws_cloudfront.OriginRequestPolicy", "version": "0.0.0" } }, @@ -143,13 +225,13 @@ } }, "constructInfo": { - "fqn": "@aws-cdk/aws-cloudfront.CfnResponseHeadersPolicy", + "fqn": "aws-cdk-lib.aws_cloudfront.CfnResponseHeadersPolicy", "version": "0.0.0" } } }, "constructInfo": { - "fqn": "@aws-cdk/aws-cloudfront.ResponseHeadersPolicy", + "fqn": "aws-cdk-lib.aws_cloudfront.ResponseHeadersPolicy", "version": "0.0.0" } }, @@ -162,7 +244,7 @@ "path": "integ-distribution-policies/Dist/Origin1", "constructInfo": { "fqn": "constructs.Construct", - "version": "10.1.270" + "version": "10.2.26" } }, "Resource": { @@ -203,13 +285,13 @@ } }, "constructInfo": { - "fqn": "@aws-cdk/aws-cloudfront.CfnDistribution", + "fqn": "aws-cdk-lib.aws_cloudfront.CfnDistribution", "version": "0.0.0" } } }, "constructInfo": { - "fqn": "@aws-cdk/aws-cloudfront.Distribution", + "fqn": "aws-cdk-lib.aws_cloudfront.Distribution", "version": "0.0.0" } }, @@ -222,7 +304,7 @@ "path": "integ-distribution-policies/Dist-2/Origin1", "constructInfo": { "fqn": "constructs.Construct", - "version": "10.1.270" + "version": "10.2.26" } }, "Resource": { @@ -246,7 +328,7 @@ "pathPattern": "*", "targetOriginId": "integdistributionpoliciesDist2Origin16AFA66C6", "cachePolicyId": { - "Ref": "CachePolicy26D8A535" + "Ref": "CachePolicyWithRefD7C98251" }, "compress": true, "originRequestPolicyId": "b689b0a8-53d0-40ab-baf2-68738e2966ac", @@ -261,13 +343,13 @@ } }, "constructInfo": { - "fqn": "@aws-cdk/aws-cloudfront.CfnDistribution", + "fqn": "aws-cdk-lib.aws_cloudfront.CfnDistribution", "version": "0.0.0" } } }, "constructInfo": { - "fqn": "@aws-cdk/aws-cloudfront.Distribution", + "fqn": "aws-cdk-lib.aws_cloudfront.Distribution", "version": "0.0.0" } }, @@ -275,7 +357,7 @@ "id": "BootstrapVersion", "path": "integ-distribution-policies/BootstrapVersion", "constructInfo": { - "fqn": "@aws-cdk/core.CfnParameter", + "fqn": "aws-cdk-lib.CfnParameter", "version": "0.0.0" } }, @@ -283,13 +365,67 @@ "id": "CheckBootstrapVersion", "path": "integ-distribution-policies/CheckBootstrapVersion", "constructInfo": { - "fqn": "@aws-cdk/core.CfnRule", + "fqn": "aws-cdk-lib.CfnRule", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.Stack", + "version": "0.0.0" + } + }, + "DistributionPolicies": { + "id": "DistributionPolicies", + "path": "DistributionPolicies", + "children": { + "DefaultTest": { + "id": "DefaultTest", + "path": "DistributionPolicies/DefaultTest", + "children": { + "Default": { + "id": "Default", + "path": "DistributionPolicies/DefaultTest/Default", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.2.26" + } + }, + "DeployAssert": { + "id": "DeployAssert", + "path": "DistributionPolicies/DefaultTest/DeployAssert", + "children": { + "BootstrapVersion": { + "id": "BootstrapVersion", + "path": "DistributionPolicies/DefaultTest/DeployAssert/BootstrapVersion", + "constructInfo": { + "fqn": "aws-cdk-lib.CfnParameter", + "version": "0.0.0" + } + }, + "CheckBootstrapVersion": { + "id": "CheckBootstrapVersion", + "path": "DistributionPolicies/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/core.Stack", + "fqn": "@aws-cdk/integ-tests-alpha.IntegTest", "version": "0.0.0" } }, @@ -298,12 +434,12 @@ "path": "Tree", "constructInfo": { "fqn": "constructs.Construct", - "version": "10.1.270" + "version": "10.2.26" } } }, "constructInfo": { - "fqn": "@aws-cdk/core.App", + "fqn": "aws-cdk-lib.App", "version": "0.0.0" } } diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.ts index acb94f5e2a56b..3ee022863c1bd 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.ts +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-policies.ts @@ -1,4 +1,5 @@ import * as cdk from 'aws-cdk-lib'; +import { IntegTest } from '@aws-cdk/integ-tests-alpha'; import { TestOrigin } from './test-origin'; import * as cloudfront from 'aws-cdk-lib/aws-cloudfront'; import { OriginRequestPolicy } from 'aws-cdk-lib/aws-cloudfront'; @@ -10,6 +11,24 @@ const cachePolicy = new cloudfront.CachePolicy(stack, 'CachePolicy', { cachePolicyName: 'ACustomCachePolicy', }); +const paramMinTtl = new cdk.CfnParameter(stack, 'MinTtlParam', { + type: 'Number', + default: '1000', +}); +const paramDefaultTtl = new cdk.CfnParameter(stack, 'DefaultTtlParam', { + type: 'Number', + default: '2000', +}); +const paramMaxTtl = new cdk.CfnParameter(stack, 'MaxTtlParam', { + type: 'Number', + default: '3000', +}); +const cachePolicyWithRef = new cloudfront.CachePolicy(stack, 'CachePolicyWithRef', { + minTtl: cdk.Duration.seconds(paramMinTtl.valueAsNumber), + defaultTtl: cdk.Duration.seconds(paramDefaultTtl.valueAsNumber), + maxTtl: cdk.Duration.seconds(paramMaxTtl.valueAsNumber), +}); + const originRequestPolicy = new cloudfront.OriginRequestPolicy(stack, 'OriginRequestPolicy', { originRequestPolicyName: 'ACustomOriginRequestPolicy', headerBehavior: cloudfront.OriginRequestHeaderBehavior.all('CloudFront-Forwarded-Proto'), @@ -42,10 +61,14 @@ new cloudfront.Distribution(stack, 'Dist', { new cloudfront.Distribution(stack, 'Dist-2', { defaultBehavior: { origin: new TestOrigin('www.example-2.com'), - cachePolicy, + cachePolicy: cachePolicyWithRef, originRequestPolicy: OriginRequestPolicy.ALL_VIEWER_EXCEPT_HOST_HEADER, responseHeadersPolicy, }, }); +new IntegTest(app, 'DistributionPolicies', { + testCases: [stack], +}); + app.synth(); From bf88ec51ee4ddc915bf5f88906a3959eb2a8a03f Mon Sep 17 00:00:00 2001 From: Tietew Date: Tue, 13 Jun 2023 11:22:44 +0900 Subject: [PATCH 4/4] add a test for Refs in template --- .../aws-cloudfront/test/cache-policy.test.ts | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/aws-cdk-lib/aws-cloudfront/test/cache-policy.test.ts b/packages/aws-cdk-lib/aws-cloudfront/test/cache-policy.test.ts index b5ee22135141e..bd30650bc0ea5 100644 --- a/packages/aws-cdk-lib/aws-cloudfront/test/cache-policy.test.ts +++ b/packages/aws-cdk-lib/aws-cloudfront/test/cache-policy.test.ts @@ -1,5 +1,6 @@ import { Template } from '../../assertions'; -import { App, Aws, Duration, Lazy, Stack } from '../../core'; +import { StringParameter } from '../../aws-ssm'; +import { App, Aws, Duration, Lazy, Stack, Token } from '../../core'; import { CachePolicy, CacheCookieBehavior, CacheHeaderBehavior, CacheQueryStringBehavior } from '../lib'; describe('CachePolicy', () => { @@ -166,6 +167,23 @@ describe('CachePolicy', () => { }, }); }); + + test('respects Tokens', () => { + new CachePolicy(stack, 'CachePolicy', { + cachePolicyName: 'MyPolicy', + minTtl: Duration.seconds(Token.asNumber(StringParameter.valueForStringParameter(stack, '/Min'))), + defaultTtl: Duration.seconds(Token.asNumber(StringParameter.valueForStringParameter(stack, '/Default'))), + maxTtl: Duration.seconds(Token.asNumber(StringParameter.valueForStringParameter(stack, '/Max'))), + }); + + Template.fromStack(stack).hasResourceProperties('AWS::CloudFront::CachePolicy', { + CachePolicyConfig: { + MinTTL: { Ref: 'SsmParameterValueMinC96584B6F00A464EAD1953AFF4B05118Parameter' }, + DefaultTTL: { Ref: 'SsmParameterValueDefaultC96584B6F00A464EAD1953AFF4B05118Parameter' }, + MaxTTL: { Ref: 'SsmParameterValueMaxC96584B6F00A464EAD1953AFF4B05118Parameter' }, + }, + }); + }); }); });