问题
I am new to CDK and we have existing lambda resource and I want to use the lambda function as task in CDK. RunLambdaTask is expecting the lambda function. Is there a way to get the lambda function from the arn?
submit_job = sfn.Task(
self, "Submit Job",
task=sfn_tasks.RunLambdaTask("how to get the lambda function")
result_path="$.guid",
)
回答1:
In order to get the lambda function using ARN you need to use - lambda.Function.fromFunctionArn
.
Usage:
const lambdaARN = `arn:aws:lambda:${region}:${accountID}:function:${lambdaName}`
const importedLambda = lambda.Function.fromFunctionArn(scope,'importedLambda',lambdaARN)
Full example:
createRunLambdaTask(scope: cdk.Construct,lambdaARN: string,resultPath: string,duration: number = 1200,name: string): sfn.Task {
const importedLambda = lambda.Function.fromFunctionArn(scope,`${name}-lambda`,lambdaARN)
const task = new Task(scope, name, {
resultPath: resultPath,
timeout: Duration.seconds(duration),
task: new tasks.RunLambdaTask(importedLambda, {
integrationPattern: sfn.ServiceIntegrationPattern.WAIT_FOR_TASK_TOKEN,
payload: {
"token.$": sfn.Context.taskToken,
"Input.$": "$"
},
})
});
return task;
}
More about fromFunctionArn.
Update-
I have just noticed you work with Python and not Typescript. basically, this is the same implementation. Follow from_function_arn documentation about how to import existing lambda.
And later pass the IFucntion
object to RunLambdaTask
.
来源:https://stackoverflow.com/questions/61825766/step-functions-task-for-existing-lambda-in-cdk