Search code examples
amazon-web-servicesaws-step-functionsaws-cdk

CDK - Pass specific lambda version to a step function task


I have a CDK Stack which creates lambda functions and tasks. Each task uses a lambda function. Currently, the step function task is pointed only to the $LATEST alias. My goal is to update the lambda functions version or alias and update the step function task accordingly.

Lambda creation:

    createLambda(scope: cdk.Construct, name: string, lambdaPath: string, handler: string = 'app.handler', timeout: number = 900): lambda.Function {
            const lambdaFunction = new lambda.Function(scope, name, {
              functionName: name, 
              runtime: lambda.Runtime.NODEJS_10_X,
              code: lambda.Code.asset(lambdaPath),
              handler: handler,
              timeout: Duration.seconds(timeout),
              description: `Generated on: ${new Date().toISOString()}`
            });
            const version  = lambdaFunction.addVersion(new Date().toISOString());

            new lambda.Alias(scope, `alias-${new Date().toISOString()}`, {
              aliasName: 'live',
              version: version,
            });

            return lambdaFunction;
          }

task creation :

    createTask(scope: cdk.Construct,lambdaFunction: lambda.Function,duration: number = 1200,name: string): sfn.Task {
            const task = new sfn.Task(scope, name, {
              task: new tasks.InvokeFunction(lambdaFunction),
              timeout: Duration.seconds(duration)
            });
            return task;
          }

So my question is: Can I point to a specific lambda version within the step function task?


Solution

  • Ended up using the lambda ARN, whenever there's a change in the lambda, the CDK create new version and return the lambda version ARN :

    createLambda(scope: cdk.Construct, name: string, lambdaPath: string, handler: string = 'app.handler', timeout: number = 900): string {
        const lambdaFunction = new lambda.Function(scope, name, {
          functionName: name, 
          runtime: lambda.Runtime.NODEJS_10_X,
          code: lambda.Code.asset(lambdaPath),
          handler: handler,
          timeout: Duration.seconds(timeout),
        });
        const lambdaFunctionARN  = lambdaFunction.addVersion(`${new Date().toISOString()}`).functionArn;
    
        return lambdaFunctionARN;
      }
    

    Later, the Step functions task get the ARN as input and import the lambda function using :

    const lambdaVersion = lambda.Function.fromFunctionArn(scope,'LambdaImportUsingARN',lambdaVersionARN)