kakakakakku blog

Weekly Tech Blog: Keep on Learning!

Step Functions から直接 Bedrock を呼び出す

最近 AWS Step Functions から AWS Lambda 関数を使わずに Amazon Bedrock を呼び出す (invokeModel) という検証をする機会があって簡単にまとめておく📝ちなみに AWS Lambda 関数を使わずに呼び出すことを直接統合 (Direct Integrations) と言ったりする.

👾 template.yaml

今回は AWS SAM を使う.AWS SAM で AWS Step Functions をデプロイするときに DefinitionUri プロパティで Amazon States Language ファイル (.asl.json) を参照することもできるけど,今回は Definition プロパティに設定することにした👌

AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31

Resources:
  StateMachine:
    Type: AWS::Serverless::StateMachine
    Properties:
      Name: sandbox-invoke-model
      Role: !GetAtt StateMachineRole.Arn
      Definition:
        StartAt: InvokeModel
        States:
          InvokeModel:
            Type: Task
            Resource: arn:aws:states:::bedrock:invokeModel
            Parameters:
              ModelId: !Sub arn:aws:bedrock:${AWS::Region}:${AWS::AccountId}:inference-profile/global.anthropic.claude-sonnet-4-5-20250929-v1:0
              Body:
                anthropic_version: bedrock-2023-05-31
                max_tokens: 500
                messages:
                  - role: user
                    content:
                      - type: text
                        text.$: $.prompt
            Next: Succeed
          Succeed:
            Type: Succeed
  StateMachineRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Action: sts:AssumeRole
            Principal:
              Service:
                - states.amazonaws.com
      Policies:
        - PolicyName: Bedrock
          PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Effect: Allow
                Action:
                  - bedrock:InvokeModel
                Resource:
                  - arn:aws:bedrock:*:*:inference-profile/*
                  - arn:aws:bedrock:*::foundation-model/*

まず AWS Step Functions と Amazon Bedrock を統合するために arn:aws:states:::bedrock:invokeModel リソースを使う.

docs.aws.amazon.com

必要なパラメータとして ModelIdBody を設定する.ModelId には Claude Sonnet 4.5 の推論プロファイルを設定した.Body には Anthropic Claude Messages API の仕様に沿って anthropic_version / max_tokens / messages を設定した.プロンプトは AWS Step Functions を実行するときに指定する😀

docs.aws.amazon.com

AWS Step Functions の IAM Role では推論プロファイルと基盤モデルに対する bedrock:InvokeModel を許可しておいた✅️

動作確認

今回は ap-northeast-1 リージョンにデプロイして AWS Step Functions を実行する.

$ aws stepfunctions start-execution \
  --state-machine-arn arn:aws:states:ap-northeast-1:000000000000:stateMachine:sandbox-invoke-model \
  --input '{"prompt": "日本で一番高い山は?"}'
{
    "executionArn": "arn:aws:states:ap-northeast-1:000000000000:execution:sandbox-invoke-model:e2f17d64-1b8d-4078-a977-b11aabcfbc67",
    "startDate": "2025-11-14T09:38:03.460000+00:00"
}

以下のような出力になっていた👌

{
  "Body": {
    "model": "claude-sonnet-4-5-20250929",
    "id": "msg_bdrk_01G84UkuCGxHxpYqygZmLWEy",
    "type": "message",
    "role": "assistant",
    "content": [
      {
        "type": "text",
        "text": "日本で一番高い山は**富士山**です。\n\n標高は**3,776メートル**で、静岡県と山梨県にまたがっています。日本の象徴としても知られ、2013年には世界文化遺産に登録されました。"
      }
    ],
    "stop_reason": "end_turn",
    "stop_sequence": null,
    "usage": {
      "input_tokens": 18,
      "cache_creation_input_tokens": 0,
      "cache_read_input_tokens": 0,
      "output_tokens": 79
    }
  },
  "ContentType": "application/json"
}

AWS Serverless Patterns Collection

実は AWS Serverless Patterns Collection にも AWS Step Functions to Amazon Bedrock Integration という同じようなサンプルがある😀違うのは AWS Step Functions の Express ワークフローを使っているところ.

ついでに試してみたら Amazon Bedrock で Claude 2.1 を使っていてまったく動かないサンプルになっていた🔥ということで Claude Sonnet 4.5 を使うように修正してみたので merge してもらえるとイイな \( 'ω')/

github.com