[Solved] AWS - Unable pass data from triggered lambda event to step function

I have a lambda fn(startExecution) being triggered by the creation of an s3 object, which in turn launches my statemachine.

functions:
  startExecution:
    handler: src/handlers/start/handler.start
    events:
      - existingS3:
          bucket: test-bucket
          events:
            - s3:ObjectCreated:*
          rules:
            - suffix: .jpg # TODO: add video file extension here
    environment:
      statemachine_arn: ${self:resources.Outputs.MiraPipeline.Value}
  
  fetchUserData:
    handler:  src/handlers/user/handler.fetchUserData

The lambda triggers as expected on the creation of the s3 object, as well as triggers the step function to start as desired.

From my startExecution lambda’s handler.js:

module.exports.start = (event, context, callback) => {
  
  // Extract the record to pass to the next step
  console.log('StartExecution triggered by s3:ObjectCreated')
    const metadata = {
      record: event.Records[0]
    }

  console.log({metadata})

  // Start the Step Function and pass the extracted metadata through to the next step
  const stateMachineArn = process.env.statemachine_arn;
  const params = {
    stateMachineArn
  }

  return stepfunctions.startExecution(params).promise().then(() => {
    callback(null, metadata)
  }).catch(error => {
    callback(error.message);
  })
}

My issue is that the metadata object does not get passed correctly to the first function in my step function…

Here’s the stepFunction configuration from the serverless.yml:

stepFunctions:
  stateMachines:
    imageProcessingStepFn:
      name: 'ImagePipeline'
      definition:
        Comment: "Using Amazon State Language to generate a state machine for processing images"
        StartAt: FetchUserDataState
        States:
          FetchUserDataState:
            Type: Task
            Resource: "arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${opt:stage}-fetchUserData"
            Next: ChoiceState

I was under the impression that the event passed to the FetchUserDataState would is passed as the second argument in the callback one the step functions.startExecution(params).promise() resolves, or am I wrong?

In my fetchUserData handler.js:

module.exports.fetchUserData = async(event, context, callback) => {

  console.log({event})
  const { record } = event
  console.log(record.s3.bucket.name)
    ...
}

The event object is empty, and the following error is thrown:

TypeError: Cannot read property 's3' of undefined
    at module.exports.fetchUserData (/var/task/src/handlers/user/handler.js:23:24)
    at /var/task/serverless-sdk/index.js:9:87180

Am I missing something here as far as the logic goes? Any help would be greatly appreciated!!

Solved! I was following a poorly structured example, after reviewing the documentation the solution was relatively simple: adding an input property to the params:

  const params = {
    stateMachineArn,
    input: JSON.stringify(metadata)
  }

Hi
Since I am new to serverless for step function, my question is how you get satemachine_arn from:

Thanks,
Tran