本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
在本教程中,您將創建 AWS Lambda 函數使用 AWS CloudFormation。 您將使用 AWS CloudFormation 用於建立堆疊 (IAM角色、Lambda 函數和狀態機器) 的主控台和YAML範本。然後,您將使用 Step Functions 控制台來啟動狀態機執行。
如需詳細AWS::StepFunctions::StateMachine
資訊,請參閱使用 CloudFormation 範本與 AWS CloudFormation 使用者指南。
步驟 1:設定 AWS CloudFormation template
在使用示例模板之前,您應該了解如何聲明 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 狀態機
-
將下列範例資料複製到以YAML範MyStateMachine.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"
]
}
}
}
}
}
-
開啟 AWS CloudFormation
控制台,然後選擇創建堆棧。
-
在 Select Template (選取範本) 頁面上,選擇 Upload a template to Amazon S3 (將範本上傳到 Amazon S3)。選擇您的 MyStateMachine
檔案,然後選擇 Next (下一步)。
-
在 Specify Details (指定詳細資訊) 頁面上,為 Stack Name (堆疊名稱) 輸入 MyStateMachine
,然後選擇 Next (下一步)。
-
在選項頁面上,選擇下一步。
-
在「檢閱」頁面上,選擇「我確認」 AWS CloudFormation
可能會創建IAM資源。 ,然後選擇 [建立]。
AWS CloudFormation 開始建立MyStateMachine
堆疊並顯示 CREATE_IN_ 狀PROGRESS態。該過程完成後, AWS CloudFormation
顯示 CREATE_ COMPLETE 狀態。
-
(選用) 若要顯示堆疊中的資源,請選取堆疊,然後選擇 Resources (資源) 標籤。
步驟 3:啟動狀態機執行
建立 Lambda 狀態機器之後,您就可以開始執行該機器。
開始狀態機器執行
-
開啟 Step Functions 主控台,然後選擇您使用建立的狀態機器名稱 AWS CloudFormation.
-
在「」MyStateMachine-ABCDEFGHIJ1K
頁面上,選擇 [新增執行]。
系統會隨即顯示 New execution (新執行) 頁面。
(選擇性) 輸入自訂執行名稱,以覆寫產生的預設值。
Step Functions 接受包含非ASCII字元的狀態機器、執行項目、活動和標籤的名稱。由於此類字元不適用於 Amazon CloudWatch,因此我們建議您僅使用ASCII字元,以便在中追蹤指標 CloudWatch。
-
選擇 Start Execution (開始執行)。
狀態機器會開始新執行,且隨即會出現新頁面顯示您正在執行的執行。
-
(選用) 在 Execution Details (執行詳細資訊) 中,檢閱 Execution Status (執行狀態)、Started (已開始) 和 Closed (已結束) 時間戳記。
-
若要檢視執行結果,請選擇 Output (輸出)。