Skip to content

Commit

Permalink
chore(cloudfront): remove the use of calculateFunctionHash (aws#14583)
Browse files Browse the repository at this point in the history
`calculateFunctionHash()` was used to compute the 'refresh token' of the
custom resource for the EdgeFunction construct.

This method is private to the lambda module and is deemed to be changed.
Instead, use the lambda function version's logical id.

The logical id of the version includes computing the function hash (among
others) and is a more reliable determinant of whether the underlying
function version changed.


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
Niranjan Jayakar authored and hollanddd committed Aug 26, 2021
1 parent 5c3024b commit af9bff4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
23 changes: 15 additions & 8 deletions packages/@aws-cdk/aws-cloudfront/lib/experimental/edge-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as iam from '@aws-cdk/aws-iam';
import * as lambda from '@aws-cdk/aws-lambda';
// hack, as this is not exported by the Lambda module
import { calculateFunctionHash } from '@aws-cdk/aws-lambda/lib/function-hash';
import * as ssm from '@aws-cdk/aws-ssm';
import {
ConstructNode,
CfnResource, ConstructNode,
CustomResource, CustomResourceProvider, CustomResourceProviderRuntime,
Resource, Stack, Stage, Token,
Lazy, Resource, Stack, Stage, Token,
} from '@aws-cdk/core';
import { Construct } from 'constructs';

Expand Down Expand Up @@ -157,17 +155,18 @@ export class EdgeFunction extends Resource implements lambda.IVersion {
addEdgeLambdaToRoleTrustStatement(edgeFunction.role!);

// Store the current version's ARN to be retrieved by the cross region reader below.
const version = edgeFunction.currentVersion;
new ssm.StringParameter(edgeFunction, 'Parameter', {
parameterName,
stringValue: edgeFunction.currentVersion.edgeArn,
stringValue: version.edgeArn,
});

const edgeArn = this.createCrossRegionArnReader(parameterNamePrefix, parameterName, edgeFunction);
const edgeArn = this.createCrossRegionArnReader(parameterNamePrefix, parameterName, version);

return { edgeFunction, edgeArn };
}

private createCrossRegionArnReader(parameterNamePrefix: string, parameterName: string, edgeFunction: lambda.Function): string {
private createCrossRegionArnReader(parameterNamePrefix: string, parameterName: string, version: lambda.Version): string {
// Prefix of the parameter ARN that applies to all EdgeFunctions.
// This is necessary because the `CustomResourceProvider` is a singleton, and the `policyStatement`
// must work for multiple EdgeFunctions.
Expand Down Expand Up @@ -196,7 +195,15 @@ export class EdgeFunction extends Resource implements lambda.IVersion {
Region: EdgeFunction.EDGE_REGION,
ParameterName: parameterName,
// This is used to determine when the function has changed, to refresh the ARN from the custom resource.
RefreshToken: calculateFunctionHash(edgeFunction),
//
// Use the logical id of the function version. Whenever a function version changes, the logical id must be
// changed for it to take effect - a good candidate for RefreshToken.
RefreshToken: Lazy.uncachedString({
produce: () => {
const cfn = version.node.defaultChild as CfnResource;
return this.stack.resolve(cfn.logicalId);
},
}),
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"Region": "us-east-1",
"ParameterName": "/cdk/EdgeFunctionArn/eu-west-1/integ-distribution-lambda-cross-region/Lambda",
"RefreshToken": "4412ddb0ae449da20173ca211c51fddc"
"RefreshToken": "LambdaCurrentVersionDF706F6A97fb843e9bd06fcd2bb15eeace80e13e"
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
Expand Down Expand Up @@ -138,7 +138,7 @@
},
"Region": "us-east-1",
"ParameterName": "/cdk/EdgeFunctionArn/eu-west-1/integ-distribution-lambda-cross-region/Lambda2",
"RefreshToken": "8f81ceb404ac454f09648e62822d9ca9"
"RefreshToken": "Lambda2CurrentVersion72012B74b9eef8becb98501bc795baca3c6169c4"
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
Expand Down

0 comments on commit af9bff4

Please sign in to comment.