Skip to content

Commit 5a3aadf

Browse files
committed
fix: support per function role with an empty iamRoleStatements clause (issue #9)
1 parent 51367c8 commit 5a3aadf

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ node_modules
33
#dist is only for the package
44
dist
55
#ignore npm pack output
6-
serverless-iam-role-per-function-*.tgz
6+
serverless-iam-roles-per-function-*.tgz
77
#ignore coverage dir
88
coverage
99
.nyc_output

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,22 @@ functions:
4646
...
4747
```
4848

49-
The plugin will create a dedicated role for each function that has an `iamRoleStatements` definition. It will include the permissions for create and write to CloudWatch logs and if VPC is defined: `AWSLambdaVPCAccessExecutionRole` will be included (as is done when using `iamRoleStatements` at the provider level).
49+
The plugin will create a dedicated role for each function that has an `iamRoleStatements` definition. It will include the permissions for create and write to CloudWatch logs, stream events and if VPC is defined: `AWSLambdaVPCAccessExecutionRole` will be included (as is done when using `iamRoleStatements` at the provider level).
50+
51+
if `iamRoleStatements` are not defined at the function level default behavior is maintained and the function will receive the global iam role. It is possible to define an empty `iamRoleStatements` for a function and then the function will receive a dedicated role with only the permissions needed for CloudWatch and (if needed) stream events and VPC. Example of defining a function with empty `iamRoleStatements` and configured VPC. The function will receive a custom role with CloudWatch logs permissions and the policy `AWSLambdaVPCAccessExecutionRole`:
52+
53+
```yaml
54+
functions:
55+
func1:
56+
handler: handler.get
57+
iamRoleStatements: []
58+
vpc:
59+
securityGroupIds:
60+
- sg-xxxxxx
61+
subnetIds:
62+
- subnet-xxxx
63+
- subnet-xxxxx
64+
```
5065

5166
By default, function level `iamRoleStatements` override the provider level definition. It is also possible to inherit the provider level definition by specifying the option `iamRoleStatementsInherit: true`:
5267

src/lib/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class ServerlessIamPerFunctionPlugin {
2929
}
3030

3131
validateStatements(statements: any): void {
32+
if(_.isEmpty(statements)) {
33+
return;
34+
}
3235
const awsPackagePluginName = "AwsPackage";
3336
if(!this.awsPackagePlugin) {
3437
for (const plugin of this.serverless.pluginManager.plugins) {
@@ -145,7 +148,7 @@ class ServerlessIamPerFunctionPlugin {
145148
*/
146149
createRoleForFunction(functionName: string, functionToRoleMap: Map<string, string>) {
147150
const functionObject = this.serverless.service.getFunction(functionName);
148-
if(_.isEmpty(functionObject.iamRoleStatements)) {
151+
if(functionObject.iamRoleStatements === undefined) {
149152
return;
150153
}
151154
if(functionObject.role) {
@@ -197,8 +200,10 @@ class ServerlessIamPerFunctionPlugin {
197200
}
198201
}
199202
//add iamRoleStatements
200-
for (const s of functionObject.iamRoleStatements) {
201-
policyStatements.push(s);
203+
if(_.isArray(functionObject.iamRoleStatements)) {
204+
for (const s of functionObject.iamRoleStatements) {
205+
policyStatements.push(s);
206+
}
202207
}
203208
functionIamRole.Properties.RoleName = functionObject.iamRoleStatementsName || this.getFunctionRoleName(functionName);
204209
const roleResourceName = this.serverless.providers.aws.naming.getNormalizedFunctionName(functionName) + globalRoleName;

0 commit comments

Comments
 (0)