Architect's Log

I'm a Cloud Architect. I'm highly motivated to reduce toils with driving DevOps.

Today I Learned - 2026-06-02

Today I Learned (TIL) の投稿。今日学んだことを雑多に書く。

Lambda関数からステートマシンを起動する

import boto3
import json
import os

sfn = boto3.client('stepfunctions')

# 環境変数から取得
STATE_MACHINE_ARN = os.environ['STATE_MACHINE_ARN']

def lambda_handler(event, context):
    # イベントからパラメータを受け取る
    input_payload = {
        "instanceType": event['instanceType'],
        "clusterId": event['clusterId'],
        # その他の必要なパラメータ
        ... 
    }

    response = sfn.start_execution(
        stateMachineArn=STATE_MACHINE_ARN,
        input=json.dumps(input_payload)
    )

    return {
        'statusCode': 200,
        'executionArn': response['executionArn']
    }

Lambda 関数からステートマシンを起動するためには、Lambda 関数に適切な IAM ロールを割り当てる必要がある。以下は IAM ポリシーの例。

{
  "Effect": "Allow",
  "Action": "states:StartExecution",
  "Resource": "arn:aws:states:ap-northeast-1:123456789:stateMachine:AddRdsReader"
}