File tree Expand file tree Collapse file tree 3 files changed +41
-6
lines changed
packages/@aws-cdk/aws-lambda Expand file tree Collapse file tree 3 files changed +41
-6
lines changed Original file line number Diff line number Diff line change @@ -90,7 +90,7 @@ export abstract class Code {
90
90
* class to bind to it. Specifically it's required to allow assets to add
91
91
* metadata for tooling like SAM CLI to be able to find their origins.
92
92
*/
93
- public bindToResource ( _resource : CfnResource ) {
93
+ public bindToResource ( _resource : CfnResource , _options ?: ResourceBindOptions ) {
94
94
return ;
95
95
}
96
96
}
@@ -192,16 +192,27 @@ export class AssetCode extends Code {
192
192
} ;
193
193
}
194
194
195
- public bindToResource ( resource : CfnResource ) {
195
+ public bindToResource ( resource : CfnResource , options : ResourceBindOptions = { } ) {
196
196
if ( ! this . asset ) {
197
197
throw new Error ( `bindToResource() must be called after bind()` ) ;
198
198
}
199
199
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 ) ;
202
204
}
203
205
}
204
206
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
+
205
216
/**
206
217
* Construction properties for {@link CfnParametersCode}.
207
218
*/
Original file line number Diff line number Diff line change @@ -192,7 +192,9 @@ export class LayerVersion extends LayerVersionBase {
192
192
licenseInfo : props . license ,
193
193
} ) ;
194
194
195
- props . code . bindToResource ( resource ) ;
195
+ props . code . bindToResource ( resource , {
196
+ resourceProperty : 'Content'
197
+ } ) ;
196
198
197
199
this . layerVersionArn = resource . ref ;
198
200
this . compatibleRuntimes = props . compatibleRuntimes ;
Original file line number Diff line number Diff line change 1
- import { expect , haveResource } from '@aws-cdk/assert' ;
1
+ import { expect , haveResource , ResourcePart } from '@aws-cdk/assert' ;
2
2
import s3 = require( '@aws-cdk/aws-s3' ) ;
3
3
import cdk = require( '@aws-cdk/core' ) ;
4
+ import cxapi = require( '@aws-cdk/cx-api' ) ;
4
5
import { Test , testCase } from 'nodeunit' ;
6
+ import path = require( 'path' ) ;
5
7
import lambda = require( '../lib' ) ;
6
8
7
9
export = testCase ( {
@@ -71,4 +73,24 @@ export = testCase({
71
73
72
74
test . done ( ) ;
73
75
} ,
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
+ }
74
96
} ) ;
You can’t perform that action at this time.
0 commit comments