0
votes

I'm trying to start an AWS State Machine (step function) once a file is uploaded to an S3 bucket. The bucket already exists and the project was created using Serverless Framework.

For that, I created this function in my serverless.yml

    functions:
    
      imagewasuploadedevent:
        handler: src/stepfunctions/imageWasUploadedEvent.handler
        events:
         - s3:
            bucket: !Ref AttachmentsBucket
            existing: true
        iamRoleStatements:
          - Effect: "Allow"
            Action:
              - "states:StartExecution"
            Resource:
              - "*"

This function should be triggered when a file is uploaded to the S3 bucket "AttachmentsBucket" that already exists and I hope it starts the State Machine processing

Now the Step Function definition is below

stepFunctions:
  stateMachines:
    ValidateImageStateMachine:
      definition:
        Comment: "This state function validates the images after users upload them to S3"
        StartAt: imagewasuploadedevent
        States:
          imageWasUploadedEvent:
            Type: Task
            Resource:
              Fn::GetAtt: [imagewasuploadedevent, Arn]
            End: true

The plug-ins section is making use of "serverless-step-functions" plugins

plugins:
  - serverless-python-requirements
  - serverless-iam-roles-per-function
  - serverless-step-functions

However, CloudFormation of the stack is failing with the following error

 An error occurred: 

ValidateImageStateMachineStepFunctionsStateMachine - Invalid State Machine 
Definition: 'MISSING_TRANSITION_TARGET: Missing 'Next' target: imagewasuploadedevent at /StartAt, MISSING_TRANSITION_TARGET: State "imageWasUploadedEvent" is not reachable. at /States/imageWasUploadedEvent' (Service: AWSStepFunctions; Status Code: 400; Error Code: InvalidDefinition; Request ID: 39217fe8-9dcd-4386-8549-b995619d2db6; Proxy: null).


I'm suspecting it has nothing to do with I'm doing but the fact that we can only use this plug-in to initiate State Machines through API Gateway functions and schedules and cloud watch events.

Could someone point me here in the right direction?

Thanks

1
Btw. is there any way to trigger it without Lambda function? Of course using Serverless and step functions pluginPatrykMilewski
From S3 I guess the way of triggering StepFunction is through CloudWtach event rules. It is explained here. I was unable at the end to do it this way using the Serverless Framework and step functions pluginMasterOfTheHouse
Thanks! Yes it's very complicated, I have no idea how to make it work, I will use Lambda thenPatrykMilewski

1 Answers

1
votes

I think this has to do with the case sensitivity of the Amazon States Language.

Try updating the StartAt with imageWasUploadedEvent

stepFunctions:
  stateMachines:
    ValidateImageStateMachine:
      definition:
        Comment: "This state function validates the images after users upload them to S3"
        StartAt: imageWasUploadedEvent
        States:
          imageWasUploadedEvent:
            Type: Task
            Resource:
              Fn::GetAtt: [imagewasuploadedevent, Arn]
            End: true