Search code examples
amazon-cloudwatchaws-step-functions

Cloudwatch Event Rule doesn't invoke when source state machine fails


I have a step function that runs 2 separate lambdas. If the step function fails or times out, I want to get an email via SNS telling me the step function failed. I created the event rule using cloudformation and specified the statemachine ARN in the event pattern. When the step function fails, no email is sent out. If I remove the stateMachineArn parameter and run my step function, I get the failure email. I've double checked numerous times that I'm entering the correct ARN for the state machine. CF for the Event Rule is below (in YAML format). Thanks.

  FailureEvent:
    Type: AWS::Events::Rule
    DependsOn:
      - StateMachine
    Properties:
      Name: !Ref FailureRuleName
      Description: "EventRule"
      EventPattern:
        detail-type:
          - "Step Functions Execution Status Change"
        detail:
          status:
            - "FAILED"
            - "TIMED_OUT"
        stateMachineArn: ["arn:aws:states:region:account#:stateMachine:statemachine"]
      Targets:
        -
          Arn:
            Ref: SNSARN
          Id: !Ref SNSTopic

Solution

  • I did get this fixed and expanded on it to invoke a lambda that publishes a custom SNS email using a lambda. My alignment was off in my EventPattern section. See below. Thanks to @Marcin.

    FailureEvent:
    Type: AWS::Events::Rule
    DependsOn:
      - FMIStateMachine
    Properties:
      Description: !Ref FailureRuleDescription
      Name: !Ref FailureRuleName
      State: "ENABLED"
      RoleArn:
        'Fn::Join': ["", ['arn:aws:iam::', !Ref 'AWS::AccountId', ':role/', !Ref LambdaExecutionRole]]
      EventPattern:
        detail-type:
          - "Step Functions Execution Status Change"
        detail:
          status:
            - "FAILED"
            - "TIMED_OUT"
          stateMachineArn: [!Ref StateMachine]
      Targets:
        - Arn:
            'Fn::Join': ["", ['arn:aws:lambda:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':function:', !Ref FailureLambda]]
          Id: !Ref FailureLambda
          Input: !Sub '{"failed_service": "${StateMachineName}","sns_arn": "arn:aws:sns:${AWS::Region}:${AWS::AccountId}:${SNSTopic}"}'