📝

EventBridge ルールから Step Functions ステートマシンを呼び出す際の JSON 形式について

に公開

EventBridge ルールの入力トランスフォーマーでは JSON がエスケープされないためステートマシンを呼び出せずに以下のエラーが発生しました。

Invalid input for target.

失敗するパターン

以下の定義でステートマシンを作成しました。

{
  "Comment": "EventBridge test state machine",
  "StartAt": "LogInput",
  "States": {
    "LogInput": {
      "Type": "Pass",
      "Result": "State machine executed successfully",
      "End": true
    }
  }
}

EventBridge ルールのイベントパターンは以下の通りです。

{
  "source": ["test.eventbridge"],
  "detail-type": ["Test Event"]
}

EventBridge ルールのターゲットには上記のステートマシンを指定します。
ターゲットの入力トランスフォーマーの設定は以下の通りです。

  • 入力パス
{
  "id": "$.detail.id",
  "resources": "$.detail.resources"
}
  • 入力テンプレート
{
  "message": "リソース詳細:<resources>"
}

上記設定で EventBridge ルールに配信されたイベントからリソース情報を抽出して message という JSON キーの値として設定する想定です。

エラーメッセージを確認できるように EventBridge ルールのターゲットにはデッドレターキューも設定しました。

イベントバスから以下のテストイベントを送信してみます。

  • イベントバス: dafault
  • イベントソース: test.eventbridge.normal
  • 詳細タイプ: Test Event Normal
  • イベントの詳細
{
  "resources": {
    "Type": "AwsS3",
    "Id": "arn:aws:s3:::my-bucket"
  },
  "id": "test-123"
}

イベントバスからは以下のようなイベントが送信されます。

{
  "version": "0",
  "id": "ac90d32d-9372-ecb1-df46-ba5bcf8a91c8",
  "detail-type": "Test Event Normal",
  "source": "test.eventbridge.normal",
  "account": "012345678901",
  "time": "2025-11-20T04:35:46Z",
  "region": "ap-northeast-1",
  "resources": [],
  "detail": {
    "resources": {
      "Type": "AwsS3",
      "Id": "arn:aws:s3:::my-bucket"
    },
    "id": "test-123"
  }
}

しかし、上記の入力トランスフォーマーの設定ではステーマシンは実行されず、デッドレターキューに以下のメッセージが配信されます。

INVALID_JSON
Invalid input for target.

上記エラーの原因は入力トランスフォーマーで JSON に含まれるダブルクォーテーションをエスケープできず、不正な JSON になっていることです。

{
  "message": "リソース詳細:{"Type": "AwsS3", "Id": "arn:aws:s3:::my-bucket"}"
}

上記事象を解消するには Lambda 関数でエスケープ処理を実行する必要があります。

成功するパターン

失敗するパターンを踏まえて以下の構成で試してみます。

  • EventBridge ルール → Lambda 関数 → Step Functions ステートマシン

EventBridge ルールのイベントパターンと Step Functions ステートマシンの設定は失敗するパターンと同じ設定です。

Lambda 関数については以下の設定で作成しました。

  • ランタイム: Python 3.14
  • 実行ロール: AdministratorAccess 権限を付与
  • コード: 以下の通り
import json
import boto3

stepfunctions = boto3.client('stepfunctions')

STATE_MACHINE_ARN = 'my-state-machine-arn'

def lambda_handler(event, context):
    print(f"Received event: {json.dumps(event)}")
    
    resources = event.get('detail', {}).get('resources', {})
    print(f"Extracted resources: {json.dumps(resources)}")
    
    resources_str = json.dumps(resources)
    print(f"Serialized resources: {resources_str}")
    
    formatted_message = {
        "message": f"リソース詳細:{resources_str}"
    }
    
    print(f"Formatted message: {json.dumps(formatted_message)}")
    
    print(f"Calling Step Functions with input: {json.dumps(formatted_message)}")
    
    response = stepfunctions.start_execution(
        stateMachineArn=STATE_MACHINE_ARN,
        input=json.dumps(formatted_message)
    )
    
    print(f"Step Functions execution started: {response['executionArn']}")
    
    return {
        'statusCode': 200,
        'body': f"State machine execution started: {response['executionArn']}"
    }

EventBridge ルールのターゲットには上記 Lambda 関数を指定します。

イベントバスから失敗するパターンと同じイベントを送信します。
イベントの送信後、Lambda 関数とステートマシンが実行されていることを確認できます。

Lambda 関数のログには以下のような内容が出力されていました。

Received event: 
{
    "version": "0",
    "id": "ac90d32d-9372-ecb1-df46-ba5bcf8a91c8",
    "detail-type": "Test Event Normal",
    "source": "test.eventbridge.normal",
    "account": "012345678901",
    "time": "2025-11-20T04:35:46Z",
    "region": "ap-northeast-1",
    "resources": [],
    "detail": {
        "resources": {
            "Type": "AwsS3",
            "Id": "arn:aws:s3:::my-bucket"
        },
        "id": "test-123"
    }
}

Extracted resources: 
{
    "Type": "AwsS3",
    "Id": "arn:aws:s3:::my-bucket"
}

Serialized resources: 
{
    "Type": "AwsS3",
    "Id": "arn:aws:s3:::my-bucket"
}

Formatted message: 
{
    "message": "リソース詳細:{\"Type\": \"AwsS3\", \"Id\": \"arn:aws:s3:::my-bucket\"}"
}

Calling Step Functions with input: 
{
    "message": "リソース詳細:{\"Type\": \"AwsS3\", \"Id\": \"arn:aws:s3:::my-bucket\"}"
}

ステートマシンでは以下の入力を受け取っていました。

{
  "message": "リソース詳細:{\"Type\": \"AwsS3\", \"Id\": \"arn:aws:s3:::my-bucket\"}"
}

Lambda 関数でイベントを処理したことで JSON に含まれるダブルクォーテーションがエスケープされ、正しい JSON 形式になっています。
そのため、ステーマシンを呼び出す際に「Invalid input for target」は発生しません。

まとめ

今回は EventBridge ルールから Step Functions ステートマシンを呼び出す際の JSON 形式について紹介しました。
どなたかの参考になれば幸いです。

Discussion