Skip to content

SNS Redrive Policy

Level: Error

Initial version: 0.1.7

cfn-lint: ES7000

tflint: aws_sns_topic_subscription_redrive_policy

You can configure the redrive policy on an Amazon SNS subscription. If SNS cannot deliver the message after the number of attempts set in its delivery policy, SNS will send it to the dead-letter queue specified in the redrive policy.

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
import { Queue } from '@aws-cdk/aws-sqs';
import { Topic } from '@aws-cdk/aws-sns';
import { UrlSubscription } from '@aws-cdk-aws-sns-subscriptions';

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

    // Dead letter queue
    const myDLQ = new Queue(
      scope, 'MyDLQ',
    );

    // SNS Topic
    const myTopic = new Topic(scope, 'MyTopic');

    // Adding an URL subscription
    myTopic.addSubscription(new UrlSubscription(
      'https://example.com',
      {
        // Configure the redrive policy for the subscription
        deadLetterQueue: myDLQ,
      }
    ));
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
{
  "Resources": {
    "MySubscription": {
    "Type": "AWS::SNS::Subscription",
    "Properties": {
      "Protocol": "https",
      "Endpoint": "https://example.com/"
      "TopicArn": "my-topic-arn",

      // Configure the redrive policy for the subscription
      "RedrivePolicy": "{ \"deadLetterTargetArn\": \"arn:aws:sqs:us-east-2:123456789012:MyDeadLetterQueue\"}"
      }
    }
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
Resources:
  MySubscription:
    Type: AWS::SNS::Subscription
    Properties:
      Protocol: https
      Endpoint: https://example.com/
      TopicArn: "my-topic-arn"

      # Configure the redrive policy for the subscription
      RedrivePolicy: |
        {
          "deadLetterTargetArn": "arn:aws:sqs:us-east-2:123456789012:MyDeadLetterQueue"
        }
 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
# For subscriptions to Lambda function endpoints
functions:
  MyFunction:
    handler: hello.handler
    events:
      - sns:
          topicName: my-topic
          # Configure the redrive policy for the subscription to the Lambda function
          redrivePolicy:
            deadLetterTargetArn: arn:aws:sqs:us-east-2:123456789012:MyDeadLetterQueue

# For subscriptions to other types of endpoint
resources:
  Resources:
    MySubscription:
      Type: AWS::SNS::Subscription
      Properties:
        Protocol: https
        Endpoint: https://example.com/
        TopicArn: "my-topic-arn"

        # Configure the redrive policy for the subscription to another type of resource
        RedrivePolicy: |
          {
            "deadLetterTargetArn": "arn:aws:sqs:us-east-2:123456789012:MyDeadLetterQueue"
          }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
resource "aws_sns_topic_subscription" "this" {
  endpoint = "https://example.com/"
  protocol = "https"
  topic_arn = "my-topic-arn"

  # Configure the redrive policy for the subscription
  redrive_policy = <<EOF
{
  "deadLetterTargetArn": "arn:aws:sqs:us-east-2:123456789012:MyDeadLetterQueue"
}
EOF
}

See also