Twinkling Star
Twinkling Star

Reputation: 135

Step functions task for existing lambda in CDK

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",
        )

Upvotes: 0

Views: 2655

Answers (2)

Rohan Lekhwani
Rohan Lekhwani

Reputation: 500

Alternatively, since lamdba function names are unique. There's also the fromFunctionName method that only needs the name.

const importedLambda = lambda.Function.fromFunctionArn(scope,'importedLambda',lambdaName)

Upvotes: 0

Amit Baranes
Amit Baranes

Reputation: 8122

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.

Upvotes: 4

Related Questions