使用建立 Step Functions 的 Lambda 狀態機 AWS CloudFormation - AWS Step Functions

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用建立 Step Functions 的 Lambda 狀態機 AWS CloudFormation

本教程將向您展示如何使用創建基本 AWS Lambda 函數 AWS CloudFormation。您將使用主 AWS CloudFormation 控台和 YAML 範本建立堆疊 (IAM 角色、Lambda 函數和狀態機器)。然後,您將使用 AWS Step Functions 控制台來啟動狀態機執行。

若要取得更多資訊,請參閱《使用指南》中的〈AWS CloudFormation 使用 CloudFormation 範本AWS::StepFunctions::StateMachine資源〉

步驟 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 函式

為將列印訊息的 Lambda 函數定義下列屬性Hello World

重要

確保您的 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. 將下列範例資料複製到名為 MyStateMachine.yaml 的檔案中以用於 YAML 範例,或 MyStateMachine.json 以用於 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 主控台,然後選擇 Create Stack (建立堆疊)

  3. Select Template (選取範本) 頁面上,選擇 Upload a template to Amazon S3 (將範本上傳到 Amazon S3)。選擇您的 MyStateMachine 檔案,然後選擇 Next (下一步)

  4. Specify Details (指定詳細資訊) 頁面上,為 Stack Name (堆疊名稱) 輸入 MyStateMachine,然後選擇 Next (下一步)

  5. 選項頁面上,選擇下一步

  6. 在 [檢] 頁面上,選擇 [我確認 AWS CloudFormation 可能會建立 IAM 資源]。 ,然後選擇 [建立]。

    AWS CloudFormation 會開始建立MyStateMachine堆疊,並顯示「建立中 _ 進度」狀態。程序完成後, AWS CloudFormation 會顯示 CREATE_COMPLETE 狀態。

  7. (選用) 若要顯示堆疊中的資源,請選取堆疊,然後選擇 Resources (資源) 標籤。

    
                顯示資源

步驟 3:啟動狀態機執行

建立 Lambda 狀態機器之後,您就可以開始執行該機器。

開始狀態機器執行

  1. 開啟 Step Functions 主控台,然後選擇您使用建立的狀態機器名稱 AWS CloudFormation。

  2. MyStateMachine-ABCDEFGIJ 1K 頁面上,選擇新的執行。

    系統會隨即顯示 New execution (新執行) 頁面。

  3. (選擇性) 若要識別您的執行項目,您可以在「名稱」(Name) 方塊中指定執行項目的名稱。默認情況下,Step Functions 自動生成一個唯一的執行名稱。

    注意

    Step Functions 可讓您建立狀態機器、執行項目和活動的名稱,以及包含非 ASCII 字元的標籤。這些非 ASCII 名稱不適用於 Amazon CloudWatch。若要確保您可以追蹤 CloudWatch 量度,請選擇僅使用 ASCII 字元的名稱。

  4. 選擇 Start Execution (開始執行)

    狀態機器會開始新執行,且隨即會出現新頁面顯示您正在執行的執行。

  5. (選用) 在 Execution Details (執行詳細資訊) 中,檢閱 Execution Status (執行狀態)Started (已開始)Closed (已結束) 時間戳記。

  6. 若要檢視執行結果,請選擇 Output (輸出)