AWSTemplateFormatVersion: '2010-09-09'
Description: "CloudFormation template for SNS Topic integration with AWS Resilience Hub.*WARNING** This template creates SNS and related resources. You will be billed for the AWS resources used if you create a stack from this template"

Parameters:
  TopicName:
    Type: String
    Description: Name of the SNS Topic
    Default: resilience-hub-notifications

  EmailAddress:
    Type: String
    Description: Email address for SNS notifications
    AllowedPattern: '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
    ConstraintDescription: Must be a valid email address

Resources:
  ResilienceHubSNSTopic:
    Type: AWS::SNS::Topic
    Properties:
      TopicName: !Ref TopicName
      Tags:
        - Key: Purpose
          Value: ResilienceHub
        - Key: Environment
          Value: Production

  ResilienceHubSNSTopicPolicy:
    Type: AWS::SNS::TopicPolicy
    Properties:
      Topics:
        - !Ref ResilienceHubSNSTopic
      PolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Sid: AllowResilienceHubPublish
            Effect: Allow
            Principal:
              Service: resiliencehub.amazonaws.com
            Action: 
              - sns:Publish
            Resource: !Ref ResilienceHubSNSTopic

  EmailSubscription:
    Type: AWS::SNS::Subscription
    Properties:
      Protocol: email
      TopicArn: !Ref ResilienceHubSNSTopic
      Endpoint: !Ref EmailAddress

Outputs:
  SNSTopicARN:
    Description: ARN of the created SNS Topic
    Value: !Ref ResilienceHubSNSTopic
    Export:
      Name: !Sub "${AWS::StackName}-TopicARN"

  SNSTopicName:
    Description: Name of the created SNS Topic
    Value: !GetAtt ResilienceHubSNSTopic.TopicName
    Export:
      Name: !Sub "${AWS::StackName}-TopicName"
