Skip to content

Commit 092fdc3

Browse files
committed
Switch to using feature flag
1 parent c52c0ea commit 092fdc3

12 files changed

Lines changed: 97 additions & 89 deletions

File tree

packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-long-name.js.snapshot/integ-distribution-function-with-long-name.assets.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-long-name.js.snapshot/integ-distribution-function-with-long-name.template.json

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,10 @@
66
"AutoPublish": true,
77
"FunctionCode": "function handler(event) { return event.request }",
88
"FunctionConfig": {
9-
"Comment": {
10-
"Fn::Join": [
11-
"",
12-
[
13-
{
14-
"Ref": "AWS::Region"
15-
},
16-
"integdistributionfuncestfunctionidnamelong0C1A86EF"
17-
]
18-
]
19-
},
9+
"Comment": "integ-distribution-function-metest-function-id-name-long0C1A86EF",
2010
"Runtime": "cloudfront-js-1.0"
2111
},
22-
"Name": {
23-
"Fn::Join": [
24-
"",
25-
[
26-
{
27-
"Ref": "AWS::Region"
28-
},
29-
"integdistributionfuncestfunctionidnamelong0C1A86EF"
30-
]
31-
]
32-
}
12+
"Name": "integ-distribution-function-metest-function-id-name-long0C1A86EF"
3313
}
3414
}
3515
},

packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-long-name.js.snapshot/manifest.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-long-name.js.snapshot/tree.json

Lines changed: 2 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-long-name.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as integ from '@aws-cdk/integ-tests-alpha';
44

55
const app = new cdk.App();
66
const stack = new cdk.Stack(app, 'integ-distribution-function-with-long-name');
7+
stack.node.setContext('@aws-cdk/aws-cloudfront:useStableGeneratedFunctionName', true);
78

