Serverless functions share an execution role

Review each function’s execution role and policies so it does not inherit permissions needed only by another function.

Description

Give SAM functions separate execution roles when they need different AWS permissions.

Potential impact

A change to a shared role affects every function using it. One function could gain access to another’s resources.

Remediation

For each function, list its required actions and resources. Set Role to a role that Lambda can assume, scope its policy to that list, and test that required calls succeed and unrelated calls are denied.

Examples

Set FunctionImage to your ECR image URI and supply existing role ARNs. The examples omit the roles’ policies.

Before

yaml
Transform: AWS::Serverless-2016-10-31
Parameters:
  FunctionImage:
    Type: String
Resources:
  Function1:
    Type: AWS::Serverless::Function
    Properties:
      PackageType: Image
      ImageUri: !Ref FunctionImage
      Role: arn:aws:iam::123456789012:role/lambda-role

  Function2:
    Type: AWS::Serverless::Function
    Properties:
      PackageType: Image
      ImageUri: !Ref FunctionImage
      Role: arn:aws:iam::123456789012:role/lambda-role

After

yaml
Transform: AWS::Serverless-2016-10-31
Parameters:
  FunctionImage:
    Type: String
Resources:
  Function1:
    Type: AWS::Serverless::Function
    Properties:
      PackageType: Image
      ImageUri: !Ref FunctionImage
      Role: arn:aws:iam::123456789012:role/function1-role

  Function2:
    Type: AWS::Serverless::Function
    Properties:
      PackageType: Image
      ImageUri: !Ref FunctionImage
      Role: arn:aws:iam::123456789012:role/function2-role

References