使用 AWS CloudFormation Step Functions でワークフローを作成する - AWS Step Functions

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

使用 AWS CloudFormation Step Functions でワークフローを作成する

このチュートリアルでは、 AWS Lambda を使用した 関数 AWS CloudFormation。 を使用します。 AWS CloudFormation コンソールとスタックを作成するためのYAMLテンプレート (IAM ロール、Lambda 関数、ステートマシン)。次に、Step Functions コンソールを使用してステートマシンの実行を開始します。

詳細については、「」の CloudFormation 「テンプレートAWS::StepFunctions::StateMachineリソースの使用」を参照してください。 AWS CloudFormation ユーザーガイド

ステップ 1: をセットアップする AWS CloudFormation テンプレート

サンプルテンプレート を使用する前に、 のさまざまな部分を宣言する方法を理解しておく必要があります。 AWS CloudFormation テンプレート。

Lambda の IAMロールを作成するには

Lambda 関数のIAMロールに関連付けられた信頼ポリシーを定義します。次の例では、 YAMLまたは を使用して信頼ポリシーを定義しますJSON。

YAML
LambdaExecutionRole: Type: "AWS::IAM::Role" Properties: AssumeRolePolicyDocument: Version: "2012-10-17" Statement: - Effect: Allow Principal: Service: lambda.amazonaws.com Action: "sts:AssumeRole"
JSON
"LambdaExecutionRole": { "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "lambda.amazonaws.com" }, "Action": "sts:AssumeRole" } ] } }

Lambda 関数を作成するには

Hello World というメッセージを印刷する Lambda 関数の以下のプロパティを定義します。

重要

Lambda 関数が同じ にあることを確認します。 AWS アカウントと AWS リージョン ステートマシンとして。

YAML
MyLambdaFunction: Type: "AWS::Lambda::Function" Properties: Handler: "index.handler" Role: !GetAtt [ LambdaExecutionRole, Arn ] Code: ZipFile: | exports.handler = (event, context, callback) => { callback(null, "Hello World!"); }; Runtime: "nodejs12.x" Timeout: "25"
JSON
"MyLambdaFunction": { "Type": "AWS::Lambda::Function", "Properties": { "Handler": "index.handler", "Role": { "Fn::GetAtt": [ "LambdaExecutionRole", "Arn" ] }, "Code": { "ZipFile": "exports.handler = (event, context, callback) => {\n callback(null, \"Hello World!\");\n};\n" }, "Runtime": "nodejs12.x", "Timeout": "25" } },

ステートマシン実行用の IAMロールを作成するには

ステートマシン実行のIAMロールに関連付けられた信頼ポリシーを定義します。

YAML
StatesExecutionRole: Type: "AWS::IAM::Role" Properties: AssumeRolePolicyDocument: Version: "2012-10-17" Statement: - Effect: "Allow" Principal: Service: - !Sub states.${AWS::Region}.amazonaws.com Action: "sts:AssumeRole" Path: "/" Policies: - PolicyName: StatesExecutionPolicy PolicyDocument: Version: "2012-10-17" Statement: - Effect: Allow Action: - "lambda:InvokeFunction" Resource: "*"
JSON
"StatesExecutionRole": { "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": [ { "Fn::Sub": "states.${AWS::Region}.amazonaws.com" } ] }, "Action": "sts:AssumeRole" } ] }, "Path": "/", "Policies": [ { "PolicyName": "StatesExecutionPolicy", "PolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "lambda:InvokeFunction" ], "Resource": "*" } ] } } ] } },

Lambda ステートマシンを作成するには

Lambda ステートマシンを定義します。

YAML
MyStateMachine: Type: "AWS::StepFunctions::StateMachine" Properties: DefinitionString: !Sub - |- { "Comment": "A Hello World example using an AWS Lambda function", "StartAt": "HelloWorld", "States": { "HelloWorld": { "Type": "Task", "Resource": "${lambdaArn}", "End": true } } } - {lambdaArn: !GetAtt [ MyLambdaFunction, Arn ]} RoleArn: !GetAtt [ StatesExecutionRole, Arn ]
JSON
"MyStateMachine": { "Type": "AWS::StepFunctions::StateMachine", "Properties": { "DefinitionString": { "Fn::Sub": [ "{\n \"Comment\": \"A Hello World example using an AWS Lambda function\",\n \"StartAt\": \"HelloWorld\",\n \"States\": {\n \"HelloWorld\": {\n \"Type\": \"Task\",\n \"Resource\": \"${lambdaArn}\",\n \"End\": true\n }\n }\n}", { "lambdaArn": { "Fn::GetAtt": [ "MyLambdaFunction", "Arn" ] } } ] }, "RoleArn": { "Fn::GetAtt": [ "StatesExecutionRole", "Arn" ] } } }

ステップ 2: を使用する AWS CloudFormation Lambda ステートマシンを作成するための テンプレート

のコンポーネントを理解したら AWS CloudFormation テンプレートをまとめ、テンプレートを使用して を作成できます。 AWS CloudFormation スタック。

Lambda ステートマシンを作成するには

  1. 次のサンプルデータを、YAMLこの例の場合は 、 MyStateMachine.jsonの場合は という名前MyStateMachine.yamlのファイルにコピーしますJSON。

    YAML
    AWSTemplateFormatVersion: "2010-09-09" Description: "An example template with an IAM role for a Lambda state machine." Resources: LambdaExecutionRole: Type: "AWS::IAM::Role" Properties: AssumeRolePolicyDocument: Version: "2012-10-17" Statement: - Effect: Allow Principal: Service: lambda.amazonaws.com Action: "sts:AssumeRole" MyLambdaFunction: Type: "AWS::Lambda::Function" Properties: Handler: "index.handler" Role: !GetAtt [ LambdaExecutionRole, Arn ] Code: ZipFile: | exports.handler = (event, context, callback) => { callback(null, "Hello World!"); }; Runtime: "nodejs12.x" Timeout: "25" StatesExecutionRole: Type: "AWS::IAM::Role" Properties: AssumeRolePolicyDocument: Version: "2012-10-17" Statement: - Effect: "Allow" Principal: Service: - !Sub states.${AWS::Region}.amazonaws.com Action: "sts:AssumeRole" Path: "/" Policies: - PolicyName: StatesExecutionPolicy PolicyDocument: Version: "2012-10-17" Statement: - Effect: Allow Action: - "lambda:InvokeFunction" Resource: "*" MyStateMachine: Type: "AWS::StepFunctions::StateMachine" Properties: DefinitionString: !Sub - |- { "Comment": "A Hello World example using an AWS Lambda function", "StartAt": "HelloWorld", "States": { "HelloWorld": { "Type": "Task", "Resource": "${lambdaArn}", "End": true } } } - {lambdaArn: !GetAtt [ MyLambdaFunction, Arn ]} RoleArn: !GetAtt [ StatesExecutionRole, Arn ]
    JSON
    { "AWSTemplateFormatVersion": "2010-09-09", "Description": "An example template with an IAM role for a Lambda state machine.", "Resources": { "LambdaExecutionRole": { "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "lambda.amazonaws.com" }, "Action": "sts:AssumeRole" } ] } } }, "MyLambdaFunction": { "Type": "AWS::Lambda::Function", "Properties": { "Handler": "index.handler", "Role": { "Fn::GetAtt": [ "LambdaExecutionRole", "Arn" ] }, "Code": { "ZipFile": "exports.handler = (event, context, callback) => {\n callback(null, \"Hello World!\");\n};\n" }, "Runtime": "nodejs12.x", "Timeout": "25" } }, "StatesExecutionRole": { "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": [ { "Fn::Sub": "states.${AWS::Region}.amazonaws.com" } ] }, "Action": "sts:AssumeRole" } ] }, "Path": "/", "Policies": [ { "PolicyName": "StatesExecutionPolicy", "PolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "lambda:InvokeFunction" ], "Resource": "*" } ] } } ] } }, "MyStateMachine": { "Type": "AWS::StepFunctions::StateMachine", "Properties": { "DefinitionString": { "Fn::Sub": [ "{\n \"Comment\": \"A Hello World example using an AWS Lambda function\",\n \"StartAt\": \"HelloWorld\",\n \"States\": {\n \"HelloWorld\": {\n \"Type\": \"Task\",\n \"Resource\": \"${lambdaArn}\",\n \"End\": true\n }\n }\n}", { "lambdaArn": { "Fn::GetAtt": [ "MyLambdaFunction", "Arn" ] } } ] }, "RoleArn": { "Fn::GetAtt": [ "StatesExecutionRole", "Arn" ] } } } } }
  2. を開きますAWS CloudFormation コンソールでスタックの作成を選択します

  3. [Select Template] (テンプレートを選択) ページで、[Upload a template to Amazon S3] (Amazon S3 にテンプレートをアップロード) を選択します。MyStateMachine ファイルを選択し、[Next] (次へ) を選択します。

  4. [Specify Details] (詳細の指定) ページで、[Stack name] (スタック名) に MyStateMachine を入力してから、[Next] (次へ) を選択します。

  5. [Options] (オプション) ページで、[Next] (次へ) を選択します。

  6. レビューページで、以下を承認します。 AWS CloudFormation は IAM リソースを作成する場合があります。次に の作成を選択します

    AWS CloudFormation はMyStateMachineスタックの作成を開始し、CREATE_IN_PROGRESS ステータスを表示します。プロセスが完了すると、 AWS CloudFormation は CREATE_COMPLETE ステータスを表示します。

  7. (省略可能) スタックのリソースを表示するには、スタックを選択して [Resources] (リソース) タブを選択します。

ステップ 3: ステートマシンの実行をスタートする

Lambda ステートマシンの作成後、実行を開始できます。

ステートマシンの実行をスタートするには

  1. Step Functions コンソールを開き、 を使用して作成したステートマシンの名前を選択します。 AWS CloudFormation.

  2. リポジトリの [] MyStateMachine-ABCDEFGHIJ1K ページ、「新しい実行」を選択します

    [新しい実行] ページが表示されます。

  3. (オプション) 生成されたデフォルトを上書きするカスタム実行名を入力します。

    非ASCII名前とログ記録

    Step Functions は、 以外のASCII文字を含むステートマシン、実行、アクティビティ、およびラベルの名前を受け入れます。このような文字は Amazon では機能しないため CloudWatch、 でメトリクスを追跡できるようにASCII、文字のみを使用することをお勧めします CloudWatch。

  4. [実行のスタート] を選択します。

    ステートマシンの新しい実行がスタートされ、実行中の実行が表示されている新しいページが表示されます。

  5. (オプション) [Execution Details] (実行の詳細) で、[Execution Status] (実行ステータス) および [Started] (開始済み) と [Closed] (終了済み) のタイムスタンプをレビューします。

  6. 実行結果を表示するには、[Output] (出力) タブを選択します。