Step functions task for existing lambda in CDK

徘徊边缘 提交于 2021-01-25 06:44:22

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!