Skip to content

Commit 721f2d7

Browse files
author
Elad Ben-Israel
committed
get rid of codeHash
1 parent e5b0e25 commit 721f2d7

File tree

7 files changed

+34
-99
lines changed

7 files changed

+34
-99
lines changed

packages/@aws-cdk/aws-lambda/lib/code.ts

-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as s3 from '@aws-cdk/aws-s3';
22
import * as s3_assets from '@aws-cdk/aws-s3-assets';
33
import * as cdk from '@aws-cdk/core';
4-
import * as crypto from 'crypto';
54

65
export abstract class Code {
76
/**
@@ -105,12 +104,6 @@ export interface CodeConfig {
105104
* Inline code (mutually exclusive with `s3Location`).
106105
*/
107106
readonly inlineCode?: string;
108-
109-
/**
110-
* The hash of the lambda code (if applicable) or `undefined` if it cannot be
111-
* computed.
112-
*/
113-
readonly codeHash?: string;
114107
}
115108

116109
/**
@@ -147,7 +140,6 @@ export class S3Code extends Code {
147140
*/
148141
export class InlineCode extends Code {
149142
public readonly isInline = true;
150-
private readonly codeHash: string;
151143

152144
constructor(private code: string) {
153145
super();
@@ -159,14 +151,11 @@ export class InlineCode extends Code {
159151
if (code.length > 4096) {
160152
throw new Error("Lambda source is too large, must be <= 4096 but is " + code.length);
161153
}
162-
163-
this.codeHash = crypto.createHash('sha256').update(code).digest('hex');
164154
}
165155

166156
public bind(_scope: cdk.Construct): CodeConfig {
167157
return {
168158
inlineCode: this.code,
169-
codeHash: this.codeHash
170159
};
171160
}
172161
}
@@ -200,7 +189,6 @@ export class AssetCode extends Code {
200189
}
201190

202191
return {
203-
codeHash: this.asset.sourceHash,
204192
s3Location: {
205193
bucketName: this.asset.s3BucketName,
206194
objectKey: this.asset.s3ObjectKey

packages/@aws-cdk/aws-lambda/lib/function-hash.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,11 @@ export function calculateFunctionHash(fn: LambdaFunction) {
77

88
const functionResource = fn.node.defaultChild as CfnResource;
99

10-
if (!fn._codeHash) {
11-
throw new Error(`assertion failed: codeHash is expected to have a value if currentVersion is defined`);
12-
}
13-
1410
// render the cloudformation resource from this function
1511
const config = stack.resolve((functionResource as any)._toCloudFormation());
1612

1713
const hash = crypto.createHash('md5');
18-
hash.update(JSON.stringify({
19-
code: fn._codeHash,
20-
config
21-
}));
14+
hash.update(JSON.stringify(config));
2215

2316
return hash.digest('hex');
2417
}

packages/@aws-cdk/aws-lambda/lib/function.ts

+2-14
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,6 @@ export class Function extends FunctionBase {
287287
return this._currentVersion;
288288
}
289289

290-
if (!this._codeHash) {
291-
throw new Error(
292-
`cannot automatically create a version resource for this function since the hash of the code cannot be calculated. ` +
293-
`This is only supported for "lambda.Code.fromAsset" and "lambda.Code.fromInline"`);
294-
}
295-
296290
this._currentVersion = new Version(this, `CurrentVersion`, {
297291
lambda: this,
298292
description: `${this.node.path}.currentVersion (created by AWS CDK)`,
@@ -470,11 +464,6 @@ export class Function extends FunctionBase {
470464

471465
public readonly permissionsNode = this.node;
472466

473-
/**
474-
* @internal
475-
*/
476-
public _codeHash?: string;
477-
478467
protected readonly canCreatePermissions = true;
479468

480469
private readonly layers: ILayerVersion[] = [];
@@ -520,7 +509,6 @@ export class Function extends FunctionBase {
520509
verifyCodeConfig(code, props.runtime);
521510

522511
this.deadLetterQueue = this.buildDeadLetterQueue(props);
523-
this._codeHash = code.codeHash;
524512

525513
const resource: CfnFunction = new CfnFunction(this, 'Resource', {
526514
functionName: this.physicalName,
@@ -820,5 +808,5 @@ export function verifyCodeConfig(code: CodeConfig, runtime: Runtime) {
820808
// if this is inline code, check that the runtime supports
821809
if (code.inlineCode && !runtime.supportsInlineCode) {
822810
throw new Error(`Inline source not allowed for ${runtime.name}`);
823-
}}
824-
811+
}
812+
}

packages/@aws-cdk/aws-lambda/test/test.code.ts

-37
Original file line numberDiff line numberDiff line change
@@ -83,43 +83,6 @@ export = {
8383
}, ResourcePart.CompleteDefinition));
8484
test.done();
8585
},
86-
87-
'codeHash is supported for assets and inline'(test: Test) {
88-
// GIVEN
89-
const stack = new cdk.Stack();
90-
91-
// WHEN
92-
const asset = new lambda.Function(stack, 'AssetCode', {
93-
code: lambda.Code.fromAsset(path.join(__dirname, 'my-lambda-handler')),
94-
runtime: lambda.Runtime.NODEJS_12_X,
95-
handler: 'index.handler'
96-
});
97-
98-
const inline = new lambda.Function(stack, 'InlineCode', {
99-
code: lambda.Code.fromInline('boom boom'),
100-
runtime: lambda.Runtime.NODEJS_12_X,
101-
handler: 'index.handler'
102-
});
103-
104-
const bucket = new lambda.Function(stack, 'S3Code', {
105-
code: lambda.Code.fromBucket(s3.Bucket.fromBucketName(stack, 'Bucket', 'bucket'), 'key'),
106-
runtime: lambda.Runtime.NODEJS_12_X,
107-
handler: 'index.handler'
108-
});
109-
110-
const params = new lambda.Function(stack, 'CfnParams', {
111-
code: lambda.Code.fromCfnParameters(),
112-
runtime: lambda.Runtime.NODEJS_12_X,
113-
handler: 'index.handler'
114-
});
115-
116-
// THEN
117-
test.deepEqual(asset._codeHash, '9678c34eca93259d11f2d714177347afd66c50116e1e08996eff893d3ca81232');
118-
test.deepEqual(inline._codeHash, '58ad733fe276cb7b9b171b0beafad40f43b6d1cd68a1b93327d3655f739dfa0e');
119-
test.deepEqual(bucket._codeHash, undefined);
120-
test.deepEqual(params._codeHash, undefined);
121-
test.done();
122-
}
12386
},
12487

12588
'lambda.Code.fromCfnParameters': {

packages/@aws-cdk/aws-lambda/test/test.function-hash.ts

+28-8
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ export = {
3939
handler: 'index.handler'
4040
});
4141

42-
test.deepEqual(calculateFunctionHash(fn1), 'a1dd0e860a511e65bf720e3bd0f6b6a9');
43-
test.deepEqual(calculateFunctionHash(fn2), 'a1dd0e860a511e65bf720e3bd0f6b6a9');
42+
test.deepEqual(calculateFunctionHash(fn1), calculateFunctionHash(fn2));
43+
test.deepEqual(calculateFunctionHash(fn1), 'aea5463dba236007afe91d2832b3c836');
4444
test.done();
4545
},
4646
},
@@ -53,8 +53,8 @@ export = {
5353
handler: 'index.handler'
5454
});
5555

