diff --git a/packages/@aws-cdk/aws-cloudfront/lib/distribution.ts b/packages/@aws-cdk/aws-cloudfront/lib/distribution.ts index 345477524e4e7..026b11f13d50f 100644 --- a/packages/@aws-cdk/aws-cloudfront/lib/distribution.ts +++ b/packages/@aws-cdk/aws-cloudfront/lib/distribution.ts @@ -354,8 +354,9 @@ export class Distribution extends Resource implements IDistribution { } else { const originIndex = this.boundOrigins.length + 1; const scope = new Construct(this, `Origin${originIndex}`); - const originId = Names.uniqueId(scope).slice(-ORIGIN_ID_MAX_LENGTH); - const originBindConfig = origin.bind(scope, { originId }); + const generatedId = Names.uniqueId(scope).slice(-ORIGIN_ID_MAX_LENGTH); + const originBindConfig = origin.bind(scope, { originId: generatedId }); + const originId = originBindConfig.originProperty?.id ?? generatedId; const duplicateId = this.boundOrigins.find(boundOrigin => boundOrigin.originProperty?.id === originBindConfig.originProperty?.id); if (duplicateId) { throw new Error(`Origin with id ${duplicateId.originProperty?.id} already exists. OriginIds must be unique within a distribution`); diff --git a/packages/@aws-cdk/aws-cloudfront/test/distribution.test.ts b/packages/@aws-cdk/aws-cloudfront/test/distribution.test.ts index 8a4c2503234aa..dcf6db0cdfc89 100644 --- a/packages/@aws-cdk/aws-cloudfront/test/distribution.test.ts +++ b/packages/@aws-cdk/aws-cloudfront/test/distribution.test.ts @@ -975,6 +975,60 @@ describe('origin IDs', () => { }); }); +describe('custom origin ids', () => { + test('test that originId param is respected', () => { + const origin = defaultOrigin(undefined, 'custom-origin-id'); + + const distribution = new Distribution(stack, 'Http1Distribution', { + defaultBehavior: { origin }, + additionalBehaviors: { + secondUsage: { + origin, + }, + }, + }); + distribution.addBehavior( + 'thirdUsage', + origin, + ); + + Template.fromStack(stack).hasResourceProperties('AWS::CloudFront::Distribution', { + DistributionConfig: { + DefaultCacheBehavior: { + CachePolicyId: '658327ea-f89d-4fab-a63d-7e88639e58f6', + Compress: true, + TargetOriginId: 'custom-origin-id', + ViewerProtocolPolicy: 'allow-all', + }, + CacheBehaviors: [{ + CachePolicyId: '658327ea-f89d-4fab-a63d-7e88639e58f6', + Compress: true, + PathPattern: 'secondUsage', + TargetOriginId: 'custom-origin-id', + ViewerProtocolPolicy: 'allow-all', + }, + { + CachePolicyId: '658327ea-f89d-4fab-a63d-7e88639e58f6', + Compress: true, + PathPattern: 'thirdUsage', + TargetOriginId: 'custom-origin-id', + ViewerProtocolPolicy: 'allow-all', + }], + Enabled: true, + HttpVersion: 'http2', + IPV6Enabled: true, + Origins: [{ + DomainName: 'www.example.com', + Id: 'custom-origin-id', + CustomOriginConfig: { + OriginProtocolPolicy: 'https-only', + }, + }], + }, + }); + }); +}); + describe('supported HTTP versions', () => { test('setting HTTP/1.1 renders HttpVersion correctly', () => { new Distribution(stack, 'Http1Distribution', { diff --git a/packages/@aws-cdk/aws-cloudfront/test/integ.distribution-origin-id.js.snapshot/DistributionOriginIdDefaultTestDeployAssert16FC3109.assets.json b/packages/@aws-cdk/aws-cloudfront/test/integ.distribution-origin-id.js.snapshot/DistributionOriginIdDefaultTestDeployAssert16FC3109.assets.json new file mode 100644 index 0000000000000..5356956a32622 --- /dev/null +++ b/packages/@aws-cdk/aws-cloudfront/test/integ.distribution-origin-id.js.snapshot/DistributionOriginIdDefaultTestDeployAssert16FC3109.assets.json @@ -0,0 +1,19 @@ +{ + "version": "21.0.0", + "files": { + "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { + "source": { + "path": "DistributionOriginIdDefaultTestDeployAssert16FC3109.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/aws-cloudfront/test/integ.distribution-origin-id.js.snapshot/DistributionOriginIdDefaultTestDeployAssert16FC3109.template.json b/packages/@aws-cdk/aws-cloudfront/test/integ.distribution-origin-id.js.snapshot/DistributionOriginIdDefaultTestDeployAssert16FC3109.template.json new file mode 100644 index 0000000000000..ad9d0fb73d1dd --- /dev/null +++ b/packages/@aws-cdk/aws-cloudfront/test/integ.distribution-origin-id.js.snapshot/DistributionOriginIdDefaultTestDeployAssert16FC3109.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/aws-cloudfront/test/integ.distribution-origin-id.js.snapshot/cdk.out b/packages/@aws-cdk/aws-cloudfront/test/integ.distribution-origin-id.js.snapshot/cdk.out new file mode 100644 index 0000000000000..8ecc185e9dbee --- /dev/null +++ b/packages/@aws-cdk/aws-cloudfront/test/integ.distribution-origin-id.js.snapshot/cdk.out @@ -0,0 +1 @@ +{"version":"21.0.0"} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-cloudfront/test/integ.distribution-origin-id.js.snapshot/integ-distribution-origin-id.assets.json b/packages/@aws-cdk/aws-cloudfront/test/integ.distribution-origin-id.js.snapshot/integ-distribution-origin-id.assets.json new file mode 100644 index 0000000000000..0231674b87b71 --- /dev/null +++ b/packages/@aws-cdk/aws-cloudfront/test/integ.distribution-origin-id.js.snapshot/integ-distribution-origin-id.assets.json @@ -0,0 +1,19 @@ +{ + "version": "21.0.0", + "files": { + "413425a9c32869faf78d5031f15a0e8f5eed244599a8c576f6f4c025d23505e6": { + "source": { + "path": "integ-distribution-origin-id.template.json", + "packaging": "file" + }, + "destinations": { + "current_account-current_region": { + "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", + "objectKey": "413425a9c32869faf78d5031f15a0e8f5eed244599a8c576f6f4c025d23505e6.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/aws-cloudfront/test/integ.distribution-origin-id.js.snapshot/integ-distribution-origin-id.template.json b/packages/@aws-cdk/aws-cloudfront/test/integ.distribution-origin-id.js.snapshot/integ-distribution-origin-id.template.json new file mode 100644 index 0000000000000..63b7420b0e21e --- /dev/null +++ b/packages/@aws-cdk/aws-cloudfront/test/integ.distribution-origin-id.js.snapshot/integ-distribution-origin-id.template.json @@ -0,0 +1,79 @@ +{ + "Resources": { + "TestDistribution94EC811C": { + "Type": "AWS::CloudFront::Distribution", + "Properties": { + "DistributionConfig": { + "CacheBehaviors": [ + { + "CachePolicyId": "658327ea-f89d-4fab-a63d-7e88639e58f6", + "Compress": true, + "PathPattern": "/second", + "TargetOriginId": "my-custom-origin-id", + "ViewerProtocolPolicy": "allow-all" + }, + { + "CachePolicyId": "658327ea-f89d-4fab-a63d-7e88639e58f6", + "Compress": true, + "PathPattern": "/third", + "TargetOriginId": "my-custom-origin-id", + "ViewerProtocolPolicy": "allow-all" + } + ], + "DefaultCacheBehavior": { + "CachePolicyId": "658327ea-f89d-4fab-a63d-7e88639e58f6", + "Compress": true, + "TargetOriginId": "my-custom-origin-id", + "ViewerProtocolPolicy": "allow-all" + }, + "Enabled": true, + "HttpVersion": "http2", + "IPV6Enabled": true, + "Origins": [ + { + "CustomOriginConfig": { + "OriginProtocolPolicy": "https-only" + }, + "DomainName": "www.example.com", + "Id": "my-custom-origin-id" + } + ] + } + } + } + }, + "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/aws-cloudfront/test/integ.distribution-origin-id.js.snapshot/integ.json b/packages/@aws-cdk/aws-cloudfront/test/integ.distribution-origin-id.js.snapshot/integ.json new file mode 100644 index 0000000000000..472cc4bd95154 --- /dev/null +++ b/packages/@aws-cdk/aws-cloudfront/test/integ.distribution-origin-id.js.snapshot/integ.json @@ -0,0 +1,12 @@ +{ + "version": "21.0.0", + "testCases": { + "DistributionOriginId/DefaultTest": { + "stacks": [ + "integ-distribution-origin-id" + ], + "assertionStack": "DistributionOriginId/DefaultTest/DeployAssert", + "assertionStackName": "DistributionOriginIdDefaultTestDeployAssert16FC3109" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-cloudfront/test/integ.distribution-origin-id.js.snapshot/manifest.json b/packages/@aws-cdk/aws-cloudfront/test/integ.distribution-origin-id.js.snapshot/manifest.json new file mode 100644 index 0000000000000..c400bae2e0e4b --- /dev/null +++ b/packages/@aws-cdk/aws-cloudfront/test/integ.distribution-origin-id.js.snapshot/manifest.json @@ -0,0 +1,111 @@ +{ + "version": "21.0.0", + "artifacts": { + "Tree": { + "type": "cdk:tree", + "properties": { + "file": "tree.json" + } + }, + "integ-distribution-origin-id.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "integ-distribution-origin-id.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "integ-distribution-origin-id": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "integ-distribution-origin-id.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}/413425a9c32869faf78d5031f15a0e8f5eed244599a8c576f6f4c025d23505e6.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", + "additionalDependencies": [ + "integ-distribution-origin-id.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": [ + "integ-distribution-origin-id.assets" + ], + "metadata": { + "/integ-distribution-origin-id/TestDistribution/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "TestDistribution94EC811C" + } + ], + "/integ-distribution-origin-id/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/integ-distribution-origin-id/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "integ-distribution-origin-id" + }, + "DistributionOriginIdDefaultTestDeployAssert16FC3109.assets": { + "type": "cdk:asset-manifest", + "properties": { + "file": "DistributionOriginIdDefaultTestDeployAssert16FC3109.assets.json", + "requiresBootstrapStackVersion": 6, + "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version" + } + }, + "DistributionOriginIdDefaultTestDeployAssert16FC3109": { + "type": "aws:cloudformation:stack", + "environment": "aws://unknown-account/unknown-region", + "properties": { + "templateFile": "DistributionOriginIdDefaultTestDeployAssert16FC3109.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": [ + "DistributionOriginIdDefaultTestDeployAssert16FC3109.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": [ + "DistributionOriginIdDefaultTestDeployAssert16FC3109.assets" + ], + "metadata": { + "/DistributionOriginId/DefaultTest/DeployAssert/BootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "BootstrapVersion" + } + ], + "/DistributionOriginId/DefaultTest/DeployAssert/CheckBootstrapVersion": [ + { + "type": "aws:cdk:logicalId", + "data": "CheckBootstrapVersion" + } + ] + }, + "displayName": "DistributionOriginId/DefaultTest/DeployAssert" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-cloudfront/test/integ.distribution-origin-id.js.snapshot/tree.json b/packages/@aws-cdk/aws-cloudfront/test/integ.distribution-origin-id.js.snapshot/tree.json new file mode 100644 index 0000000000000..f758d5d3464f3 --- /dev/null +++ b/packages/@aws-cdk/aws-cloudfront/test/integ.distribution-origin-id.js.snapshot/tree.json @@ -0,0 +1,135 @@ +{ + "version": "tree-0.1", + "tree": { + "id": "App", + "path": "", + "children": { + "Tree": { + "id": "Tree", + "path": "Tree", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.1.140" + } + }, + "integ-distribution-origin-id": { + "id": "integ-distribution-origin-id", + "path": "integ-distribution-origin-id", + "children": { + "TestDistribution": { + "id": "TestDistribution", + "path": "integ-distribution-origin-id/TestDistribution", + "children": { + "Origin1": { + "id": "Origin1", + "path": "integ-distribution-origin-id/TestDistribution/Origin1", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.1.140" + } + }, + "Resource": { + "id": "Resource", + "path": "integ-distribution-origin-id/TestDistribution/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::CloudFront::Distribution", + "aws:cdk:cloudformation:props": { + "distributionConfig": { + "enabled": true, + "origins": [ + { + "domainName": "www.example.com", + "id": "my-custom-origin-id", + "customOriginConfig": { + "originProtocolPolicy": "https-only" + } + } + ], + "defaultCacheBehavior": { + "pathPattern": "*", + "targetOriginId": "my-custom-origin-id", + "cachePolicyId": "658327ea-f89d-4fab-a63d-7e88639e58f6", + "compress": true, + "viewerProtocolPolicy": "allow-all" + }, + "cacheBehaviors": [ + { + "pathPattern": "/second", + "targetOriginId": "my-custom-origin-id", + "cachePolicyId": "658327ea-f89d-4fab-a63d-7e88639e58f6", + "compress": true, + "viewerProtocolPolicy": "allow-all" + }, + { + "pathPattern": "/third", + "targetOriginId": "my-custom-origin-id", + "cachePolicyId": "658327ea-f89d-4fab-a63d-7e88639e58f6", + "compress": true, + "viewerProtocolPolicy": "allow-all" + } + ], + "httpVersion": "http2", + "ipv6Enabled": true + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/aws-cloudfront.CfnDistribution", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/aws-cloudfront.Distribution", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/core.Stack", + "version": "0.0.0" + } + }, + "DistributionOriginId": { + "id": "DistributionOriginId", + "path": "DistributionOriginId", + "children": { + "DefaultTest": { + "id": "DefaultTest", + "path": "DistributionOriginId/DefaultTest", + "children": { + "Default": { + "id": "Default", + "path": "DistributionOriginId/DefaultTest/Default", + "constructInfo": { + "fqn": "constructs.Construct", + "version": "10.1.140" + } + }, + "DeployAssert": { + "id": "DeployAssert", + "path": "DistributionOriginId/DefaultTest/DeployAssert", + "constructInfo": { + "fqn": "@aws-cdk/core.Stack", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests.IntegTestCase", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/integ-tests.IntegTest", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "@aws-cdk/core.App", + "version": "0.0.0" + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-cloudfront/test/integ.distribution-origin-id.ts b/packages/@aws-cdk/aws-cloudfront/test/integ.distribution-origin-id.ts new file mode 100644 index 0000000000000..fb131791930c0 --- /dev/null +++ b/packages/@aws-cdk/aws-cloudfront/test/integ.distribution-origin-id.ts @@ -0,0 +1,21 @@ +import * as cdk from '@aws-cdk/core'; +import { IntegTest } from '@aws-cdk/integ-tests'; +import * as cloudfront from '../lib'; +import { TestOrigin } from './test-origin'; + +const app = new cdk.App(); +const stack = new cdk.Stack(app, 'integ-distribution-origin-id'); + +const origin = new TestOrigin('www.example.com', { originId: 'my-custom-origin-id' }); + +const distribution = new cloudfront.Distribution(stack, 'TestDistribution', { + defaultBehavior: { origin }, +}); +distribution.addBehavior('/second', origin); +distribution.addBehavior('/third', origin); + +new IntegTest(app, 'DistributionOriginId', { + testCases: [stack], +}); + +app.synth(); diff --git a/packages/@aws-cdk/aws-cloudfront/test/test-origin.ts b/packages/@aws-cdk/aws-cloudfront/test/test-origin.ts index 6c7f1bfcba0d6..2f4f70c899eac 100644 --- a/packages/@aws-cdk/aws-cloudfront/test/test-origin.ts +++ b/packages/@aws-cdk/aws-cloudfront/test/test-origin.ts @@ -26,8 +26,10 @@ export class TestOriginGroup implements IOrigin { } } -export function defaultOrigin(domainName?: string): IOrigin { - return new TestOrigin(domainName ?? 'www.example.com'); +export function defaultOrigin(domainName?: string, originId?: string): IOrigin { + return new TestOrigin(domainName ?? 'www.example.com', { + originId, + }); } export function defaultOriginGroup(): IOrigin {