Gathering detailed insights and metrics for serverless-iam-roles-per-function-v4
Gathering detailed insights and metrics for serverless-iam-roles-per-function-v4
Gathering detailed insights and metrics for serverless-iam-roles-per-function-v4
Gathering detailed insights and metrics for serverless-iam-roles-per-function-v4
Serverless framework plugin to manage IAM roles. Compatible with serverless-aws-alias-v4 and AWS
npm install serverless-iam-roles-per-function-v4
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
20 Commits
1 Watchers
2 Branches
1 Contributors
Updated on May 08, 2025
Latest Version
0.2.3-rc
Package Id
serverless-iam-roles-per-function-v4@0.2.3-rc
Unpacked Size
41.05 kB
Size
11.76 kB
File Count
4
NPM Version
10.9.2
Node Version
22.15.0
Published on
May 08, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
serverless-iam-roles-per-function-v4
Serverless framework plugin to manage IAM roles. Compatible with serverless-aws-alias-v4 and AWS
This plugin facilitates the management of your IAM roles across different stages with or without function aliases.
Key features:
THIS PLUGIN IS CURRENTLY IN RELEASE CANDIDATE STATUS. PLEASE EXERCISE CAUTION WHEN USING IT IN PRODUCTION ENVIRONMENTS.
1npm install --save-dev serverless-iam-roles-per-function-v4
Add the plugin to your serverless.yml
file:
1plugins: 2 - serverless-iam-roles-per-function-v4
This plugin must be placed after serverless-aws-alias-v4 in your plugins list.
Example:
1plugins: 2 - serverless-esbuild # Must be the first 3 - serverless-aws-alias-v4 # Must be before serverless-iam-roles-per-function-v4 4 - serverless-iam-roles-per-function-v4 5 - serverless-offline # Must be the last
All roles includes permissions for creating and writing to CloudWatch logs, handling stream events, and if a VPC is defined, the AWSLambdaVPCAccessExecutionRole
policy will be included.
Define your IAM role statements at the provider-level:
1provider: 2 name: aws 3 iam: 4 role: 5 statements: 6 - Effect: "Allow" 7 Action: 8 - xray:PutTelemetryRecords 9 - xray:PutTraceSegments 10 Resource: "*"
The plugin creates a dedicated IAM role for each function that has an iamRoleStatements
definition.
1functions: 2 func1: 3 handler: handler.get 4 iamRoleStatementsName: my-custom-role-name # Optional custom name instead of using the default generated one 5 iamRoleStatements: 6 - Effect: "Allow" 7 Action: 8 - dynamodb:GetItem 9 Resource: "arn:aws:dynamodb:${self:provider.region}:*:table/name" 10 ... 11 func2: 12 handler: handler.put 13 iamRoleStatements: 14 - Effect: "Allow" 15 Action: 16 - dynamodb:PutItem 17 Resource: "arn:aws:dynamodb:${self:provider.region}:*:table/name" 18 ...
If iamRoleStatements
are not defined at the function level, the default behavior is maintained and the function will use the global IAM role. You can also define an empty iamRoleStatements
array for a function, which will give the function a dedicated role with only the essential permissions needed for CloudWatch and, if applicable, stream events and VPC access.
Below is an example of a function with empty iamRoleStatements
and a configured VPC. This function will receive a custom role with CloudWatch logs permissions and the AWSLambdaVPCAccessExecutionRole
policy:
1functions: 2 func1: 3 handler: handler.get 4 iamRoleStatements: [] 5 vpc: 6 securityGroupIds: 7 - sg-xxxxxx 8 subnetIds: 9 - subnet-xxxx 10 - subnet-xxxxx
By default, function-level iamRoleStatements
override any provider-level definitions. However, you can inherit the provider-level definitions by adding the option iamRoleStatementsInherit: true
to your function configuration:
1provider: 2 name: aws 3 iam: 4 role: 5 statements: 6 - Effect: "Allow" 7 Action: 8 - xray:PutTelemetryRecords 9 - xray:PutTraceSegments 10 Resource: "*" 11 ... 12functions: 13 func1: 14 handler: handler.get 15 iamRoleStatementsInherit: true 16 iamRoleStatements: 17 - Effect: "Allow" 18 Action: 19 - dynamodb:GetItem 20 Resource: "arn:aws:dynamodb:${self:provider.region}:*:table/name"
The role generated for func1
will include both the statements defined at the provider level and those defined at the function level.
If you want to change the default behavior from override
to inherit
for all functions, you can specify the following custom configuration:
1custom: 2 serverless-iam-roles-per-function-v4: 3 defaultInherit: true
The plugin adopts a normalized naming convention for function roles. Function roles follow this naming pattern:
1<FunctionName><StageOrAlias><Region>LambdaRole
Where:
This naming convention ensures that:
AWS imposes a 64-character limit on role names. If the generated name exceeds this limit, the plugin will throw an error with a descriptive message suggesting that you use a custom role name.
To avoid exceeding the 64-character limit, you have two options:
iamRoleStatementsName
property1functions: 2 func1: 3 handler: handler.get 4 iamRoleStatementsName: my-custom-role-name 5 iamRoleStatements: 6 - Effect: "Allow" 7 Action: 8 - dynamodb:GetItem 9 Resource: "arn:aws:dynamodb:${self:provider.region}:*:table/name" 10 ...
shortenFunctionNames
option in your custom configuration, which will automatically shorten function names in role generation:1custom: 2 serverless-iam-roles-per-function-v4: 3 shortenFunctionNames: true
When role names exceed the 64-character AWS limit, the plugin employs a two-step shortening process:
LambdaRole
suffix from the nameshortenFunctionNames
is enabled, it will then shorten the function name portionThis approach helps maintain meaningful role names while staying within AWS limits.
This plugin automatically detects if you're using the serverless-aws-alias-v4 plugin and adjusts its behavior accordingly:
You can define permission boundaries at the function level using the iamPermissionsBoundary
property:
1functions: 2 func1: 3 handler: handler.get 4 iamPermissionsBoundary: !Sub arn:aws:iam::xxxxx:policy/your_permissions_boundary_policy 5 iamRoleStatementsName: my-custom-role-name 6 iamRoleStatements: 7 - Effect: "Allow" 8 Action: 9 - sqs:* 10 Resource: "*" 11 ...
You can set a permissions boundary for all roles by using the iamGlobalPermissionsBoundary
property in the custom
section:
1custom: 2 serverless-iam-roles-per-function-v4: 3 iamGlobalPermissionsBoundary: !Sub arn:aws:iam::xxxx:policy/permissions-boundary-policy
For more information, see Permissions Boundaries.
By default, this plugin overwrites existing IAM roles during deployment with the same name. You can change this behavior with the overwriteExistingRoles
configuration option:
1custom: 2 serverless-iam-roles-per-function-v4: 3 overwriteExistingRoles: false
When overwriteExistingRoles
is set to:
true
(default): The plugin creates or updates IAM roles for all functions. Any existing roles with the same name will be overwritten with the new definition.false
: The plugin only creates new roles if they don't already exist. Existing roles are left untouched, and functions will use these existing roles as-is.This option is particularly useful when:
When using this plugin with serverless-aws-alias-v4, IAM roles are preserved by default during stack updates and even when the CloudFormation stack is deleted. This preservation behavior, managed by the preserveRoles
option, ensures:
This approach is particularly useful for multi-stage/alias deployments where you want to maintain separate but persistent IAM roles for each stage or alias.
You can control this behavior with the preserveRoles
configuration:
1custom: 2 serverless-iam-roles-per-function-v4: 3 preserveRoles: false # Disables role preservation with or without `serverless-aws-alias-v4`
Setting preserveRoles: false
will make roles follow the standard CloudFormation lifecycle, meaning they will be deleted when the stack is deleted or when they're removed from the template.
[!NOTE] When roles are preserved, you need to manually delete them from the AWS console or CLI if you want to completely clean up resources after deleting a stack.
When deploying to multiple stages with commands like:
1serverless deploy --stage development 2serverless deploy --stage staging 3serverless deploy --stage production
The plugin creates separate roles for each stage:
MyFunctionDevelopmentUsEastLambdaRole
MyFunctionStagingUsEastLambdaRole
MyFunctionProductionUsEastLambdaRole
Each role has the permissions defined for that function. When you update a function's permissions and redeploy to the same stage, the role is updated rather than replaced, preserving its ARN and any resources that reference it.
The plugin automatically deduplicates IAM policy statements to prevent redundant permissions. This helps:
Deduplication occurs across all policy types, including CloudWatch logs, stream access, SQS permissions, and custom statements. Statements are considered duplicates when they have the same Effect, Action, Resource, and Condition properties.
By default, only error messages are displayed. To view detailed logs, use one of these methods:
SLS_DEBUG=*
--verbose
or -v
flag when deploying: sls deploy --verbose
1custom: 2 serverless-iam-roles-per-function-v4: 3 verbose: true
This project is licensed under the MIT License - see the LICENSE.md file for details.
Contributions are welcome! Feel free to submit a pull request or open an issue.
This plugin is inspired by:
No vulnerabilities found.
No security vulnerabilities found.