56-
test.notDeepEqual(calculateFunctionHash(fn1), 'a1dd0e860a511e65bf720e3bd0f6b6a9');
57-
test.deepEqual(calculateFunctionHash(fn1), '1ad32fcab0aa68bf36825df8e6f0a7a2');
56+
test.notDeepEqual(calculateFunctionHash(fn1), 'aea5463dba236007afe91d2832b3c836');
57+
test.deepEqual(calculateFunctionHash(fn1), '979b4a14c6f174c745cdbcd1036cf844');
5858
test.done();
5959
},
6060

@@ -79,8 +79,8 @@ export = {
7979
}
8080
});
8181

82-
test.deepEqual(calculateFunctionHash(fn1), 'ea4daca738e83756eec5a3a3c806d54d');
83-
test.deepEqual(calculateFunctionHash(fn2), '2cb1ed852c739c60254b3bbfb258e513');
82+
test.deepEqual(calculateFunctionHash(fn1), 'd1bc824ac5022b7d62d8b12dbae6580c');
83+
test.deepEqual(calculateFunctionHash(fn2), '3b683d05465012b0aa9c4ff53b32f014');
8484
test.done();
8585
},
8686

@@ -105,8 +105,28 @@ export = {
105105
}
106106
});
107107

108-
test.deepEqual(calculateFunctionHash(fn1), 'ea4daca738e83756eec5a3a3c806d54d');
109-
test.deepEqual(calculateFunctionHash(fn2), '4bdbec98b3c838410b5637fb71101514');
108+
test.deepEqual(calculateFunctionHash(fn1), 'd1bc824ac5022b7d62d8b12dbae6580c');
109+
test.deepEqual(calculateFunctionHash(fn2), '0f168f0772463e8e547bb3800937e54d');
110+
test.done();
111+
},
112+
113+
'inline code change impacts the hash'(test: Test) {
114+
const stack1 = new Stack();
115+
const fn1 = new lambda.Function(stack1, 'MyFunction', {
116+
runtime: lambda.Runtime.NODEJS_12_X,
117+
code: lambda.Code.fromInline('foo'),
118+
handler: 'index.handler',
119+
});
120+
121+
const stack2 = new Stack();
122+
const fn2 = new lambda.Function(stack2, 'MyFunction', {
123+
runtime: lambda.Runtime.NODEJS_10_X,
124+
code: lambda.Code.fromInline('foo bar'),
125+
handler: 'index.handler',
126+
});
127+
128+
test.deepEqual(calculateFunctionHash(fn1), 'ebf2e871fc6a3062e8bdcc5ebe16db3f');
129+
test.deepEqual(calculateFunctionHash(fn2), 'ffedf6424a18a594a513129dc97bf53c');
110130
test.done();
111131
}
112132
};

