Feature Request : per-function IAM policies

Many AWS solution architects recommend setting up per-function IAM policies so that we follow the principle of least privilege and give each function only the permission it needs.

However, at the moment, it’s quite clumsy to add custom roles for each function (requires setting up the role as custom CF resource, and have to manually include the permissions the Serverless framework adds for you out by default, eg. CWLogs), and as such I don’t know of anyone who actually do this in production.

The framework should make it easy to set this up, in the same way that you can specify settings at the provider level (memory, timeout, etc.) and override them at a function level. I’m thinking something along the lines of:

service: new-service

provider:
  name: aws
  iamRoleStatements:
    -  Effect: "Allow"
       Action:
         - "s3:ListBucket"
       Resource:
         Fn::Join:
           - ""
           - - "arn:aws:s3:::"
             - Ref: ServerlessDeploymentBucket

functions:
  func0:
    ...
    iamRoleStatements:
      -  Effect: "Allow"
         Action:
           - "s3:PutObject"
         Resource:
           Fn::Join:
             - ""
             - - "arn:aws:s3:::"
               - Ref: ServerlessDeploymentBucket
               - "/*"
    func1:
      ...
    func2:
      ...

In the above example, func1 and func2 would inherit the IAM role created at the service level, with the permission for s3:ListBuckets. But since func0 specifies its own IAM statement, these statements should be concatenated with the service level IAM statements to create a new role, and assigned to the func0 function, giving it permissions for both s3:ListBuckets and s3:PutObject.

4 Likes

Very much agree here. Especially since functions are supposed to be able to take-over any provider-level parameters.

Is there a roadmap with serverless features that are planned?

1 Like

Yeah the framework does need this.

@horike37 what do you think?

Thank you for pinging @DavidWells :smile:
just commented it on https://github.com/serverless/serverless/issues/4313#issuecomment-349614369.

I’ve just put together a new plugin, implementing support for iamRoleStatements at the function level. More details at: https://github.com/functionalone/serverless-iam-roles-per-function . Feedback and comments are welcome.

3 Likes