【发布时间】:2023-04-06 12:12:01
【问题描述】:
我对 AWS Step Functions 和 AWS Lambda Functions 非常陌生,真的需要一些帮助。我有一个状态机,我试图检查某个文件是否存在于我的 S3 存储桶中,然后如果文件存在则让状态机遵循一个路径,如果不存在则让状态机遵循不同的路径。
下面显示了我的状态机代码的开头,涵盖了这个问题
{
"Comment": "This is a test for running the structure of the CustomCreate job.",
"StartAt": "PreStep",
"States": {
"PreStep": {
"Comment": "Check that all the necessary files exist before running the job.",
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXX:function:CustomCreate-PreStep-Function",
"Next": "Run Job Choice"
},
"Run Job Choice": {
"Comment": "This step chooses whether or not to go forward with running the main job.",
"Type": "Choice",
"Choices": [
{
"Variable": "$.FoundNecessaryFiles",
"BooleanEquals": true,
"Next": "Spin Up Cluster"
},
{
"Variable": "$.FoundNecessaryFiles",
"BooleanEquals": false,
"Next": "Do Not Run Job"
}
]
},
"Do Not Run Job": {
"Comment": "This step triggers if the PreStep fails and the job should not run.",
"Type": "Fail",
"Cause": "PreStep unsuccessful"
},
"Spin Up Cluster": {
"Comment": "Spins up the EMR Cluster.",
"Type": "Pass",
"Next": "Update Env"
},
"Update Env": {
"Comment": "Update the environment variables in the EMR Cluster.",
"Type": "Pass",
"Next": "Run Job"
},
"Run Job": {
"Comment": "Add steps to the EMR Cluster.",
"Type": "Pass",
"End": true
}
}
}
以下代码描述了我的 CustomCreate-PreStep-Function Lambda 函数
exports.handler = async function(event, context, callback) {
var AWS = require('aws-sdk');
var s3 = new AWS.S3();
var params = {Bucket: 'BUCKET_NAME', Key: 'FILE_NAME'};
s3.getObject(params, function(err, data) {
if (err) {
console.log(err, err.stack);
// file does not exist
console.log("failed");
callback(null,false);
}
else {
console.log(data);
//file exist
console.log("succeeded");
var FoundNecessaryFiles = true;
// return FoundNecessaryFiles;
callback(null,event.FoundNecessaryFiles=true);
}
});
};
我尝试了很多方法,但都无法正常工作。如您所见,我正在尝试使用 Lambda 函数将状态为真/假的变量 FoundNecessaryFiles 传回,具体取决于是否找到了文件,并使用该值来指导在下一步。最好通过修复变量回传来解决这个问题,因为我稍后可能需要在状态机中再次使用该方法,但我也愿意接受另一种解决方案,无论是组合步骤还是其他任何可能的方法.
另外,如果存在正确的文件,我在此过程中的下一步将是启动一个 AWS EMR 集群,我也不清楚如何进行完成。如果有人能够在使用 Step Functions 运行 AWS EMR 集群方面提供任何帮助,我将不胜感激。
【问题讨论】:
标签: amazon-web-services aws-lambda amazon-emr aws-step-functions