AWS Step Functions とは
Serverless Microservices Orchestration – AWS Step Functions FAQs – Amazon Web Services
AWS Step Functions is a fully managed service that makes it easier to coordinate the components of distributed applications and microservices using visual workflows.
GUI でステップ実行を定義できる AWS のサーバーレスオーケストレーションサービスです。
機能概要
- アプリケーションのコンポーネントを一連のステップとして整理および視覚化するためのグラフィカルコンソールを提供
- 各ステップを自動的にトリガーおよび追跡し、エラーが発生した場合は再試行する
- コードを記述することなくステップを変更および追加できる
- 多くの AWS サービスの API を直接呼び出すことができる
- Amazon States Language でワークフローを定義する
- 2 種類のワークフローが存在する
- Standard ワークフロー: 1 度だけ実行、最大 1 年間実行可能
- Express ワークフロー: 1 度以上実行、最大 5 分間実行可能
- 並列処理、待機処理、条件分岐、ループ処理など、エラーハンドリングなども実装可能
ユースケース
AWS Step Functions Use Cases | Serverless Microservice Orchestration | Amazon Web Services
- Lambda 関数を組み合わせて Web ベースのアプリケーションを構築
- セキュリティインシデント対応の自動化
- データ処理と ETL オーケストレーション
- PDF または画像からデータを抽出して処理するようなメディア処理
メリット
Serverless Microservices Orchestration – AWS Step Functions FAQs – Amazon Web Services
What are the benefits of designing my application using orchestration?
Breaking an application into service components (or steps) ensures that the failure of one component does not bring the whole system down. Each component scales independently and that component may be updated without requiring the entire system to be redeployed after each change.
- アプリケーションのコンポーネントを疎結合にできる
- コンポーネントごとに変更が可能
- コンポーネントの分割で耐障害性があがる
他サービスとの比較
Serverless Microservices Orchestration – AWS Step Functions FAQs – Amazon Web Services
- Step Functions: サービスコンポーネントを調整する場合に使用
- SQS: サービス間のメッセージの送信、保存、受信に使用
- SWF: 外部シグナルの介入やプロセス間でのデータのやり取りなどで使用
- EventBridge: イベントのルーティングで使用
- Lambda: イベントドリブンでのコード実行に使用
Amazon States Language
Amazon States Language を使用して Step Functions ワークフローを定義する - AWS Step Functions
- JSON ベースの構造化言語
- 分岐、エラーなどの状態を定義する
- 拡張子は .asl.json
- 組み込み関数も用意されている
以下は簡単な定義です。
- Type: Succeed のみ
{
"Comment": "A description of my state machine",
"StartAt": "成功",
"States": {
"成功": {
"Type": "Succeed"
}
},
"QueryLanguage": "JSONata"
}
- 単一の Lambda 関数を呼び出す
{
"Comment": "A description of my state machine",
"StartAt": "Lambda Invoke",
"States": {
"Lambda Invoke": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"End": true,
"Arguments": {
"FunctionName": "arn:aws:lambda:ap-northeast-1:012345678901:function:python39:$LATEST"
}
}
},
"QueryLanguage": "JSONata"
}
- 分岐処理
{
"Comment": "Minimal two-way branching process",
"StartAt": "CheckValue",
"States": {
"CheckValue": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.value",
"NumericGreaterThan": 10,
"Next": "HighValue"
}
],
"Default": "LowValue"
},
"HighValue": {
"Type": "Pass",
"Result": "Value is high",
"End": true
},
"LowValue": {
"Type": "Pass",
"Result": "Value is low",
"End": true
}
}
}
- ループ処理
{
"Comment": "Minimal loop process",
"StartAt": "CheckCount",
"States": {
"CheckCount": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.count",
"NumericLessThan": 3,
"Next": "IncrementCount"
}
],
"Default": "End"
},
"IncrementCount": {
"Type": "Pass",
"Parameters": {
"count.$": "$.count + 1"
},
"Next": "CheckCount"
},
"End": {
"Type": "Pass",
"End": true
}
}
}
試してみた
Step Functions を学習するためのチュートリアルとワークショップ - AWS Step Functions
ドキュメントでは複数のチュートリアルが紹介されています。
今回は Lambda を使用するパターンを参考にして 3 つの関数を順番に実行する方法を試してみました。
01. 1 つ目 の Lambda 関数の作成
以下の設定で作成しました。
- ランタイム: Python 3.14
- 実行ロール: AdministratorAccess 権限を付与
- コード: 以下の通り
def lambda_handler(event, context):
message = event.get('message', '')
result = message + ' World'
return {
'message': result
}
02. 2 つ目 の Lambda 関数の作成
以下の設定で作成しました。
- ランタイム: Python 3.14
- 実行ロール: AdministratorAccess 権限を付与
- コード: 以下の通り
def lambda_handler(event, context):
message = event.get('message', '')
result = message + ' From'
return {
'message': result
}
03. 3 つ目 の Lambda 関数の作成
以下の設定で作成しました。
- ランタイム: Python 3.14
- 実行ロール: AdministratorAccess 権限を付与
- コード: 以下の通り
def lambda_handler(event, context):
message = event.get('message', '')
result = message + ' Step Functions'
return {
'message': result
}
04. Step Functions ステートマシンの作成
以下の定義で作成しました。
Lambda 関数の ARN は手順 01 ~ 03 で作成した関数の ARN です。
{
"Comment": "Sequential Lambda execution workflow",
"StartAt": "Lambda1",
"States": {
"Lambda1": {
"Type": "Task",
"Resource": "arn:aws:lambda:ap-northeast-1:012345678901:function:first",
"Next": "Lambda2"
},
"Lambda2": {
"Type": "Task",
"Resource": "arn:aws:lambda:ap-northeast-1:012345678901:function:second",
"Next": "Lambda3"
},
"Lambda3": {
"Type": "Task",
"Resource": "arn:aws:lambda:ap-northeast-1:012345678901:function:third",
"End": true
}
}
}
05. 動作確認
3 つの関数の実行完了後に「Hello World From Step Functions」という結果を得られれば OK です。
実行は Step Functions のコンソールから以下の入力で行います。
{
"message": "Hello"
}
Step Functions コンソールですべての実行が成功したことを確認します。

実行の入力と出力タブで以下の出力を確認できれば完了です。
{
"message": "Hello World From Step Functions"
}
Step Functions で 3 つの Lambda 関数を順番に実行できることを確認できました。
まとめ
今回は AWS Step Functions について紹介しました。
どなたかの参考になれば幸いです。
Discussion