Skip to content

Commit 9c75c91

Browse files
authored
feat(infra): Expose Optional Lambda Execution Role Property [CLK-239123] (#6)
* feat(infra): Expose Lambda execution role property [CLK-239123] * chore(infra): Add UT to verify role property sets execution role on lambda [CLK-239123]
1 parent c91bbbd commit 9c75c91

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

docs/interfaces/UploadProps.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- [fileName](UploadProps.md#filename)
1111
- [path](UploadProps.md#path)
1212
- [prune](UploadProps.md#prune)
13+
- [role](UploadProps.md#role)
1314

1415
## Properties
1516

@@ -50,3 +51,15 @@ Whether or not to clear out the destination directory before uploading.
5051
**`Default`**
5152

5253
false
54+
55+
___
56+
57+
### role
58+
59+
`Optional` `Readonly` **role**: `IRole`
60+
61+
Used as the Lambda Execution Role for the BucketDeployment.
62+
63+
**`Default`**
64+
65+
- role is created automatically by the Construct

src/generator.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Ajv, { SchemaObject } from 'ajv';
2+
import { IRole } from 'aws-cdk-lib/aws-iam';
23
import { Bucket } from 'aws-cdk-lib/aws-s3';
34
import { BucketDeployment, Source } from 'aws-cdk-lib/aws-s3-deployment';
45
import { Construct } from 'constructs';
@@ -29,6 +30,11 @@ export interface UploadProps {
2930
* @default false
3031
*/
3132
readonly prune?: boolean;
33+
/**
34+
* Used as the Lambda Execution Role for the BucketDeployment.
35+
* @default - role is created automatically by the Construct
36+
*/
37+
readonly role?: IRole;
3238
}
3339

3440
export interface SerializerProps {
@@ -152,6 +158,7 @@ export class Generator extends Construct {
152158
destinationKeyPrefix: this._uploadProps.path,
153159
sources: [Source.jsonData(this._uploadProps.fileName, contents)],
154160
prune: this._uploadProps.prune ?? false,
161+
role: this._uploadProps.role,
155162
});
156163
}
157164
}

test/generator.test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/* eslint-disable dot-notation */
22
// ^ Helps us test private properties like functions and fields
3-
import { App, Stack } from 'aws-cdk-lib';
3+
import { App, CfnElement, Stack } from 'aws-cdk-lib';
44
import { Template } from 'aws-cdk-lib/assertions';
5+
import { Role, ServicePrincipal } from 'aws-cdk-lib/aws-iam';
56
import { schema } from './resources/test.schema';
67
import { Generator, GeneratorFileType, GeneratorProps } from '../src';
78

@@ -86,5 +87,28 @@ describe('Generator', () => {
8687
Prune: true,
8788
});
8889
});
90+
91+
it('uses custom Lambda execution role when set', () => {
92+
const testRole = new Role(stack, 'TestExecutionRole', { assumedBy: new ServicePrincipal('test.amazonaws.com') });
93+
new Generator(stack, 'Generator', {
94+
...actualProps,
95+
upload: {
96+
...actualProps.upload,
97+
role: testRole,
98+
},
99+
});
100+
const template = Template.fromStack(stack);
101+
const lambda = template.findResources('AWS::Lambda::Function');
102+
const lambdaId = Object.keys(lambda)[0];
103+
// Verify BucketDeployment uses the Lambda with role set
104+
template.hasResourceProperties('Custom::CDKBucketDeployment', {
105+
ServiceToken: { 'Fn::GetAtt': [lambdaId, 'Arn'] },
106+
});
107+
// Verify Lambda uses the role
108+
const roleId = stack.getLogicalId(testRole.node.defaultChild as CfnElement);
109+
template.hasResourceProperties('AWS::Lambda::Function', {
110+
Role: { 'Fn::GetAtt': [roleId, 'Arn'] },
111+
});
112+
});
89113
});
90114
});

0 commit comments

Comments
 (0)