AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  eventbridge-integration-plaid-karte-dynamodb
  
  (qs-1sfl5n32m)

Metadata:
  AWS::ServerlessRepo::Application:
    Name: Amazon-eventbridge-integration-plaid-karte-dynamodb
    Description: This application demonstrates an integration with AWS DynamoDB, AWS SNS and AWS Lambda for Amazon EventBridge SaaS Partner PLAID
    Author: AWS Quick Start
    LicenseUrl: LICENSE.txt
    ReadmeUrl: README.md
    Labels:
      - amazon
      - eventbridge
      - integration
      - solution
      - aws
      - lambda
      - sns
      - dynamodb
      - plaid
    HomePageUrl: https://aws.amazon.com/quickstart/eventbridge/karte-eventbridge-integration-plaid-karte-dynamodb/
    SemanticVersion: 0.1.0
    SourceCodeUrl: https://github.com/aws-quickstart/eventbridge-integration-plaid-karte-dynamodb
  AWS::CloudFormation::Interface:
    ParameterGroups:
      - Label:
          default: Amazon EventBridge integration solution
        Parameters:
          - EventSourceName
          - DyanamoDBTableName
    ParameterLabels:
      EventSourceName:
        default: Event source name
      DyanamoDBTableName:
        default: DynamoDB table name

Parameters:
  EventSourceName:
    Type: String
    AllowedPattern: ^aws\.partner\/karte\.io(\/[\.\-_a-z0-9]+){3}$
    MinLength: 1
    MaxLength: 256
    Description: Name of the Amazon EventBridge KARTE event source to associate with an event bus (for example, aws.partner/karte.io/{AWSAccountID}/{KARTEProjectId}/{AppName}).
  DyanamoDBTableName:
    Type: String
    Description: Name of DynamoDB table

Globals:
  Function:
    Timeout: 30
    Environment:
      Variables:
        dynamodb_table_name: !Ref DyanamoDBTableName
Resources:
  #############
  #  SNS  #
  #############

  SNSTopic:
    Type: AWS::SNS::Topic

  #############
  #  Lambda  #
  #############
  WriteToDynamoLambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: src/WriteToDynamo
      Handler: writeToDynamo.handler
      Runtime: nodejs14.x
      Policies:
        - AmazonDynamoDBFullAccess
        - SNSCrudPolicy:
            TopicName:
              Fn::GetAtt:
                - SNSTopic
                - TopicName
      Events:
        SubscribedTopic:
          Properties:
            Topic:
              Ref: SNSTopic
          Type: SNS

  #################
  #  EventBridge  #
  #################
  EventBridgeEventBus:
    Type: AWS::Events::EventBus
    Properties:
      Name: !Ref EventSourceName
      EventSourceName: !Ref EventSourceName

  EventBridgeRule:
    Type: AWS::Events::Rule
    Properties:
      Description: catch-all rule for event bus
      EventBusName: !Ref EventBridgeEventBus
      EventPattern:
        account:
          - !Ref AWS::AccountId
        detail-type:
          - 'KARTE Action'
        detail:
          name:
            - _write_to_dynamo_request
      Name: write_to_dynamo
      State: ENABLED
      Targets:
        - Arn:
            Ref: SNSTopic
          Id: !GetAtt SNSTopic.TopicName

  EventTopicPolicy:
    Type: 'AWS::SNS::TopicPolicy'
    Properties:
      PolicyDocument:
        Statement:
          - Effect: Allow
            Principal:
              Service: events.amazonaws.com
            Action: 'sns:Publish'
            Resource: '*'
      Topics:
        - !Ref SNSTopic

  #################
  #  DynamoDB  #
  #################
  DynamoDB:
    Type: AWS::DynamoDB::Table
    Properties:
      BillingMode: PAY_PER_REQUEST
      AttributeDefinitions:
        - AttributeName: user_id
          AttributeType: S
      KeySchema:
        - AttributeName: user_id
          KeyType: HASH
      TableName: !Ref DyanamoDBTableName
      TimeToLiveSpecification:
        AttributeName: expired_at_unix_time
        Enabled: true

Outputs:
  EventBridgeEventBus:
    Description: SaaS event bus ARN
    Value: !GetAtt EventBridgeEventBus.Arn
  EventBridgeRule:
    Description: EventBridge rule ARN
    Value: !GetAtt EventBridgeRule.Arn
  WriteToDynamoLambdaFunction:
    Description: WriteToDynamoLambdaFunction ARN
    Value: !GetAtt WriteToDynamoLambdaFunction.Arn
  SNSTopic:
    Description: SNS Topic topic name
    Value: !GetAtt SNSTopic.TopicName