
【VPC外リージョンサービス】
Step Functions は、可視化された State Machine(ステートマシン) によって、分散処理や複数のAWSサービスをオーケストレーションするサーバーレスワークフローサービスです。
各 State(ステート) はワークフロー内の処理単位であり、Definition(定義) は AWS States Language(ASL) というJSONベースの言語で記述します。実行ごとに生成される Execution(実行) では、各ステップの進行状況やエラーを可視的に追跡できます。
ワークフローでは、Task ステートで Lambda、ECS、EKS、Batch、Glue、SageMaker などのサービスを呼び出し、実際の処理を行います。Pass ステートでデータをそのまま渡したり、Choice ステートで条件分岐を定義したり、Parallel ステートで並列処理を行うことができます。Map ステートを使えば、配列データに対して繰り返し処理を実行でき、Wait ステートで待機時間を制御したり、Succeed/Fail ステートでワークフローを明示的に終了させることもできます。エラー処理 機能ではRetry(再試行)やCatch(例外捕捉)の動作を細かく設定でき、堅牢なフロー制御を実現します。
ワークフロータイプには、長時間実行や履歴保持に適した Standard Workflow(標準ワークフロー) と、高頻度・短時間処理向けの Express Workflow(エクスプレスワークフロー) の2種類があります。
Step Functions は、SNS/SQS、EventBridge、API Gateway、DynamoDB などともシームレスに統合されており、イベント駆動型やマイクロサービス連携型のシステム構築にも最適です。
サーバーレスETL処理、長時間ジョブの実行、業務フローの自動化などを安全かつ効率的に実現できます。
重要用語
ユースケース
| バッチ処理のワークフロー管理 | 複数のLambdaやバッチジョブを順次・並列に実行するフローを定義し、失敗時のリトライやエラー分岐を含めたバッチ処理を可視化する。 |
| 業務プロセスのオーケストレーション | 申請→承認→処理実行、といった人やシステムが関わる一連の業務プロセスを状態遷移として定義し、自動化と見える化を行う。 |
| 長時間処理の状態管理 | 数分〜数時間かかる外部システムとの連携処理などをステートマシンで管理し、途中状態や再実行を制御できるようにする。 |
ベストプラクティス
| ステートマシンの分割設計 | 大規模ワークフローは複数の小さなステートマシンに分割して管理性を高める。 |
| エラーハンドリングとリトライ | 各ステートにRetry/Catchを設定し、一時的な障害に強いフローにする。 |
| 可視化された実行履歴の活用 | 実行履歴を分析してボトルネックや失敗パターンを特定する。 |
高可用性・バックアップ・リトライ
| 高可用性・バックアップ・リトライ設計のポイント |
|---|
| 【デフォルト】AWS内部で冗長化 ・ワークフローエンジン ・ステートマシン定義の保存 ・実行状態の管理(Execution State) ・実行履歴(Execution History) ・タスクトークン管理 |
| 【自動リトライ】デフォルトでオフ 各ステートにRetryを設定するとエラーが起きたとき自動再実行 |
セキュリティ
| 関連サービス | 設定内容 |
|---|---|
| CloudTrail(操作履歴の記録・監査・追跡) | 【自動記録】 作成・更新・削除・設定変更は自動記録される。(コントロールプレーンAPI) データ操作は追跡できない(データプレーンAPI) |
| Config(リソースの構成状態・設定変更を記録) | 【Configが有効な場合】 ステートマシン設定変更履歴・暗号化/ログ出力設定の準拠評価(対象に限る) |
| GuardDuty(脅威を自動検出) | 【GuardDutyが有効な場合】 ステートマシン変更のAPI異常検知 |
Step Functionsが実行ロールを引き受ける典型的な連携パターン
| 実行ロールを介して連携するサービス | 実行ロールにアタッチするポリシー |
|---|---|
| CloudWatch Logs | logs:PutLogEvents |
| DynamoDB | dynamodb:GetItem dynamodb:PutItem dynamodb:UpdateItem dynamodb:DeleteItem dynamodb:Query dynamodb:Scan |
| S3 | s3:ListBucket s3:GetObject s3:PutObject s3:DeleteObject |
| Lambda | lambda:InvokeFunction |
| SNS | sns:Publish |
| SQS(送信) | sqs:SendMessage |
| SQS(ポーリング) | sqs:ReceiveMessage sqs:DeleteMessage sqs:GetQueueAttributes sqs:ChangeMessageVisibility |
| ECS | ecs:DescribeTasks ecs:RunTask ecs:StopTask iam:PassRole |
| EventBridge(Event Bus) | events:PutEvents |
| EventBridge Scheduler | scheduler:CreateSchedule scheduler:DeleteSchedule |
| API Gateway | execute-api:Invoke |
| Step Functions(ネストされたワークフロー) | states:StartExecution states:DescribeExecution states:StopExecution |
| Kinesis Data Streams | kinesis:PutRecord kinesis:PutRecords |
| Kinesis Data Firehose | firehose:PutRecord firehose:PutRecordBatch |
| Redshift Data API | redshift-data:ExecuteStatement redshift-data:DescribeStatement |
| Backup | backup:StartBackupJob backup:DescribeBackupJob |
| DataBrew | databrew:StartJobRun databrew:DescribeJob |
| Athena | athena:StartQueryExecution athena:GetQueryExecution s3:PutObject s3:GetObject |
| SageMaker | sagemaker:CreateTrainingJob sagemaker:DescribeTrainingJob sagemaker:CreateTransformJob sagemaker:DescribeTransformJob sagemaker:CreateProcessingJob sagemaker:DescribeProcessingJob iam:PassRole |
| Bedrock | bedrock:InvokeModel |
| EMR | emr:AddJobFlowSteps emr:DescribeStep elasticmapreduce:DescribeCluster |
| Batch | batch:SubmitJob batch:DescribeJobs iam:PassRole |
| Glue | glue:StartJobRun glue:GetJobRun glue:GetJobRuns |
| CodePipeline | codepipeline:StartPipelineExecution |
| CodeBuild | codebuild:StartBuild codebuild:BatchGetBuilds |
| X-Ray | xray:PutTraceSegments xray:PutTelemetryRecords |
| MediaConvert | mediaconvert:CreateJob mediaconvert:GetJob |
| Secrets Manager | secretsmanager:GetSecretValue kms:Decrypt |
| SSM Automation | ssm:StartAutomationExecution ssm:GetAutomationExecution |
| SSM Parameter Store | ssm:GetParameter kms:Decrypt |
信頼ポリシー: Step Functions
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "states.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
ログ・監視
| ログ出力先 | ログの種類 |
|---|---|
| CloudWatch Logs | 実行履歴ログ |
標準メトリクス
| メトリクス名 | 説明 |
|---|---|
| ExecutionTime | 実行時間 |
| ExecutionsStarted | 開始された実行数 |
| ConsumedCapacity | 消費キャパシティ |
| ProvisionedBucketSize | プロビジョニング済みバケットサイズ |
| ProvisionedRefillRate | プロビジョニング済み補充率 |
| ExecutionThrottled | スロットルされた実行数 |
| ExecutionsAborted | 中止された実行数 |
| ExecutionsFailed | 失敗した実行数 |
| ExecutionsSucceeded | 成功した実行数 |
| ExecutionsTimedOut | タイムアウトした実行数 |
制限値(固定値/ハードリミット/ソフトリミット)
| 固定値 | 制限値 |
|---|---|
| 実行時間(Standard) | 1年 |
| ステート数/ステートマシン | 25,000 |
| 実行履歴のイベント数 | 25,000 |
| 同時実行数(Express) | 無制限 |
| 状態遷移数(Express) | 無制限 |
| ハードリミット | 制限値 |
|---|---|
| 実行時間(Express) | 5分 |
| 実行履歴の保持期間 | 90日 |
| ペイロードサイズ | 256 KB |
| ソフトリミット | 制限値 |
|---|---|
| ステートマシン数/リージョン | 10,000 |
| 同時実行数(Standard) | 1,000,000 |
| 状態遷移数(Standard) | 5,000/秒 |
AWS CLIのサンプルコード
実行ロールを作成する(信頼ポリシーのファイル名 指定)
stepfunctions-trust-policy.json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "states.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
aws iam create-role \
--role-name StepFunctionsExecutionRole \
--assume-role-policy-document file://stepfunctions-trust-policy.json
実行ロールを表示する
aws iam list-roles
実行ロールを削除する(ロール名 指定)
aws iam list-attached-role-policies \
--role-name StepFunctionsExecutionRole
実行ロールにAWS管理ポリシーをアタッチする(ロール名、ポリシーのARN 指定)
aws iam attach-role-policy \
--role-name StepFunctionsExecutionRole \
--policy-arn arn:aws:iam::aws:policy/AWSLambdaRole
実行ロールからAWS管理ポリシーをデタッチする(ロール名、ポリシーのARN 指定)
aws iam detach-role-policy \
--role-name StepFunctionsExecutionRole \
--policy-arn arn:aws:iam::aws:policy/AWSLambdaRole
ステートマシンを作成する(ステートマシン定義ファイル名 ロール名 指定)

