简体   繁体   English

通过AWS Step Functions调用AWS Lambda?

[英]Invoke AWS Lambda via AWS Step Functions?

One Lambda is calling another in a synchronous manner, gets some data and continues in processing. 一个Lambda以同步方式调用另一个Lambda,获取一些数据并继续进行处理。

This means, the costs are taken into account for both Lambdas in the call time period. 这意味着,在通话时间段内两个Lambda都要考虑成本。

Is it possible to use AWS Step Functions in this case without breaking the processing into a state machine blocks, which would make the code too complex? 在这种情况下是否可以使用AWS Step Functions而不将处理分为状态机块,这会使代码过于复杂?

Node.js 8.10 code: Node.js 8.10代码:

const AWS = require('aws-sdk');
const lambda = new AWS.Lambda({ apiVersion: '2015-03-31' });

exports.handle = async event => {
    try {
        const packageId = event.pathParameters['id'];

        const entry = await packageEntry(packageId);            
        const tags = await tagsForEntry(entry.id);

        return httpResponse(200, { ...entry, tags });
    }
    catch (err) {
        return httpResponse(500, err.message);
    }
}

async function tagsForEntry(entryId) {
    const response = await lambda.invoke({
        FunctionName: process.env.TAGGING_LAMBDA,
        InvocationType: 'RequestResponse',
        Payload: JSON.stringify({
            method: 'GET_TAGS',
            payload: { entryId }
        })
    }).promise();
    const payload = response.Payload ? JSON.parse(response.Payload.toString()) : {};
    return payload.tags || [];
}

async function packageEntry(packageId) {
    // return a package entry from a database
}

function httpResponse(statusCode, data) {
    // return a HTTP response object
}

Only modification you have to do is to return data from 1st function which required by the 2nd function. 您只需要做的修改就是从第二功能返回的第一功能返回数据。 Step function will automatically get the output of the first function as the input of the 2nd function. 步进功能将自动获得第一个功能的输出作为第二个功能的输入。

Also, you don't need to handle the function calls between the lambda functions, state machine will handle that. 另外,您不需要处理lambda函数之间的函数调用,状态机将处理该调用。

Also, there won't be additional cost for the service, other than the lambda execution cost, so your cost remains the same. 此外,除了lambda执行费用外,该服务不会有其他费用,因此您的费用保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM