AWS Step Function同步任务令牌 [英] AWS Step Function Synchronous Task Token

查看:89
本文介绍了AWS Step Function同步任务令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用例,我想使用步进函数来解决,但找不到解决此问题的方法.您的帮助将不胜感激.

问题是这样的:我有一个Amazon API Gateway,该网关具有/start 终端节点.对该端点的 POST 应该会启动数据处理会话,并将URL返回给API客户端可以用来捕获某些数据的应用程序.数据捕获完成后,在通过回调将最终响应发送到API客户端之前,需要进行一些处理.

我的想法(如下所示)是生成任务令牌并将其发送到数据捕获服务.然后,当用户数据捕获完成时,服务可以将请求发送到Step Function API,以表明该阶段已完成.问题是如何从步骤功能"中将URL返回给客户端?我不想使用回调来做到这一点.

一种选择是在"Step Function Initiator" Lambda中创建数据捕获会话,然后如何为数据捕获服务提供任务令牌?

真的,我需要的是一种从Step Function内向启动执行的Lambda同步返回某些内容(该调用的URL或第一阶段的任务令牌)的机制.这可能吗?您将如何解决这个问题?

解决方案

  • 在步骤函数启动器lambda中,您必须执行get-execution-history API和任务令牌将成为捕获数据"任务参数的一部分.由于这是第一步,因此实际上应该在几秒钟内完成,因此,我们可以继续每秒运行一次此循环,直到启动步骤功能中的所需步骤并获得任务令牌为止.

以这个示例为例,我将任务令牌从当前步骤函数传递到另一个步骤函数调用.

  {" StartAt":"ChildTask",州":{"ChildTask":{"End":是的,类型":任务",资源":"arn:aws:states :::: states:startExecution.waitForTaskToken",参数":{输入":{"token.$":"$$.Task.Token","foo":"bar"},"StateMachineArn":"arn:aws:states:us-east-1:110011001100:stateMachine:ChildStateMachine",名称":"MyExecutionName"}}}} 

获取执行历史记录:

  aws stepfunctions get-execution-history --execution-arn arn:aws:states:us-east-1:110011001100:execution:ParentStateMachine:667102b3-b19c-b7ab-b119-9ec6cf23e505 

结果:

执行历史记录和任务令牌中的前几个条目之一是参数的一部分.我们可以退出循环,抓住它,然后将其发送回Api Gateway.

  {时间戳":"2021-03-12T13:56:58.097000-05:00","type":"TaskScheduled","id":3,"previousEventId":2" taskScheduledEventDetails" ;: {"resourceType":状态","resource":"startExecution.waitForTaskToken",区域":"us-east-1","parameters":"{\" Input \":{\" foo \":\"bar \",\"token \":\"o6QVQ9gls ....... = \},\" StateMachineArn \:\" arn:aws:states:us-east-1:110011001100:stateMachine:ChildStateMachine \,\" Name \:\"; MyExecutionName \"}"}} 

I have a use case which I want to use Step Functions to solve but I can't find a way to solve this problem. Your help would be greatly appreciated.

The problem goes like this: I have an Amazon API Gateway which has a /start endpoint. a POST to this endpoint should start a data processing session and return a URL to an app which the API client can use to capture some data. Once data capture is complete, some processing takes place before the final response is sent to the API client via a callback.

My thinking, as you can see below, is to generate a task token and send it to the Data Capture Service. Then, when the user data capture is complete, the service can send a request to the Step Function API to say that stage is complete. The problem with this is how can I return the URL to the client from within the Step Function? I don't want to use a callback to do this.

One option is to create the data capture session within the 'Step Function Initiator' Lambda but then how do I provide the Data Capture Service with a task token?

Really, what I need is some mechanism of synchronously returning something (either a URL from that call or the task token from the first stage) from within the Step Function to the Lambda which started the execution. Is this possible? How would you solve this?

解决方案

  • In step function initiator lambda, you must be doing start-execution which returns an executionArn
  • Next, you can loop and call get-execution-history api and task token will be part of the 'capture data' task parameters. Since this is the first step, this really should be done with in couple of seconds, so, we can keep running this loop every second until desired step in step function is initiated and task token can be obtained.

Take this example, i am passing the task token to another step function call from current step function.

{
   "StartAt":"ChildTask",
   "States":{
      "ChildTask":{
         "End":true,
         "Type":"Task",
         "Resource":"arn:aws:states:::states:startExecution.waitForTaskToken",
         "Parameters":{
            "Input":{
               "token.$":"$$.Task.Token",
               "foo":"bar"
            },
            "StateMachineArn":"arn:aws:states:us-east-1:110011001100:stateMachine:ChildStateMachine",
            "Name":"MyExecutionName"
         }
      }
   }
}

Get Execution history:

aws stepfunctions get-execution-history --execution-arn arn:aws:states:us-east-1:110011001100:execution:ParentStateMachine:667102b3-b19c-b7ab-b119-9ec6cf23e505

Result:

one of the first few entries in execution history and task token is part of the parameters. we can exit the loop, grab that, send it back to Api Gateway.

{
    "timestamp": "2021-03-12T13:56:58.097000-05:00",
    "type": "TaskScheduled",
    "id": 3,
    "previousEventId": 2,
    "taskScheduledEventDetails": {
        "resourceType": "states",
        "resource": "startExecution.waitForTaskToken",
        "region": "us-east-1",
        "parameters": "{\"Input\":{\"foo\":\"bar\",\"token\":\"o6QVQ9gls.......=\"},\"StateMachineArn\":\"arn:aws:states:us-east-1:110011001100:stateMachine:ChildStateMachine\",\"Name\":\"MyExecutionName\"}"
    }
}

这篇关于AWS Step Function同步任务令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