state-machine-definition.json
{
"Comment": "All Step Functions state types example",
"StartAt": "PassState",
"States": {
"PassState": {
"Type": "Pass",
"Comment": "入力をそのまま次に渡す",
"Result": {
"status": "started",
"items": [
{"id": 1, "value": 10},
{"id": 2, "value": 20},
{"id": 3, "value": 30}
]
},
"ResultPath": "$.data",
"Next": "WaitState"
},
"WaitState": {
"Type": "Wait",
"Comment": "3秒間待機",
"Seconds": 3,
"Next": "TaskState"
},
"TaskState": {
"Type": "Task",
"Comment": "Lambda関数を実行",
"Resource": "arn:aws:lambda:ap-northeast-1:123456789012:function:ProcessData",
"ResultPath": "$.taskResult",
"Next": "ChoiceState",
"Catch": [
{
"ErrorEquals": ["States.ALL"],
"Next": "FailState"
}
]
},
"ChoiceState": {
"Type": "Choice",
"Comment": "条件分岐",
"Choices": [
{
"Variable": "$.taskResult.success",
"BooleanEquals": true,
"Next": "ParallelState"
},
{
"Variable": "$.taskResult.success",
"BooleanEquals": false,
"Next": "FailState"
}
],
"Default": "FailState"
},
"ParallelState": {
"Type": "Parallel",
"Comment": "並列処理",
"Branches": [
{
"StartAt": "Branch1Task",
"States": {
"Branch1Task": {
"Type": "Task",
"Resource": "arn:aws:lambda:ap-northeast-1:123456789012:function:ProcessBranch1",
"End": true
}
}
},
{
"StartAt": "Branch2Task",
"States": {
"Branch2Task": {
"Type": "Task",
"Resource": "arn:aws:lambda:ap-northeast-1:123456789012:function:ProcessBranch2",
"End": true
}
}
}
],
"ResultPath": "$.parallelResult",
"Next": "MapState"
},
"MapState": {
"Type": "Map",
"Comment": "配列の各要素を処理",
"ItemsPath": "$.data.items",
"MaxConcurrency": 2,
"Iterator": {
"StartAt": "ProcessItem",
"States": {
"ProcessItem": {
"Type": "Task",
"Resource": "arn:aws:lambda:ap-northeast-1:123456789012:function:ProcessItem",
"End": true
}
}
},
"ResultPath": "$.mapResult",
"Next": "SucceedState"
},
"SucceedState": {
"Type": "Succeed",
"Comment": "正常終了"
},
"FailState": {
"Type": "Fail",
"Comment": "異常終了",
"Error": "ProcessingError",
"Cause": "タスクの処理中にエラーが発生しました"
}
}
}
aws stepfunctions create-state-machine \
--name MyStateMachine \
--definition file://state-machine-definition.json \
--role-arn arn:aws:iam::123456789012:role/StepFunctionsExecutionRole \
--tags Key=Environment,Value=Production
ステートマシーンの一覧を表示する
aws stepfunctions list-state-machines
ステートマシンを表示する(ステートマシーンのARN 指定)
aws stepfunctions describe-state-machine \
--state-machine-arn arn:aws:states:ap-northeast-1:123456789012:stateMachine:MyStateMachine
ステートマシンを削除する(ステートマシーンのARN 指定)
aws stepfunctions delete-state-machine \
--state-machine-arn arn:aws:states:ap-northeast-1:123456789012:stateMachine:MyStateMachine
ステートマシンの実行を開始する(ステートマシーンのARN 指定)
aws stepfunctions start-execution \
--state-machine-arn arn:aws:states:ap-northeast-1:123456789012:stateMachine:MyStateMachine \
--name execution-20241208-001 \
--input '{"orderId": "12345", "amount": 1000}'
ステートマシンの実行を停止する(ステートマシーンのARN 指定)
aws stepfunctions stop-execution \
--execution-arn arn:aws:states:ap-northeast-1:123456789012:execution:MyStateMachine:execution-001 \
--error "ManualStop" \
--cause "手動で停止しました"
ステートマシンの実行一覧を表示する(ステートマシーンのARN 指定)
aws stepfunctions list-executions \
--state-machine-arn arn:aws:states:ap-northeast-1:123456789012:stateMachine:MyStateMachine
ステートマシンの実行の詳細を表示する(ステートマシーンのARN 指定)
aws stepfunctions describe-execution \
--execution-arn arn:aws:states:ap-northeast-1:123456789012:execution:MyStateMachine:execution-001
ステートマシンの実行履歴を表示する(ステートマシーンのARN 指定)
aws stepfunctions get-execution-history \
--execution-arn arn:aws:states:ap-northeast-1:123456789012:execution:MyStateMachine:execution-001
CloudFormationのサンプルコード
Terraformのサンプルコード
料金計算
| 課金項目 | 説明 |
|---|---|
| 状態遷移数 | 実行された状態遷移の数 |
| Express Workflow | Express Workflowの実行回数と実行時間 |