89
new cloudfront.Function(stack, 'test-function-id-name-long', {
910
code: cloudfront.FunctionCode.fromInline('function handler(event) { return event.request }'),

packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function-runtime.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import * as cdk from 'aws-cdk-lib';
22
import { IntegTest } from '@aws-cdk/integ-tests-alpha';
33
import { TestOrigin } from './test-origin';
44
import * as cloudfront from 'aws-cdk-lib/aws-cloudfront';
5+
import * as cxapi from 'aws-cdk-lib/cx-api';
56

67
const app = new cdk.App();
78
const stack = new cdk.Stack(app, 'integ-distribution-function', { env: { region: 'eu-west-1' } });
9+
stack.node.setContext(cxapi.CLOUDFRONT_FUNCTION_STABLE_GENERATED_NAME, false);
810

911
const cfFunctionRequest = new cloudfront.Function(stack, 'FunctionRequest', {
1012
code: cloudfront.FunctionCode.fromInline('function handler(event) { return event.request }'),

packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.distribution-function.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import * as cdk from 'aws-cdk-lib';
22
import { TestOrigin } from './test-origin';
33
import * as cloudfront from 'aws-cdk-lib/aws-cloudfront';
4+
import * as cxapi from 'aws-cdk-lib/cx-api';
45

56
const app = new cdk.App();
67
const stack = new cdk.Stack(app, 'integ-distribution-function', { env: { region: 'eu-west-1' } });
8+
stack.node.setContext(cxapi.CLOUDFRONT_FUNCTION_STABLE_GENERATED_NAME, false);
79

810
const cfFunction = new cloudfront.Function(stack, 'Function', {
911
code: cloudfront.FunctionCode.fromInline('function handler(event) { return event.request }'),

packages/@aws-cdk/cx-api/FEATURE_FLAGS.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Flags come in three types:
6363
| [@aws-cdk/aws-rds:auroraClusterChangeScopeOfInstanceParameterGroupWithEachParameters](#aws-cdkaws-rdsauroraclusterchangescopeofinstanceparametergroupwitheachparameters) | When enabled, a scope of InstanceParameterGroup for AuroraClusterInstance with each parameters will change. | 2.97.0 | (fix) |
6464
| [@aws-cdk/aws-rds:preventRenderingDeprecatedCredentials](#aws-cdkaws-rdspreventrenderingdeprecatedcredentials) | When enabled, creating an RDS database cluster from a snapshot will only render credentials for snapshot credentials. | 2.98.0 | (fix) |
6565
| [@aws-cdk/aws-codepipeline-actions:useNewDefaultBranchForCodeCommitSource](#aws-cdkaws-codepipeline-actionsusenewdefaultbranchforcodecommitsource) | When enabled, the CodeCommit source action is using the default branch name 'main'. | 2.103.1 | (fix) |
66+
| [@aws-cdk/aws-cloudfront:useStableGeneratedFunctionName](#aws-cdkaws-cloudfrontusestablegeneratedfunctionname) | When enabled, the method used to generate CloudFront function names is more consistent across deployments | V2NEXT | (fix) |
6667

6768
<!-- END table -->
6869

@@ -116,7 +117,8 @@ The following json shows the current recommended set of flags, as `cdk init` wou
116117
"@aws-cdk/aws-rds:auroraClusterChangeScopeOfInstanceParameterGroupWithEachParameters": true,
117118
"@aws-cdk/aws-appsync:useArnForSourceApiAssociationIdentifier": true,
118119
"@aws-cdk/aws-rds:preventRenderingDeprecatedCredentials": true,
119-
"@aws-cdk/aws-codepipeline-actions:useNewDefaultBranchForCodeCommitSource": true
120+
"@aws-cdk/aws-codepipeline-actions:useNewDefaultBranchForCodeCommitSource": true,
121+
"@aws-cdk/aws-cloudfront:useStableGeneratedFunctionName": true
120122
}
121123
}
122124
```
@@ -1193,4 +1195,23 @@ However, with the activation of this feature flag, the default branch is updated
11931195
| 2.103.1 | `false` | `true` |
11941196

11951197

1198+
### @aws-cdk/aws-cloudfront:useStableGeneratedFunctionName
1199+
1200+
*When enabled, the method used to generate CloudFront function names is more consistent across deployments* (fix)
1201+
1202+
The method used to generated CloudFront function names can be unstable (may change from
1203+
one deployment to another) for functions with long IDs or that are deeply nested within
1204+
a stack, especially if the region is not specified at deployment time.
1205+
1206+
Enable this flag to use a more deterministic means of generating `functionName`s that does
1207+
not include the region. You will need to specify `functionName` prop directly if including
1208+
the region in the function name is necessary for your use case.
1209+
1210+
1211+
| Since | Default | Recommended |
1212+
| ----- | ----- | ----- |
1213+
| (not in v1) | | |
1214+
| V2NEXT | `false` | `true` |
1215+
1216+
11961217
<!-- END details -->

packages/aws-cdk-lib/aws-cloudfront/lib/function.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as fs from 'fs';
22
import { Construct } from 'constructs';
33
import { CfnFunction } from './cloudfront.generated';
4-
import { IResource, Lazy, Names, Resource, Stack, Token } from '../../core';
5-
import { Fact } from '../../region-info';
4+
import { FeatureFlags, IResource, Lazy, Names, Resource, Stack } from '../../core';
5+
import { CLOUDFRONT_FUNCTION_STABLE_GENERATED_NAME } from '../../cx-api';
66

77
/**
88
* Represents the function's source code
@@ -197,17 +197,17 @@ export class Function extends Resource implements IFunction {
197197
}
198198

199199
private generateName(): string {
200-
// Function names are globally unique, even if the stack is deployed in different regions;
201-
// therefore, we prefix the region name to the resource name. It's important; however, to make
202-
// sure that we don't exceed the service's length limit. We subtract the length of the region
203-
// name from the max length to support the concatenation. If the region is currently unresolved,
204-
// then we use the name of the longest known region.
205-
const serviceMaxNameLength = 64;
206-
const region = Stack.of(this).region;
207-
const regionNameLength = !Token.isUnresolved(region) ? region.length : Math.max(...Fact.regions.map((r) => r.length));
208-
return Lazy.string({
209-
produce: () => `${region}${Names.uniqueResourceName(this, { maxLength: serviceMaxNameLength - regionNameLength })}`,
210-
});
200+
if (FeatureFlags.of(this).isEnabled(CLOUDFRONT_FUNCTION_STABLE_GENERATED_NAME)) {
201+
return Lazy.string({
202+
produce: () => Names.uniqueResourceName(this, { maxLength: 64, allowedSpecialCharacters: '-_' }),
203+
});
204+
}
205+
206+
const name = Stack.of(this).region + Names.uniqueId(this);
207+
if (name.length > 64) {
208+
return name.substring(0, 32) + name.substring(name.length - 32);
209+
}
210+
return name;
211211
}
212212
}
213213

packages/aws-cdk-lib/aws-cloudfront/test/function.test.ts

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as path from 'path';
22
import { Template } from '../../assertions';
33
import { App, Stack } from '../../core';
4+
import { CLOUDFRONT_FUNCTION_STABLE_GENERATED_NAME } from '../../cx-api';
45
import { Function, FunctionCode, FunctionRuntime } from '../lib';
56

67
describe('CloudFront Function', () => {
@@ -77,9 +78,10 @@ describe('CloudFront Function', () => {
7778
});
7879
});
7980

80-
test('long stack and resource id in environment agnostic stack', () => {
81+
test('long stack and resource id in environment agnostic stack with feature flag set', () => {
8182
const app = new App();
8283
const stack = new Stack(app, 'test-stack-name-longer-than-thirtytwo-characters');
84+
stack.node.setContext(CLOUDFRONT_FUNCTION_STABLE_GENERATED_NAME, true);
8385
new Function(stack, 'test-resoruce-id-also-longer-but-with-different-end', {
8486
code: FunctionCode.fromInline('code'),
8587
});
@@ -89,31 +91,11 @@ describe('CloudFront Function', () => {
8991
testresoruceidalsolongerbutwithdifferentendFD0687C9: {
9092
Type: 'AWS::CloudFront::Function',
9193
Properties: {
92-
Name: {
93-
'Fn::Join': [
94-
'',
95-
[
96-
{
97-
Ref: 'AWS::Region',
98-
},
99-
'teststacknamelongertherbutwithdifferentend3C400F7C',
100-
],
101-
],
102-
},
94+
Name: 'test-stack-name-longer-than-onger-but-with-different-end3C400F7C',
10395
AutoPublish: true,
10496
FunctionCode: 'code',
10597
FunctionConfig: {
106-
Comment: {
107-
'Fn::Join': [
108-
'',
109-
[
110-
{
111-
Ref: 'AWS::Region',
112-
},
113-
'teststacknamelongertherbutwithdifferentend3C400F7C',
114-
],
115-
],
116-
},
98+
Comment: 'test-stack-name-longer-than-onger-but-with-different-end3C400F7C',
11799
Runtime: 'cloudfront-js-1.0',
118100
},
119101
},
@@ -122,9 +104,10 @@ describe('CloudFront Function', () => {
122104
});
123105
});
124106

125-
test('long stack and resource id with region specified', () => {
107+
test('long stack and resource id with region specified with feature flag set', () => {
126108
const app = new App();
127-
const stack = new Stack(app, 'test-stack-name-longer-than-thirtytwo-characters', { env: { region: 'test-east-1' } });
109+
const stack = new Stack(app, 'test-stack-name-longer-than-thirtytwo-characters', { env: { region: 'test-region-1' } });
110+
stack.node.setContext(CLOUDFRONT_FUNCTION_STABLE_GENERATED_NAME, true);
128111
new Function(stack, 'test-resoruce-id-also-longer-but-with-different-end', {
129112
code: FunctionCode.fromInline('code'),
130113
});
@@ -134,11 +117,11 @@ describe('CloudFront Function', () => {
134117
testresoruceidalsolongerbutwithdifferentendFD0687C9: {
135118
Type: 'AWS::CloudFront::Function',
136119
Properties: {
137-
Name: 'test-east-1teststacknamelongerthagerbutwithdifferentend3C400F7C',
120+
Name: 'test-stack-name-longer-than-onger-but-with-different-end3C400F7C',
138121
AutoPublish: true,
139122
FunctionCode: 'code',
140123
FunctionConfig: {
141-
Comment: 'test-east-1teststacknamelongerthagerbutwithdifferentend3C400F7C',
124+
Comment: 'test-stack-name-longer-than-onger-but-with-different-end3C400F7C',
142125
Runtime: 'cloudfront-js-1.0',
143126
},
144127
},

0 commit comments

Comments
 (0)