Lambda invocation principal uses a wildcard

Specify the intended service or account in Lambda invocation permissions.

Description

A wildcard Principal in AWS::Lambda::Permission does not restrict the caller by name. Check SourceArn and SourceAccount in the same statement to determine its scope.

Potential impact

If the source conditions are also too broad, unintended invocations may increase costs or affect data the function processes.

Remediation

Specify the required service or account. For AWS services, apply the supported source conditions and verify that only the intended events run the function.

Examples

The second excerpt restricts Principal to S3. Both reference function and bucket and limit invocations to that bucket and the stack’s account.

Before

yaml
AWSTemplateFormatVersion: "2010-09-09"
Description: Grants S3 permission to invoke Lambda
Resources:
  s3Permission:
    Type: AWS::Lambda::Permission
    Properties:
      FunctionName: !GetAtt function.Arn
      Action: lambda:InvokeFunction
      Principal: "*"
      SourceAccount: !Ref "AWS::AccountId"
      SourceArn: !GetAtt bucket.Arn

After

yaml
AWSTemplateFormatVersion: "2010-09-09"
Description: Grants S3 permission to invoke Lambda
Resources:
  s3Permission:
    Type: AWS::Lambda::Permission
    Properties:
      FunctionName: !GetAtt function.Arn
      Action: lambda:InvokeFunction
      Principal: s3.amazonaws.com
      SourceAccount: !Ref "AWS::AccountId"
      SourceArn: !GetAtt bucket.Arn

References