Skip to content

Commit d998e46

Browse files
Elad Ben-Israelmergify[bot]
authored andcommitted
fix(lambda): asset metadata invalid for layers (#4205)
* fix(lambda): asset metadata invalid for layers The `aws:asset:property` metadata entry was "Code" intead of "Content" as it should be for lambda layers. Fixes #4076 Fixes aws/aws-sam-cli#1411 * fix build
1 parent 6e5292e commit d998e46

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export abstract class Code {
9090
* class to bind to it. Specifically it's required to allow assets to add
9191
* metadata for tooling like SAM CLI to be able to find their origins.
9292
*/
93-
public bindToResource(_resource: CfnResource) {
93+
public bindToResource(_resource: CfnResource, _options?: ResourceBindOptions) {
9494
return;
9595
}
9696
}
@@ -192,16 +192,27 @@ export class AssetCode extends Code {
192192
};
193193
}
194194

195-
public bindToResource(resource: CfnResource) {
195+
public bindToResource(resource: CfnResource, options: ResourceBindOptions = { }) {
196196
if (!this.asset) {
197197
throw new Error(`bindToResource() must be called after bind()`);
198198
}
199199

200-
// https://github.com/aws/aws-cdk/issues/1432
201-
this.asset.addResourceMetadata(resource, 'Code');
200+
const resourceProperty = options.resourceProperty || 'Code';
201+
202+
// https://github.com/aws/aws-cdk/issues/1432
203+
this.asset.addResourceMetadata(resource, resourceProperty);
202204
}
203205
}
204206

207+
export interface ResourceBindOptions {
208+
/**
209+
* The name of the CloudFormation property to annotate with asset metadata.
210+
* @see https://github.com/aws/aws-cdk/issues/1432
211+
* @default Code
212+
*/
213+
readonly resourceProperty?: string;
214+
}
215+
205216
/**
206217
* Construction properties for {@link CfnParametersCode}.
207218
*/

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,9 @@ export class LayerVersion extends LayerVersionBase {
192192
licenseInfo: props.license,
193193
});
194194

195-
props.code.bindToResource(resource);
195+
props.code.bindToResource(resource, {
196+
resourceProperty: 'Content'
197+
});
196198

197199
this.layerVersionArn = resource.ref;
198200
this.compatibleRuntimes = props.compatibleRuntimes;

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { expect, haveResource } from '@aws-cdk/assert';
1+
import { expect, haveResource, ResourcePart } from '@aws-cdk/assert';
22
import s3 = require('@aws-cdk/aws-s3');
33
import cdk = require('@aws-cdk/core');
4+
import cxapi = require('@aws-cdk/cx-api');
45
import { Test, testCase } from 'nodeunit';
6+
import path = require('path');
57
import lambda = require('../lib');
68

79
export = testCase({
@@ -71,4 +73,24 @@ export = testCase({
7173

7274
test.done();
7375
},
76+
77+
'asset metadata is added to the cloudformation resource'(test: Test) {
78+
// GIVEN
79+
const stack = new cdk.Stack();
80+
stack.node.setContext(cxapi.ASSET_RESOURCE_METADATA_ENABLED_CONTEXT, true);
81+
82+
// WHEN
83+
new lambda.LayerVersion(stack, 'layer', {
84+
code: lambda.Code.fromAsset(path.join(__dirname, 'layer-code'))
85+
});
86+
87+
// THEN
88+
expect(stack).to(haveResource('AWS::Lambda::LayerVersion', {
89+
Metadata: {
90+
'aws:asset:path': 'asset.45f085ecc03a1a22cf003fba3fab28e660c92bcfcd4d0c01b62c7cd191070a2d',
91+
'aws:asset:property': 'Content'
92+
}
93+
}, ResourcePart.CompleteDefinition));
94+
test.done();
95+
}
7496
});

0 commit comments

Comments
 (0)