チュートリアル: Step Functions と AWS SAM CLI Local を使用したワークフローのテスト
Step Functions Local はサポートされていません
Step Functions Local はクラウド版と同等の機能を提供しておらず、サポートされていません。
テスト目的で Step Functions をエミュレートするサードパーティーソリューションを検討することもできます。
ローカルマシンで AWS Step Functions および AWS Lambda の両方が実行されている状態で、コードを AWS にデプロイすることなく、ステートマシンと Lambda 関数をテストできます。
詳細については、以下の各トピックを参照してください。
ステップ 1: AWS SAM を設定する
AWS Serverless Application Model (AWS SAM) CLI Local では、AWS Command Line Interface、AWS SAM、および Docker をインストールする必要があります。
-
AWS SAM CLI をインストールします。
注記
AWS SAM CLI をインストールする前に、AWS CLI および Docker をインストールする必要があります。AWS SAM CLI インストールについては、[Prerequisites] (前提条件) を参照。
-
AWS SAM クイックスタートドキュメントをひととおり確認してください。必ず、以下のステップに従います。
これにより、
sam-appディレクトリが作成され、Python ベースの Hello World Lambda 関数を含む環境が構築されます。
ステップ 2: AWS SAM CLI Local をテストする
これで AWS SAM のインストールが完了し、Hello World Lambda 関数を作成できたため、この関数をテストします。sam-app ディレクトリで、次のコマンドを入力します。
sam local start-api
これにより、Lambda 関数のローカルインスタンスが起動されます。次のような出力が表示されます:
2019-01-31 16:40:27 Found credentials in shared credentials file: ~/.aws/credentials
2019-01-31 16:40:27 Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET]
2019-01-31 16:40:27 You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions changes will be reflected instantly/automatically. You only need to restart SAM CLI if you update your AWS SAM template
2019-01-31 16:40:27 * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
ブラウザを開き、次のように入力します。
http://127.0.0.1:3000/hello
これにより、次のようなレスポンスが出力されます。
{"message": "hello world", "location": "72.21.198.66"}
Ctrl+C キーを押して、Lambda API を終了します。
ステップ 3: AWS SAM CLI Local を起動する
関数が機能することをテストしたので、AWS SAM CLI Local を起動します。sam-app ディレクトリで、次のコマンドを入力します。
sam local start-lambda
これにより AWS SAM CLI Local が起動し、次の出力のように、使用するエンドポイントが表示されます。
2019-01-29 15:33:32 Found credentials in shared credentials file: ~/.aws/credentials
2019-01-29 15:33:32 Starting the Local Lambda Service. You can now invoke your Lambda Functions defined in your template through the endpoint.
2019-01-29 15:33:32 * Running on http://127.0.0.1:3001/ (Press CTRL+C to quit)
ステップ 4: Step Functions Local のスタート
JAR ファイル
Step Functions Local の .jar ファイルバージョンを使用している場合は、Lambda エンドポイントを指定して Step Functions をスタートします。.jar ファイルを展開したディレクトリで、次のコマンドを入力します。
java -jar StepFunctionsLocal.jar --lambda-endpoint http://localhost:3001
Step Functions Local がスタートすると、環境に続いて、~/.aws/credentials ファイルで設定した認証情報がチェックされます。デフォルトでは、架空のユーザー ID を使用して起動し、region us-east-1 としてリスト表示されます。
2019-01-29 15:38:06.324: Failed to load credentials from environment because Unable to load AWS credentials from environment variables (AWS_ACCESS_KEY_ID (or AWS_ACCESS_KEY) and AWS_SECRET_KEY (or AWS_SECRET_ACCESS_KEY))
2019-01-29 15:38:06.326: Loaded credentials from profile: default
2019-01-29 15:38:06.326: Starting server on port 8083 with account account-id, region us-east-1
Docker
Docker バージョンの Step Functions Local を使用している場合、次のコマンドを使用して Step Functions を起動します。
docker run -p 8083:8083 amazon/aws-stepfunctions-local
Docker バージョンの Step Functions のインストールについては、Docker での Step Functions Local (ダウンロード可能バージョン) のセットアップ を参照してください。
注記
または .jar ファイルから Step Functions を起動する場合は環境変数を設定することで、コマンドラインを通じて、エンドポイントを指定できます。Docker バージョンの場合は、エンドポイントと認証情報をテキストファイルに指定する必要があります。Step Functions Local の設定オプションを指定する を参照してください。
ステップ 5: AWS SAM CLI Local 関数を参照するステートマシンを作成する
Step Functions Local が実行されたら、ステップ 1: AWS SAM を設定する で初期化した HelloWorldFunction を参照するステートマシンを作成します。
aws stepfunctions --endpoint http://localhost:8083 create-state-machine --definition "{\ \"Comment\": \"A Hello World example of the Amazon States Language using an AWS Lambda Local function\",\ \"StartAt\": \"HelloWorld\",\ \"States\": {\ \"HelloWorld\": {\ \"Type\": \"Task\",\ \"Resource\": \"arn:aws:lambda:region:account-id:function:HelloWorldFunction\",\ \"End\": true\ }\ }\ }\" --name "HelloWorld" --role-arn "arn:aws:iam::012345678901:role/DummyRole"
これによりステートマシンが作成され、実行をスタートするために使用できる Amazon リソースネーム (ARN) が提供されます。
{
"creationDate": 1548805711.403,
"stateMachineArn": "arn:aws:states:region:account-id:stateMachine:HelloWorld"
}
ステップ 6: ローカルステートマシンの実行を開始する
ステートマシンを作成したら、実行を開始します。次の aws stepfunctions コマンドを使用するときは、エンドポイントとステートマシンの ARN を参照する必要があります。
aws stepfunctions --endpoint http://localhost:8083 start-execution --state-machine arn:aws:states:region:account-id:stateMachine:HelloWorld --name test
これにより、HelloWorld ステートマシンの test という実行が開始されます。
{
"startDate": 1548810641.52,
"executionArn": "arn:aws:states:region:account-id:execution:HelloWorld:test"
}
これで Step Functions がローカルに実行されたので、AWS CLI を使用して操作することができます。例えば、この実行に関する情報を取得するには、以下のコマンドを使用します。
aws stepfunctions --endpoint http://localhost:8083 describe-execution --execution-arn arn:aws:states:region:account-id:execution:HelloWorld:test
実行のために describe-execution を呼び出すと、次のような出力により、完全な詳細が提供されます。
{
"status": "SUCCEEDED",
"startDate": 1549056334.073,
"name": "test",
"executionArn": "arn:aws:states:region:account-id:execution:HelloWorld:test",
"stateMachineArn": "arn:aws:states:region:account-id:stateMachine:HelloWorld",
"stopDate": 1549056351.276,
"output": "{\"statusCode\": 200, \"body\": \"{\\\"message\\\": \\\"hello world\\\", \\\"location\\\": \\\"72.21.198.64\\\"}\"}",
"input": "{}"
}