説明
必要な AWS 権限が異なる SAM 関数には、それぞれに実行ロールを割り当てます。
想定される影響
共有ロールの変更は、そのロールを使うすべての関数に反映されます。ある関数から別の関数のリソースにアクセスできる場合があります。
対処方法
関数ごとに必要なアクションとリソースを整理します。Role に Lambda が引き受けられるロールを指定し、その範囲にポリシーを絞ります。必要な呼び出しが成功し、関係のない呼び出しが拒否されることを確認します。
例
FunctionImage に利用する ECR イメージの URI を指定し、既存のロールの ARN を使います。例ではロールのポリシーを省略しています。
変更前
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
変更後
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