可以从 lambda 执行 step 函数,但是当我尝试传递一个值时,它说 input {} 失败 [英] Can execute a step function from a lambda but when I try to pass a value it fails saying input {}

查看:40
本文介绍了可以从 lambda 执行 step 函数,但是当我尝试传递一个值时,它说 input {} 失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上我已经创建了一个 S3 存储桶,我将文件放入其中.这会触发一个 lambda_function(a),它调用一个 step 函数,然后触发另一个 lambda_function(b).

我正在尝试将文件名从存储桶传递给 lambda_function(b).

到目前为止我有:将文件名从 S3 存储桶传递给 lambda_function(a),从这里我面临无法将其传递给 step 函数的问题.

我阅读了大量文章(例如 https://medium.com/@tturnbull/passing-data-between-lambdas-with-aws-step-functions-6f8d45f717c3)但我似乎无法让它工作......

所以我的 lambda_function(a) 看起来像这样:

const AWS = require('aws-sdk');const stepFunctions = new AWS.StepFunctions({地区:'us-west-2'});让索引 = 函数索引(事件,上下文,回调){const fileName = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));常量参数 = {stateMachineArn: 'MY-STATE-MACHINE-NO',名称:文件名};stepFunctions.startExecution(params, (err, data) => {如果(错误){控制台日志(错误);常量响应 = {状态代码:500,正文:JSON.stringify({消息:'有一个错误'})};回调(空,响应);} 别的 {控制台日志(数据);常量响应 = {状态代码:200,正文:JSON.stringify({消息:'步进功能有效'}),};回调(空,响应);}});};module.exports.init = (事件、上下文、回调) =>{};出口处理程序 = 索引;

这一切正常.我的步进函数如下所示:

