Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(cloudfront): remove the use of calculateFunctionHash #14583

Merged
merged 1 commit into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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