Skip to content

Commit

Permalink
fix(apigateway): deployment not invalidated when integration is chang…
Browse files Browse the repository at this point in the history
…ed (#4552)

* fix(apigateway): deployment not invalidated when integration is changed

The calculation of the deployment logical id is based on a hash of the apigateway model, which includes components such as the resource and method configurations. method integrations, which are part of the method configuration are also included, and could possible include references to other resources such as lambda functions.

When the hash was calculated, tokens have been resolved using `StringConcat` which basically converted all tokens to `[Object object]`. This means, for example, that if we changed the reference of an integration lambda to a different handler, the hash would remain the same.

There is no apparent reason why we can't simply resolve tokens using the CloudFormation concatenation function during "prepare", which means that if we now reference a different resource, the token will resolve to a different value and deployment will be invalidated.

Fixes #4551
Fixes aws-samples/aws-cdk-intro-workshop#83

* fix lint errors

* update expectations

* add lambda asset

* update expectation

* update expectations

* update decdk tests
  • Loading branch information
Elad Ben-Israel authored and mergify[bot] committed Oct 17, 2019
1 parent b149b02 commit eac7695
Show file tree
Hide file tree
Showing 11 changed files with 1,088 additions and 972 deletions.
15 changes: 4 additions & 11 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, DefaultTokenResolver, Lazy, RemovalPolicy, Resource, Stack, StringConcat, Tokenization } from '@aws-cdk/core';
import { Construct, Lazy, RemovalPolicy, Resource, Stack } from '@aws-cdk/core';
import crypto = require('crypto');
import { CfnDeployment, CfnDeploymentProps } from './apigateway.generated';
import { IRestApi } from './restapi';
Expand Down Expand Up @@ -121,20 +121,13 @@ class LatestDeploymentResource extends CfnDeployment {
* add via `addToLogicalId`.
*/
protected prepare() {
const stack = Stack.of(this);

// if hash components were added to the deployment, we use them to calculate
// a logical ID for the deployment resource.
if (this.hashComponents.length > 0) {
const md5 = crypto.createHash('md5');
this.hashComponents
.map(c => {
return Tokenization.resolve(c, {
scope: this,
resolver: new DefaultTokenResolver(new StringConcat()),
preparing: true,
});
})
.forEach(c => md5.update(JSON.stringify(c)));

this.hashComponents.map(x => stack.resolve(x)).forEach(c => md5.update(JSON.stringify(c)));
this.overrideLogicalId(this.originalLogicalId + md5.digest("hex"));
}
super.prepare();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"Name": "cors-api-test"
}
},
"corsapitestDeployment2BF1633Af56aad239353437f465d2090f2edab0c": {
"corsapitestDeployment2BF1633A197b7a426736ab40a59d8a41c2d3d50d": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
Expand All @@ -29,7 +29,7 @@
"Ref": "corsapitest8682546E"
},
"DeploymentId": {
"Ref": "corsapitestDeployment2BF1633Af56aad239353437f465d2090f2edab0c"
"Ref": "corsapitestDeployment2BF1633A197b7a426736ab40a59d8a41c2d3d50d"
},
"StageName": "prod"
}
Expand Down
Loading

0 comments on commit eac7695

Please sign in to comment.