<代码> {评论":使用 Pass 状态的亚马逊状态语言的 Hello World 示例",StartAt":HelloWorld",状态":{你好世界":{类型":任务",资源":MY-ARN-NUMBER",输入路径":$.title",结果路径":$.title",输出路径":$.title",结束":真}}}

我知道这是有效的,因为如果我用

开始对步进函数执行

<代码>{标题":131231231.xml"}

存储在 'title' 中的这个文件名被传递给 lambda_function(b) 作为 'event'

所以我剩下的问题是试图将它从 lambda_function(a) 获取到 step 函数.

我知道它必须作为 JSON 发送,因此如果您参考我的函数,我已将其编辑为包括:

 const 响应 = {状态代码:200,正文:JSON.stringify({消息:'步骤功能有效',标题:131231231.xml"}),标题:131231231.xml"};回调(空,响应);}

但是这在 step 函数上失败了:

<代码> {错误":States.Runtime",原因":在执行状态HelloWorld"(在事件 ID #2 处输入)时发生错误.路径 '$.title' 无效:路径没有结果:$['title']";}

更新感谢@rajesh 的回复,我能够确定

console.log('RESPONSE:' + response);

打印响应:[object Object]

所以我将响应更改为

<代码>{var response = JSON.stringify({"title":"131231231.xml"});};console.log('RESPONSE: ' + response);回调(空,响应);

在日志中返回...

响应:{标题":131231231.xml"}

所以从技术上讲,这应该是对的?..错了...如果我检查 step 函数,它会失败并输出错误消息

<代码> {错误":States.Runtime",原因":在执行状态HelloWorld"(在事件 ID #2 处输入)时发生错误.路径 '$.title' 无效:路径没有结果:$['title']";}

如果我在 step 函数的执行细节中检查输入选项卡,输入显示为空... {}

那么为什么输入没有捕获标题"JSON...有什么想法吗?

解决方案

您正在paramsname 字段中传递您的输入.但是,您需要通过 params 的 input 字段传递您的输入(在您的情况下为空,因此出现错误).试试:

const 参数 = {stateMachineArn: 'MY-STATE-MACHINE-NO',输入:"{\"title\" : \"" + 文件名 + "\"}"};

请参阅 sdk 的文档,了解更多信息:

var 参数 = {stateMachineArn: 'STRING_VALUE',/* 需要 */输入:'STRING_VALUE',名称:'STRING_VALUE'};stepfunctions.startExecution(params, function(err, data) {if (err) console.log(err, err.stack);//发生错误否则 console.log(data);//成功响应});

<块引用>

输入——(字符串)包含用于执行的 JSON 输入数据的字符串,例如:"input": "{\"first_name\" : \"test\"}"

So basically I've created an S3 bucket which I drop my files into. This triggers a lambda_function(a) which calls a step function which then triggers another lambda_function(b).

I am trying to pass the file name from the bucket to lambda_function(b).

So far I have: passing the file name from S3 bucket to lambda_function(a), from here I am facing the problem where I cant pass it to the step function.

I have read numerous articles (eg https://medium.com/@tturnbull/passing-data-between-lambdas-with-aws-step-functions-6f8d45f717c3) but I just cant seem to get it working...

so My lambda_function(a) looks like so:

const AWS = require('aws-sdk');

const stepFunctions = new AWS.StepFunctions({
region: 'us-west-2'
});

let index = function index(event, context, callback) {
      
const fileName = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
const params = {
    stateMachineArn: 'MY-STATE-MACHINE-NO',
    name: fileName
};

stepFunctions.startExecution(params, (err, data) => {
    if (err) {
    console.log(err);
    const response = {
        statusCode: 500,
        body: JSON.stringify({
        message: 'There was an error'
        })
    };
    callback(null, response);
    } else {
    console.log(data);
    const response = {
        statusCode: 200,
        body: JSON.stringify({
        message: 'Step function worked'
        }),
    };
    callback(null, response);
    }
});
};

module.exports.init = (event, context, callback) => {
};
exports.handler = index;

This all works fine. My step function looks like so:

  {
  "Comment": "A Hello World example of the Amazon States Language using a Pass state",
  "StartAt": "HelloWorld",
  "States": {
    "HelloWorld": {
      "Type": "Task",
      "Resource": "MY-ARN-NUMBER",
      "InputPath": "$.title",
      "ResultPath": "$.title",
      "OutputPath": "$.title",
      "End": true
    }
  }
}

I know this is working because if I start an execution on the step function with

{
    "title":"131231231.xml"
}

this filename stored in 'title' gets passed to lambda_function(b) as 'event'

So the problem I am left with is trying to get it from lambda_function(a) to the step function.

I know it has to be sent as a JSON so if you refer back to my function I have edited it to include:

 const response = {
    statusCode: 200,
    body: JSON.stringify({
    message: 'Step function worked',
    title :"131231231.xml"
    }),
    title :"131231231.xml"
};
callback(null, response);
}

But this fails on the step function with:

    {
  "error": "States.Runtime",
  "cause": "An error occurred while executing the state 'HelloWorld' (entered at the event id #2). Invalid path '$.title' : No results for path: $['title']"
}

UPDATE Thanks to @rajesh for replying I was able to determine that

console.log('RESPONSE: ' + response);

prints RESPONSE: [object Object]

SO I changed the response to be

{
  var response = JSON.stringify({"title":"131231231.xml"});
};
console.log('RESPONSE: ' + response);
callback(null, response);

In the logs this returns...

RESPONSE: {"title":"131231231.xml"}

So technically this should be right?..wrong...If I check the step function it fails with the output error message of

    {
  "error": "States.Runtime",
  "cause": "An error occurred while executing the state 'HelloWorld' (entered at the event id #2). Invalid path '$.title' : No results for path: $['title']"
}

and If I check the input tab in the execution details in the step function the input shows empty as... {}

So why is the input not capturing the 'title' JSON...any ideas?

解决方案

You are passing your input in the name field of params. However, you need to pass your input through the input field of params (which is empty in your case, hence the error). Try:

const params = {
    stateMachineArn: 'MY-STATE-MACHINE-NO',
    input: "{\"title\" : \"" + fileName + "\"}"
};

See the sdk's documentation for more info:

var params = {
  stateMachineArn: 'STRING_VALUE', /* required */
  input: 'STRING_VALUE',
  name: 'STRING_VALUE'
};
stepfunctions.startExecution(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

input — (String) The string that contains the JSON input data for the execution, for example: "input": "{\"first_name\" : \"test\"}"

这篇关于可以从 lambda 执行 step 函数,但是当我尝试传递一个值时,它说 input {} 失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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