Python 获取步骤函数执行结果

Python 获取步骤函数执行结果,python,aws-lambda,aws-api-gateway,boto3,aws-step-functions,Python,Aws Lambda,Aws Api Gateway,Boto3,Aws Step Functions,我是AWS新手,正在为阶跃函数而挣扎 import json import boto3 import uuid client = boto3.client('stepfunctions') def lambda_handler(event, context): transactionId = str(uuid.uuid1()) print(transactionId) input = {'TransactionId':transactionId,'tex

我是AWS新手,正在为阶跃函数而挣扎

import json
import boto3
import uuid

client = boto3.client('stepfunctions')


def lambda_handler(event, context):
    transactionId = str(uuid.uuid1())
    print(transactionId) 
    
    input = {'TransactionId':transactionId,'text':'search_word'} 
    
    response = client.start_execution(
        stateMachineArn='arn:aws:states:ap-northeast-2:xxxxxxxxxx:stateMachine:MyStateMachine',
        name=transactionId,
        input=json.dumps(input)
        )
        
    print(response) 
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }
{ 
  "Comment": "A simple AWS Step Functions state machine.", 
  "StartAt": "Tokenize", 
  "States": {  
    "Tokenize": { 
      "Type": "Task", 
      "Resource": "arn:aws:lambda:ap-northeast-2:xxxxxxxx:function:search_ko", 
      "Next": "Search"
    }, 
    "Search": { 
      "Type": "Task", 
      "Resource": "arn:aws:lambda:ap-northeast-2:xxxxxxx:function:BM-25-Get-Index", 
      "End": true 
    } 
  }
}
我的工作流程如下:

客户端('search_word')->api网关->lambda函数(调用步骤函数)->步骤函数(生成搜索输出)->客户端

这是我的invoke lambda函数

import json
import boto3
import uuid

client = boto3.client('stepfunctions')


def lambda_handler(event, context):
    transactionId = str(uuid.uuid1())
    print(transactionId) 
    
    input = {'TransactionId':transactionId,'text':'search_word'} 
    
    response = client.start_execution(
        stateMachineArn='arn:aws:states:ap-northeast-2:xxxxxxxxxx:stateMachine:MyStateMachine',
        name=transactionId,
        input=json.dumps(input)
        )
        
    print(response) 
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }
{ 
  "Comment": "A simple AWS Step Functions state machine.", 
  "StartAt": "Tokenize", 
  "States": {  
    "Tokenize": { 
      "Type": "Task", 
      "Resource": "arn:aws:lambda:ap-northeast-2:xxxxxxxx:function:search_ko", 
      "Next": "Search"
    }, 
    "Search": { 
      "Type": "Task", 
      "Resource": "arn:aws:lambda:ap-northeast-2:xxxxxxx:function:BM-25-Get-Index", 
      "End": true 
    } 
  }
}
我想从step函数中获取执行结果并将其传递给客户端。但是我不知道怎么做

只要我能将步骤函数的执行结果提供给客户端,工作流就不必是我建议的

这是我的阶跃函数

import json
import boto3
import uuid

client = boto3.client('stepfunctions')


def lambda_handler(event, context):
    transactionId = str(uuid.uuid1())
    print(transactionId) 
    
    input = {'TransactionId':transactionId,'text':'search_word'} 
    
    response = client.start_execution(
        stateMachineArn='arn:aws:states:ap-northeast-2:xxxxxxxxxx:stateMachine:MyStateMachine',
        name=transactionId,
        input=json.dumps(input)
        )
        
    print(response) 
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }
{ 
  "Comment": "A simple AWS Step Functions state machine.", 
  "StartAt": "Tokenize", 
  "States": {  
    "Tokenize": { 
      "Type": "Task", 
      "Resource": "arn:aws:lambda:ap-northeast-2:xxxxxxxx:function:search_ko", 
      "Next": "Search"
    }, 
    "Search": { 
      "Type": "Task", 
      "Resource": "arn:aws:lambda:ap-northeast-2:xxxxxxx:function:BM-25-Get-Index", 
      "End": true 
    } 
  }
}
请帮忙


提前谢谢

您试图将同步的api网关与异步的step函数结合起来。如果使用step函数,则流应该是异步的。您不会通过单个API调用获得响应。您需要创建另一个API,该API将继续“轮询”step函数,以查看它是否已成功执行以及响应是什么

以下是您可以尝试的流程

  • 初始化step函数的API调用。您可以尝试直接将API网关post调用与step函数连接,并查看它是否返回执行arn作为响应。如果是的话,你就不需要羔羊了。如果没有,则坚持使用当前lambda并使其返回执行arn
  • 然后,您的应用程序获取执行arn,并使用它定期调用另一个API。如果您认为完成步骤函数执行所需的时间少于30秒(API网关超时限制),则可以定期调用API或让后端中的lambda保持轮询以获取响应
  • 然后,第二个API/Lambda使用查找执行结果,并返回响应
  • 编辑: 如果您相信您的step函数将在30秒内持续执行,那么可以尝试使用单个lambda启动step函数,然后使用DescribeeExecution继续轮询它以完成