packages/@aws-cdk/aws-lambda/test/test.function.ts

+2-19
Original file line numberDiff line numberDiff line change
@@ -185,22 +185,6 @@ export = testCase({
185185

186186
'currentVersion': {
187187

188-
'cannot be created for unsupported lambda.Code types'(test: Test) {
189-
// GIVEN
190-
const stack = new cdk.Stack();
191-
192-
// WHEN
193-
const fn = new lambda.Function(stack, 'fn', {
194-
handler: 'foo',
195-
runtime: lambda.Runtime.NODEJS_12_X,
196-
code: lambda.Code.fromCfnParameters()
197-
});
198-
199-
// THEN
200-
test.throws(() => fn.currentVersion, /cannot automatically create a version resource for this function/);
201-
test.done();
202-
},
203-
204188
// see test.function-hash.ts for more coverage for this
205189
'logical id of version is based on the function hash'(test: Test) {
206190
// GIVEN
@@ -235,18 +219,17 @@ export = testCase({
235219
expect(stack1).to(haveOutput({
236220
outputName: 'CurrentVersionArn',
237221
outputValue: {
238-
Ref: "MyFunctionCurrentVersion197490AFa10cabe18b67f28a68a1ad20c7468ba6"
222+
Ref: "MyFunctionCurrentVersion197490AF1a9a73cf5c46aec5e40fb202042eb60b"
239223
}
240224
}));
241225
expect(stack2).to(haveOutput({
242226
outputName: 'CurrentVersionArn',
243227
outputValue: {
244-
Ref: "MyFunctionCurrentVersion197490AF35a71af54fb82ee3ff968834b82d088c"
228+
Ref: "MyFunctionCurrentVersion197490AF8360a045031060e3117269037b7bffd6"
245229
}
246230
}));
247231
test.done();
248232
}
249-
250233
},
251234

252235
});

packages/@aws-cdk/aws-lambda/test/test.lambda-version.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export = {
134134
},
135135
"FunctionVersion": {
136136
"Fn::GetAtt": [
137-
"FnCurrentVersion17A89ABB3127ebe0d45a77e18d3b1ca1c6c8c55c",
137+
"FnCurrentVersion17A89ABB19ed45993ff69fd011ae9fd4ab6e2005",
138138
"Version"
139139
]
140140
},

0 commit comments

Comments
 (0)