Search code examples
amazon-web-servicesaws-lambdaaws-sdkserverless-frameworkaws-step-functions

Getting Two Step Function Executions in serverless for one call [AWS]


I am working with serverless framework and step functions, I have a lambda function that triggered by an s3 object upload, then that lambda is programmed to start a state machine execution, but each time I upload some file it always shows two executions.

serverless.yml

plugins:
  - serverless-step-functions

provider:
   iamManagedPolicies:
       - arn:aws:iam::aws:policy/AWSStepFunctionsFullAccess

iamRoleStatements:
  - Effect: "Allow"
    Action:
      - states:*
    Resource: "*"

functions:
  init:
    handler: src/functions/main/handler.startStepFunction
    events:
      - s3:
          bucket: ${IMAGE_BUCKET_NAME}
          event: s3:ObjectCreated:*

Function

module.exports.startStepFunction = async event => {
  // start the step function with event parameter
  const params = {
    stateMachineArn: `${STATE_MACHINE}`,
    input: JSON.stringify(event)
  };

  const functionPromise = stepfunctions.startExecution(params, function(err, data) {
    if (err) console.log(err, err.stack);
    else     console.log(data);
  }).promise();

  await functionPromise;
};

Execution for one single file upload (both success executions are for one file upload)

enter image description here

I am not sure why something like this happens, anything that I miss out or making it act weird? Thanks in advance.


Solution

  • The problem is in these lines:

    const functionPromise = stepfunctions.startExecution(params, function(err, data) {
        if (err) console.log(err, err.stack);
        else     console.log(data);
      }).promise();
    

    where I promisifying the startExecution Function aswell as there is the callback,

    the right code should be like this below,

    const functionPromise = stepfunctions.startExecution(params).promise();