Skip to content

(cloudfront): Function name is created non-deterministically #28629

@laurelmay

Description

@laurelmay

Describe the bug

Creating a CloudFront Function without specifying name can result in non-deterministic naming when the Node ID is relatively long. This results in unexpected function renames.

Expected Behavior

The method used to create a CloudFront Function name should be deterministic, predicable, and stable.

Current Behavior

The name of Functions is unstable, resulting in resource renames and snapshot failures.

Reproduction Steps

Run the following sample integration test several times. See that eventually it bounces between:

  • AReallyLonIdIsAlsoPrettyLongIGuess25040025
  • AReallyLoIdIsAlsoPrettyLongIGuess25040025
    for the name of the function.
import * as cdk from 'aws-cdk-lib';
import * as cloudfront from 'aws-cdk-lib/aws-cloudfront';
import * as integ from '@aws-cdk/integ-tests-alpha';

const app = new cdk.App();
const stack = new cdk.Stack(app, 'A-Really-Long-Stack-Name-Example');

const fn = new cloudfront.Function(stack, 'ExampleFunctionIdIsAlsoPrettyLongIGuess', {
  code: cloudfront.FunctionCode.fromInline('function handler(event) { return event.request }'),
});

new integ.IntegTest(app, 'FunctionNameLong', {
  testCases: [stack],
});

Possible Solution

private generateName(): string {
const name = Stack.of(this).region + Names.uniqueId(this);
if (name.length > 64) {
return name.substring(0, 32) + name.substring(name.length - 32);
}
return name;
}
}
Should probably be reworked but that might result in yet again renaming functions. Names.uniqueResourceName should probably be used instead.

Additional Information/Context

This is also causing the failure on #28571

CDK CLI Version

2.118.0

Framework Version

2.118.0

Node.js Version

v20.2.0

OS

Linux

Language

TypeScript

Language Version

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/aws-cloudfrontRelated to Amazon CloudFrontbugThis issue is a bug.effort/mediumMedium work item – several days of effortp2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions