Skip to content

Commit e1b48bc

Browse files
DavidChristiansenmergify[bot]
authored andcommitted
feat(s3-deployment): optional role override for bucket-deployment (#4284)
* Optional role override to bucket-deployment * Updated props documentation * Update bucket-deployment.ts
1 parent 9c208d0 commit e1b48bc

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

packages/@aws-cdk/aws-s3-deployment/lib/bucket-deployment.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ export interface BucketDeploymentProps {
7070
* @default 128
7171
*/
7272
readonly memoryLimit?: number;
73+
74+
/**
75+
* Execution role associated with this function
76+
*
77+
* @default - A role is automatically created
78+
*/
79+
readonly role?: iam.IRole;
7380
}
7481

7582
export class BucketDeployment extends cdk.Construct {
@@ -91,6 +98,7 @@ export class BucketDeployment extends cdk.Construct {
9198
handler: 'index.handler',
9299
lambdaPurpose: 'Custom::CDKBucketDeployment',
93100
timeout: cdk.Duration.minutes(15),
101+
role: props.role,
94102
memorySize: props.memoryLimit
95103
});
96104

@@ -156,4 +164,4 @@ function calcSourceHash(srcDir: string): string {
156164
}
157165

158166
return sha.digest('hex');
159-
}
167+
}

packages/@aws-cdk/aws-s3-deployment/test/test.bucket-deployment.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { countResources, expect, haveResource } from '@aws-cdk/assert';
22
import cloudfront = require('@aws-cdk/aws-cloudfront');
3+
import iam = require('@aws-cdk/aws-iam');
34
import s3 = require('@aws-cdk/aws-s3');
45
import cdk = require('@aws-cdk/core');
56
import { Test } from 'nodeunit';
@@ -234,7 +235,7 @@ export = {
234235
s3OriginSource: {
235236
s3BucketSource: bucket
236237
},
237-
behaviors : [ {isDefaultBehavior: true}]
238+
behaviors: [{ isDefaultBehavior: true }]
238239
}
239240
]
240241
});
@@ -267,7 +268,7 @@ export = {
267268
s3OriginSource: {
268269
s3BucketSource: bucket
269270
},
270-
behaviors : [ {isDefaultBehavior: true}]
271+
behaviors: [{ isDefaultBehavior: true }]
271272
}
272273
]
273274
});
@@ -427,8 +428,38 @@ export = {
427428
// we expect to find only two handlers, one for each configuration
428429

429430
expect(stack).to(countResources('AWS::Lambda::Function', 2));
430-
expect(stack).to(haveResource('AWS::Lambda::Function', { MemorySize: 256 }));
431+
expect(stack).to(haveResource('AWS::Lambda::Function', { MemorySize: 256 }));
431432
expect(stack).to(haveResource('AWS::Lambda::Function', { MemorySize: 1024 }));
432433
test.done();
434+
},
435+
436+
'deployment allows custom role to be supplied'(test: Test) {
437+
438+
// GIVEN
439+
const stack = new cdk.Stack();
440+
const bucket = new s3.Bucket(stack, 'Dest');
441+
const existingRole = new iam.Role(stack, 'Role', {
442+
assumedBy: new iam.ServicePrincipal('lambda.amazon.com')
443+
});
444+
445+
// WHEN
446+
new s3deploy.BucketDeployment(stack, 'DeployWithRole', {
447+
sources: [s3deploy.Source.asset(path.join(__dirname, 'my-website'))],
448+
destinationBucket: bucket,
449+
role: existingRole
450+
});
451+
452+
// THEN
453+
expect(stack).to(countResources('AWS::IAM::Role', 1));
454+
expect(stack).to(countResources('AWS::Lambda::Function', 1));
455+
expect(stack).to(haveResource('AWS::Lambda::Function', {
456+
"Role": {
457+
"Fn::GetAtt": [
458+
"Role1ABCC5F0",
459+
"Arn"
460+
]
461+
}
462+
}));
463+
test.done();
433464
}
434465
};

0 commit comments

Comments
 (0)