0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Amazon EventBridge Rule の IAM ロールで Lambda を呼び出す

Posted at

今回のアップデートのポイント

従来

従来は EventBridge がターゲット(Lambda関数、SNSトピック、SQSキューなど)を呼び出す際には、ターゲット側のリソースベースポリシーで制御する必要がありました。

この場合、複数の Lambda が存在していた場合、各 Lambda ごとにリソースベースポリシーを設定する必要があります。

CloudFormation で作成していた場合、Lambda の数だけ AWS::Lambda::Permission を書かなければいけないイメージです。

アップデート(推奨)

今回のアップデート内容で、EventBridge の IAM ロールを使用してターゲットを実行できるようになりました。そのため、ひとつの IAM ロールを利用して複数の Lambda を呼び出すような構成にすることが可能です。

やってみる

実装

せっかくなので、CloudFormation で実装してみます。Lambda関数はひとつですが、EventBridge用のIAMロールで複数の Lambda関数を呼び出すように記載してあげれば良いです。 AWS::Lambda::Permissionはありません。

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: SAM template for EventBridge Rule triggering Lambda function

Parameters:
  NameTest:
    Type: String
    Default: EventBridge_Rule_Test

Resources:
  # Lambda関数
  LambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: !Ref NameTest
      Handler: index.lambda_handler
      Runtime: python3.10
      InlineCode: |
        import json
        
        def lambda_handler(event, context):
            # TODO implement
            print({'statusCode': 200})
            return {
                'statusCode': 200,
                'body': json.dumps('Hello from Lambda!')
            }

  # EventBridge用のIAMロール
  EventBridgeIamRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: !Sub "${NameTest}-EventBridgeRole"
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Sid: Events
            Effect: Allow
            Principal:
              Service: events.amazonaws.com
            Action: sts:AssumeRole
            Condition:
              StringEquals:
                aws:SourceAccount: !Ref AWS::AccountId
                aws:SourceArn: !Sub arn:aws:events:${AWS::Region}:${AWS::AccountId}:rule/${NameTest}
      Policies:
        - PolicyName: LambdaInvoke
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Action:
                  - lambda:InvokeFunction
                Resource:
                  - !GetAtt LambdaFunction.Arn

  # EventBridgeルール
  EventsRule:
    Type: AWS::Events::Rule
    Properties:
      Name: !Ref NameTest
      EventPattern: 
        source:
          - custom.source
        detail-type:
          - TestEvent
        detail:
          key1:
            - value1
      State: ENABLED
      EventBusName: default
      Targets:
        - Id: LambdaInvoke
          Arn: !GetAtt LambdaFunction.Arn
          RoleArn: !GetAtt EventBridgeIamRole.Arn

動作確認

EventBridge からテスト送信してみます。

スクリーンショット 2025-04-17 20.41.58.png

Lambda が実行されたか確認してみます。

スクリーンショット 2025-04-17 20.54.33.png

成功です!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?