Skip to content

Commit 1e896e4

Browse files
committed
Added more test
1 parent d381a90 commit 1e896e4

File tree

1 file changed

+56
-1
lines changed
  • packages/aws-cdk-lib/aws-lambda-event-sources/test

1 file changed

+56
-1
lines changed

packages/aws-cdk-lib/aws-lambda-event-sources/test/s3.test.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,5 +252,60 @@ describe('S3EventSource', () => {
252252
'SourceArn': 'arn:aws:s3:::some-bucket-not-in-this-account',
253253
});
254254
});
255-
});
256255

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

Comments
 (0)