リソースベースポリシー

Lambda関数に直接アタッチするアクセス許可ポリシー。他のAWSアカウント、組織、AWSサービスがその関数を呼び出すことを許可する

出典: Viewing resource-based IAM policies in Lambda

IAMポリシーとの違い

リソースベースポリシーIAMポリシー(実行ロール)
アタッチ先Lambda関数IAMロール/ユーザー
制御対象誰がこの関数を呼び出せるかこの関数が何にアクセスできるか
用途外部からの呼び出し許可関数内部からのAWSリソースアクセス
外部サービス/アカウント → [リソースベースポリシー] → Lambda関数 → [実行ロール] → 他のAWSリソース

主なユースケース

  • S3バケットのイベントでLambdaを起動
  • SNSトピックからLambdaを呼び出し
  • API GatewayからLambdaを呼び出し
  • 別のAWSアカウントからLambdaを呼び出し
  • EventBridgeルールでLambdaをトリガー

Lambda関数の呼び出し方式

出典: Understanding Lambda function invocation methods

“All of these methods are direct ways to invoke your function. In Lambda, a common use case is to invoke your function based on an event that occurs elsewhere in your application. Some services can invoke a Lambda function with each new event. This is called a trigger. For stream and queue-based services, Lambda invokes the function with batches of records. This is called an event source mapping.”

公式ドキュメントでは以下の3つを区別している:

方式説明
直接呼び出し (Direct invocation)コンソール、SDK、CLI、Function URLなどで直接呼び出す
トリガー (Trigger)S3やSNSなどのサービスがイベント発生時にLambdaを呼び出す
イベントソースマッピング (Event Source Mapping)ストリーム/キューからLambdaがバッチでレコードを取得して処理

イベントソースマッピング

Lambdaが管理するリソースで、ストリーム/キューからレコードを読み取り、バッチにまとめてLambda関数を呼び出す

出典: How Lambda processes records from stream and queue-based event sources

“An event source mapping is a Lambda resource that reads items from stream and queue-based services and invokes a function with batches of records. Within an event source mapping, resources called event pollers actively poll for new messages and invoke functions.”

トリガーとの違い

出典: How Lambda processes records from stream and queue-based event sources

“Event source mappings differ from direct triggers, which are used by services like Amazon S3, Amazon Simple Notification Service (Amazon SNS), and Amazon API Gateway to invoke Lambda functions directly. Triggers are suitable for discrete events and real-time processing.”

観点トリガーイベントソースマッピング
呼び出し方向サービス → Lambda(プッシュ)Lambda → サービス(ポーリング)
処理単位個別イベント(discrete events)複数レコードをバッチで処理
用途単発イベント、リアルタイム処理大量ストリーム/キュー処理
管理場所イベント発生元サービスLambda
【トリガー】
┌─────────┐   イベント発生   ┌─────────┐
│   S3    │ ───────────────▶ │ Lambda  │
│         │   (プッシュ型)   │         │
└─────────┘                  └─────────┘

【イベントソースマッピング】
┌─────────┐                  ┌─────────────────────┐      ┌─────────┐
│   SQS   │ ◀────ポーリング──│ Event Source Mapping│ ───▶ │ Lambda  │
│ (キュー)│                  │   (Lambdaが管理)    │      │         │
└─────────┘                  └─────────────────────┘      └─────────┘

対応サービス

出典: How Lambda processes records from stream and queue-based event sources

  • Amazon SQS
  • Amazon Kinesis Data Streams
  • Amazon DynamoDB Streams
  • Amazon MSK / Self-managed Apache Kafka
  • Amazon MQ
  • Amazon DocumentDB

バッチ処理の仕組み

出典: How Lambda processes records from stream and queue-based event sources

“By default, an event source mapping batches records together into a single payload that Lambda sends to your function.”

以下のいずれかの条件を満たすとLambdaが呼び出される:

出典: How Lambda processes records from stream and queue-based event sources

“Lambda invokes your function when one of the following three criteria is met: The batching window reaches its maximum value… The batch size is met… The payload size reaches 6 MB.”

  1. バッチウィンドウ(最大待機時間)に達した
  2. バッチサイズ(最大レコード数)に達した
  3. ペイロードサイズが6MBに達した

バッチサイズ

1回の呼び出しで処理するレコードの最大数

出典: EventSourceMappingConfiguration - AWS Lambda

“BatchSize: The maximum number of records in each batch that Lambda pulls from a stream or queue and sends to the function.”

イベントソース別のデフォルト値と上限

イベントソースデフォルト最大
SQS (標準)1010,000
SQS (FIFO)1010
Kinesis10010,000
DynamoDB Streams10010,000
Amazon MSK / Kafka10010,000

関連パラメータ

パラメータ説明
BatchSize1回で処理する最大レコード数
MaximumBatchingWindowInSecondsバッチを溜める最大待機時間(秒)

注意点

出典: How Lambda processes records from stream and queue-based event sources

“Lambda event source mappings process each event at least once, and duplicate processing of records can occur. To avoid potential issues related to duplicate events, we strongly recommend that you make your function code idempotent.”

  • 少なくとも1回処理される(重複の可能性あり)
  • 関数は冪等(べきとう)に実装すべき

ReportBatchItemFailures(部分バッチ失敗レポート)

バッチ処理で一部のレコードだけ失敗した場合、失敗したレコードのみを再処理させる機能

デフォルトの動作

出典: Configuring partial batch response with Kinesis Data Streams and Lambda

“When consuming and processing streaming data from an event source, by default Lambda checkpoints to the highest sequence number of a batch only when the batch is a complete success. Lambda treats all other results as a complete failure and retries processing the batch up to the retry limit.”

  • バッチ全体が成功した場合のみチェックポイントを進める
  • 1件でも失敗すると、バッチ全体を再処理

ReportBatchItemFailuresを有効にすると

出典: Configuring partial batch response with Kinesis Data Streams and Lambda

“To allow for partial successes while processing batches from a stream, turn on ReportBatchItemFailures. Allowing partial successes can help to reduce the number of retries on a record, though it doesn’t entirely prevent the possibility of retries in a successful record.”

  • 失敗したレコードのみを再処理
  • 成功したレコードは再処理されない

対応サービス

出典: Batch Processing - Lambda Powertools Python

“The AWS documentation outlines the use of the Batch Processing utility for handling partial failures when processing batches from Amazon SQS, Amazon Kinesis Data Streams, and Amazon DynamoDB Streams using Lambda functions.”

  • Kinesis Data Streams
  • DynamoDB Streams
  • SQS