@@ -252,5 +252,60 @@ describe('S3EventSource', () => {
252
252
'SourceArn' : 'arn:aws:s3:::some-bucket-not-in-this-account' ,
253
253
} ) ;
254
254
} ) ;
255
- } ) ;
256
255
256
+ test ( 'Test bucket account is referenced intrinsicly' , ( ) => {
257
+ // GIVEN
258
+ const stack = new cdk . Stack ( ) ;
259
+ const fn = new TestFunction ( stack , 'Fn' ) ;
260
+ const bucket = new s3 . Bucket ( stack , 'B' ) ;
261
+
262
+ // WHEN
263
+ fn . addEventSource ( new sources . S3EventSource ( bucket , {
264
+ events : [ s3 . EventType . OBJECT_CREATED , s3 . EventType . OBJECT_REMOVED ] ,
265
+ filters : [
266
+ { prefix : 'prefix/' } ,
267
+ { suffix : '.png' } ,
268
+ ] ,
269
+ } ) ) ;
270
+
271
+ // THEN
272
+ Template . fromStack ( stack ) . hasResourceProperties ( 'AWS::Lambda::Permission' , {
273
+ 'Principal' : 's3.amazonaws.com' ,
274
+ 'SourceAccount' : {
275
+ 'Ref' : 'AWS::AccountId' ,
276
+ } ,
277
+ 'SourceArn' : {
278
+ 'Fn::GetAtt' : [ 'B08E7C7AF' , 'Arn' ] ,
279
+ } ,
280
+ } ) ;
281
+ } ) ;
282
+
283
+ test ( 'Default to stack account if bucket account doesnt exist' , ( ) => {
284
+ // GIVEN
285
+ const app = new cdk . App ( ) ;
286
+ const stack = new cdk . Stack ( app , 'stack' ) ;
287
+ const fn = new TestFunction ( stack , 'Fn' ) ;
288
+
289
+ let accountB = '' ;
290
+ //WHEN
291
+ const foreignBucket =
292
+ s3 . Bucket . fromBucketAttributes ( stack , 'ImportedBucket' , {
293
+ bucketArn : 'arn:aws:s3:::some-bucket-not-in-this-account' ,
294
+ // The account the bucket really lives in
295
+ account : accountB ,
296
+ } ) ;
297
+
298
+ // This will generate the IAM bindings
299
+ fn . addEventSource ( new sources . S3EventSource ( foreignBucket as s3 . Bucket ,
300
+ { events : [ s3 . EventType . OBJECT_CREATED ] } ) ) ;
301
+
302
+ // THEN
303
+ Template . fromStack ( stack ) . hasResourceProperties ( 'AWS::Lambda::Permission' , {
304
+ 'Principal' : 's3.amazonaws.com' ,
305
+ 'SourceAccount' : {
306
+ 'Ref' : 'AWS::AccountId' ,
307
+ } ,
308
+ 'SourceArn' : 'arn:aws:s3:::some-bucket-not-in-this-account' ,
309
+ } ) ;
310
+ } ) ;
311
+ } ) ;
0 commit comments