
Product
Socket for Jira Is Now Available
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.
serverless-iam-roles-per-function-v4
Advanced tools
Serverless framework plugin to manage IAM roles. Compatible with serverless-aws-alias-v4 and AWS
serverless-iam-roles-per-function-v4Serverless 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.
npm install --save-dev serverless-iam-roles-per-function-v4
Add the plugin to your serverless.yml file:
plugins:
- serverless-iam-roles-per-function-v4
This plugin must be placed after serverless-aws-alias-v4 in your plugins list.
Example:
plugins:
- serverless-esbuild # Must be the first
- serverless-aws-alias-v4 # Must be before serverless-iam-roles-per-function-v4
- serverless-iam-roles-per-function-v4
- 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:
provider:
name: aws
iam:
role:
statements:
- Effect: "Allow"
Action:
- xray:PutTelemetryRecords
- xray:PutTraceSegments
Resource: "*"
The plugin creates a dedicated IAM role for each function that has an iamRoleStatements definition.
functions:
func1:
handler: handler.get
iamRoleStatementsName: my-custom-role-name # Optional custom name instead of using the default generated one
iamRoleStatements:
- Effect: "Allow"
Action:
- dynamodb:GetItem
Resource: "arn:aws:dynamodb:${self:provider.region}:*:table/name"
...
func2:
handler: handler.put
iamRoleStatements:
- Effect: "Allow"
Action:
- dynamodb:PutItem
Resource: "arn:aws:dynamodb:${self:provider.region}:*:table/name"
...
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:
functions:
func1:
handler: handler.get
iamRoleStatements: []
vpc:
securityGroupIds:
- sg-xxxxxx
subnetIds:
- subnet-xxxx
- 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:
provider:
name: aws
iam:
role:
statements:
- Effect: "Allow"
Action:
- xray:PutTelemetryRecords
- xray:PutTraceSegments
Resource: "*"
...
functions:
func1:
handler: handler.get
iamRoleStatementsInherit: true
iamRoleStatements:
- Effect: "Allow"
Action:
- dynamodb:GetItem
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:
custom:
serverless-iam-roles-per-function-v4:
defaultInherit: true
The plugin adopts a normalized naming convention for function roles. Function roles follow this naming pattern:
<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 propertyfunctions:
func1:
handler: handler.get
iamRoleStatementsName: my-custom-role-name
iamRoleStatements:
- Effect: "Allow"
Action:
- dynamodb:GetItem
Resource: "arn:aws:dynamodb:${self:provider.region}:*:table/name"
...
shortenFunctionNames option in your custom configuration, which will automatically shorten function names in role generation:custom:
serverless-iam-roles-per-function-v4:
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:
functions:
func1:
handler: handler.get
iamPermissionsBoundary: !Sub arn:aws:iam::xxxxx:policy/your_permissions_boundary_policy
iamRoleStatementsName: my-custom-role-name
iamRoleStatements:
- Effect: "Allow"
Action:
- sqs:*
Resource: "*"
...
You can set a permissions boundary for all roles by using the iamGlobalPermissionsBoundary property in the custom section:
custom:
serverless-iam-roles-per-function-v4:
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:
custom:
serverless-iam-roles-per-function-v4:
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:
custom:
serverless-iam-roles-per-function-v4:
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:
serverless deploy --stage development
serverless deploy --stage staging
serverless deploy --stage production
The plugin creates separate roles for each stage:
MyFunctionDevelopmentUsEastLambdaRoleMyFunctionStagingUsEastLambdaRoleMyFunctionProductionUsEastLambdaRoleEach 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 --verbosecustom:
serverless-iam-roles-per-function-v4:
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:
FAQs
Serverless framework plugin to manage IAM roles. Compatible with serverless-aws-alias-v4 and AWS
We found that serverless-iam-roles-per-function-v4 demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.