Search code examples
amazon-web-servicesamazon-s3aws-lambdaserverless-frameworkaws-step-functions

Serverless Framework AWS StepFunction not working


I have a problem that so far I'm unable to identify the root cause. I have an AWS step machine that should be invoked once a file is uploaded to an S3 bucket. So far when I upload the file to the S3 bucket, the lambda function that is defined in the StartAt key (StartAt: ImgUploadedEvent) starts as I can see in the lambda logs.

Here is the code:


stepFunctions:
  stateMachines:
    ValidateImageStateMachine:
      loggingConfig:
        level: ALL
        includeExecutionData: true
        destinations:
          - Fn::GetAtt: [ StepFuncLogGroup, Arn ]
      definition:
        Comment: "This state function validates the images after users upload them to S3"
        StartAt: ImgUploadedEvent
        States:
          ImgUploadedEvent:
            Type: Task
            Resource:
              Fn::GetAtt: [ImgUploaded, Arn]
            End: true

Below is the lambda function that is declared as the start of the StepMachine This lambda function as I can see from the logs indeed get called once I modified an Object in S3


functions:

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

To check that the Step Function was working I created a log group and added it to the Step Function.

resources:
  Resources:
    StepFuncLogGroup:
      Type: AWS::Logs::LogGroup
      Properties:
        LogGroupName: /aws/stepfunctions/${self:service}-${self:provider.stage}

I see this cloud watch log group correctly associated with the Step function in the AWS Console.

However when I upload an object to S3, I do see in the logs of the lambda functions that it was invoked, but I can not see any logs on the Step Function log group

My question is:

Is the Step Function indeed working and it is just an issue with the logs in the Step Function?

Or it is that the Step Function itself is not working and the lambda function is working just a lambda function totally independent of the Step Function?

What I have to do so the lambda function gets trigger as part of the Step Function?

BR


Solution

  • After studying how StepFunctions work I finally arrived to the conclusion that this is a wrong pattern.

    In no place on the documentation of the plugins or Amazon it says that what I did is a pattern

    StepFunctions can be started by events on CloudWatch and those events could be originated on a change on S3. That is not what I did here There is no a direct link between an action on S3 and a StepFunction

    The link in AWS documentation could confuse someone to think otherwise. Here is the title Starting a State Machine Execution in Response to Amazon S3 Events

    But the state machine does not start because an S3 events but because the CloudWatch log that this event generates

    A lambda proxy function is a good way of invoking an State Machine. This is a easy to use pattern and very common as we can use it with SQS etc

    So the correct response to this question is

    The state machine does not start because it is never invoked. We just called a lambda function that was used as the StartAt for an StateMachine. This does mean I invoked the State Machine.

    That is the reason why there is no logs for the state machine meanwhile there are correct logs for the lambda function

    Hope this response helps I will add more details and reference to this response

    BR