Skip to content

EventBridge Rule without DLQ

Level: Error

Initial version: 0.1.3

cfn-lint: ES4000

tflint: aws_cloudwatch_event_target_no_dlq

Sometimes, an event isn't successfully delivered to the target(s) specified in a rule. By default, EventBridge will retry for 24 hours and up to 185 times, but you can customize the retry policy.

If EventBridge cannot deliver an event after all its retries, it can send it to a dead-letter queue. You can then inspect the event and remediate the underlying issue.

Implementations

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { Function } from '@aws-cdk/aws-lambda';
import { Rule } from '@aws-cdk/aws-events';
import * as targets from '@aws-cdk/aws-events-targets';

export class MyStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const myFunction = new Function(
      scope, 'MyFunction',
      {
        code: Code.fromAsset('src/hello/'),
        handler: 'main.handler',
        runtime: Runtime.PYTHON_3_8,
      }
    );

    const myRule = new Rule(
      scope, 'MyRule',
      {
        eventPattern: {
          source: ['my-source'],
        }
      }
    );

    myRule.addTarget(new targets.LambdaFunction(
      myfunction,
      // Add a DLQ to the 'myFunction' target
      {
        deadLetterQueue: myQueue,
      }
    ));
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
{
  "Resources": {
    "MyRule": {
      "Type": "AWS::Events::Rule",
      "Properties": {
        "EventBusName": "default",
        "EventPattern": "{\"source\": [\"my-source\"]}",
        "Targets": [{
          "Id": "MyFunction",
          "Arn": "arn:aws:lambda:us-east-1:111122223333:function:MyFunction",
          // Add a DLQ to the 'MyFunction' target
          "DeadLetterConfig": {
            "Arn": "arn:aws:sqs:us-east-1:111122223333:dlq"
          }
        }]
      }
    }
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
Resources:
  MyRule:
    Type: AWS::Events::Rule
    Properties:
      EventBusName: default
      EventPattern: |
        {
          "source": ["my-source"]
        }
      Targets:
        - Id: MyFunction
          Arn: arn:aws:lambda:us-east-1:111122223333:function:MyFunction
          # Add a DLQ to the 'MyFunction' target
          DeadLetterConfig:
            Arn: arn:aws:sqs:us-east-1:111122223333:dlq
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
resources:
  Resources:
    MyRule:
      Type: AWS::Events::Rule
      Properties:
        EventBusName: default
        EventPattern: |
          {
            "source": ["my-source"]
          }
        Targets:
          - Id: MyFunction
            Arn: arn:aws:lambda:us-east-1:111122223333:function:MyFunction
            # Add a DLQ to the 'MyFunction' target
            DeadLetterConfig:
              Arn: arn:aws:sqs:us-east-1:111122223333:dlq
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
resource "aws_cloudwatch_event_rule" "this" {
  event_pattern = <<EOF
{
  "source": ["my-source"]
}
EOF
}

resource "aws_cloudwatch_event_target" "this" {
  rule      = aws_cloudwatch_event_rule.this.name
  target_id = "MyFunction"
  arn       = "arn:aws:lambda:us-east-1:111122223333:function:MyFunction"

  # Add a DLQ to the 'MyFunction' target
  dead_letter_config {
    arn = "arn:aws:sqs:us-east-1:111122223333:dlq"
  }
}

See also