{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "Link-watcher learning foundation: storage, queues, and DLQ alarm. Application integration is a guided assignment.",
  "Parameters": {
    "AlertTopicArn": {
      "Type": "String",
      "Description": "Existing SNS topic in the stack region/account for DLQ notifications",
      "AllowedPattern": "^arn:[a-z-]+:sns:[a-z0-9-]+:[0-9]{12}:.+$"
    }
  },
  "Resources": {
    "DeadLetters": {
      "Type": "AWS::SQS::Queue",
      "Properties": {
        "MessageRetentionPeriod": 1209600,
        "SqsManagedSseEnabled": true
      }
    },
    "CheckJobs": {
      "Type": "AWS::SQS::Queue",
      "Properties": {
        "VisibilityTimeout": 180,
        "MessageRetentionPeriod": 345600,
        "ReceiveMessageWaitTimeSeconds": 20,
        "SqsManagedSseEnabled": true,
        "RedrivePolicy": {
          "deadLetterTargetArn": {
            "Fn::GetAtt": [
              "DeadLetters",
              "Arn"
            ]
          },
          "maxReceiveCount": 5
        }
      }
    },
    "WatcherState": {
      "Type": "AWS::DynamoDB::Table",
      "Properties": {
        "BillingMode": "PAY_PER_REQUEST",
        "AttributeDefinitions": [
          {
            "AttributeName": "pk",
            "AttributeType": "S"
          },
          {
            "AttributeName": "sk",
            "AttributeType": "S"
          }
        ],
        "KeySchema": [
          {
            "AttributeName": "pk",
            "KeyType": "HASH"
          },
          {
            "AttributeName": "sk",
            "KeyType": "RANGE"
          }
        ],
        "SSESpecification": {
          "SSEEnabled": true
        },
        "PointInTimeRecoverySpecification": {
          "PointInTimeRecoveryEnabled": true
        },
        "TimeToLiveSpecification": {
          "AttributeName": "expires_at",
          "Enabled": true
        }
      }
    },
    "DeadLetterAlarm": {
      "Type": "AWS::CloudWatch::Alarm",
      "Properties": {
        "AlarmDescription": "Check jobs exhausted retries; inspect before redrive.",
        "Namespace": "AWS/SQS",
        "MetricName": "ApproximateNumberOfMessagesVisible",
        "Dimensions": [
          {
            "Name": "QueueName",
            "Value": {
              "Fn::GetAtt": [
                "DeadLetters",
                "QueueName"
              ]
            }
          }
        ],
        "Statistic": "Maximum",
        "Period": 60,
        "EvaluationPeriods": 1,
        "Threshold": 0,
        "ComparisonOperator": "GreaterThanThreshold",
        "TreatMissingData": "notBreaching",
        "AlarmActions": [
          {
            "Ref": "AlertTopicArn"
          }
        ]
      }
    }
  },
  "Outputs": {
    "QueueUrl": {
      "Value": {
        "Ref": "CheckJobs"
      }
    },
    "QueueArn": {
      "Value": {
        "Fn::GetAtt": [
          "CheckJobs",
          "Arn"
        ]
      }
    },
    "TableName": {
      "Value": {
        "Ref": "WatcherState"
      }
    },
    "DeadLetterQueueUrl": {
      "Value": {
        "Ref": "DeadLetters"
      }
    }
  }
}
