Skip to content

Commit

Permalink
fix(lambda): asset metadata invalid for layers (#4205)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Elad Ben-Israel authored and mergify[bot] committed Sep 23, 2019
1 parent 6e5292e commit d998e46
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
19 changes: 15 additions & 4 deletions packages/@aws-cdk/aws-lambda/lib/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export abstract class Code {
* class to bind to it. Specifically it's required to allow assets to add
* metadata for tooling like SAM CLI to be able to find their origins.
*/
public bindToResource(_resource: CfnResource) {
public bindToResource(_resource: CfnResource, _options?: ResourceBindOptions) {
return;
}
}
Expand Down Expand Up @@ -192,16 +192,27 @@ export class AssetCode extends Code {
};
}

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

// https://github.com/aws/aws-cdk/issues/1432
this.asset.addResourceMetadata(resource, 'Code');
const resourceProperty = options.resourceProperty || 'Code';

// https://github.com/aws/aws-cdk/issues/1432
this.asset.addResourceMetadata(resource, resourceProperty);
}
}

export interface ResourceBindOptions {
/**
* The name of the CloudFormation property to annotate with asset metadata.
* @see https://github.com/aws/aws-cdk/issues/1432
* @default Code
*/
readonly resourceProperty?: string;
}

/**
* Construction properties for {@link CfnParametersCode}.
*/
Expand Down
4 changes: 3 additions & 1 deletion packages/@aws-cdk/aws-lambda/lib/layers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ export class LayerVersion extends LayerVersionBase {
licenseInfo: props.license,
});

props.code.bindToResource(resource);
props.code.bindToResource(resource, {
resourceProperty: 'Content'
});

this.layerVersionArn = resource.ref;
this.compatibleRuntimes = props.compatibleRuntimes;
Expand Down
24 changes: 23 additions & 1 deletion packages/@aws-cdk/aws-lambda/test/test.layers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { expect, haveResource } from '@aws-cdk/assert';
import { expect, haveResource, ResourcePart } from '@aws-cdk/assert';
import s3 = require('@aws-cdk/aws-s3');
import cdk = require('@aws-cdk/core');
import cxapi = require('@aws-cdk/cx-api');
import { Test, testCase } from 'nodeunit';
import path = require('path');
import lambda = require('../lib');

export = testCase({
Expand Down Expand Up @@ -71,4 +73,24 @@ export = testCase({

test.done();
},

'asset metadata is added to the cloudformation resource'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
stack.node.setContext(cxapi.ASSET_RESOURCE_METADATA_ENABLED_CONTEXT, true);

// WHEN
new lambda.LayerVersion(stack, 'layer', {
code: lambda.Code.fromAsset(path.join(__dirname, 'layer-code'))
});

// THEN
expect(stack).to(haveResource('AWS::Lambda::LayerVersion', {
Metadata: {
'aws:asset:path': 'asset.45f085ecc03a1a22cf003fba3fab28e660c92bcfcd4d0c01b62c7cd191070a2d',
'aws:asset:property': 'Content'
}
}, ResourcePart.CompleteDefinition));
test.done();
}
});

0 comments on commit d998e46

Please sign in to comment.