Skip to content

Commit

Permalink
fix(apigateway): Token resolution should occur within the prepare con…
Browse files Browse the repository at this point in the history
…text

stack.resolve() works only after the CDK app has been fully prepared.
During the 'prepare' phase, it should instead resolve the token
partially and within the local context.

fixes #3705
  • Loading branch information
Niranjan Jayakar committed Sep 2, 2019
1 parent 16eb658 commit 6d3ed80
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
17 changes: 14 additions & 3 deletions packages/@aws-cdk/aws-apigateway/lib/deployment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Construct, Lazy, RemovalPolicy, Resource, Stack } from '@aws-cdk/core';
import { Construct, DefaultTokenResolver, Lazy, RemovalPolicy, Resource, Stack, StringConcat, Tokenization } from '@aws-cdk/core';
import crypto = require('crypto');
import { CfnDeployment, CfnDeploymentProps } from './apigateway.generated';
import { IRestApi } from './restapi';
Expand Down Expand Up @@ -128,12 +128,23 @@ class LatestDeploymentResource extends CfnDeployment {
if (this.hashComponents.length > 0) {
const md5 = crypto.createHash('md5');
this.hashComponents
.map(c => stack.resolve(c))
.map(c => {
try {
// TODO: Remove the code in the try block with next major version release of CDK.
// It's here to be backwards compatible, i.e., prevent LogicalIds to change.
return stack.resolve(c);
} catch (e) {
return Tokenization.resolve(c, {
scope: this,
resolver: new DefaultTokenResolver(new StringConcat()),
preparing: true,
});
}
})
.forEach(c => md5.update(JSON.stringify(c)));

this.overrideLogicalId(this.originalLogicalId + md5.digest("hex"));
}

super.prepare();
}
}
10 changes: 8 additions & 2 deletions packages/@aws-cdk/core/lib/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ export class Tokenization {
*/
public static resolve(obj: any, options: ResolveOptions): any {
return resolve(obj, {
...options,
preparing: false
scope: options.scope,
resolver: options.resolver,
preparing: options.preparing || false
});
}

Expand Down Expand Up @@ -150,6 +151,11 @@ export interface ResolveOptions {
* The resolver to apply to any resolvable tokens found
*/
readonly resolver: ITokenResolver;

/**
* Whether the resolution is being executed during the prepare phase or not.
*/
readonly preparing?: boolean;
}

/**
Expand Down

0 comments on commit 6d3ed80

Please sign in to comment.