AWSTemplateFormatVersion: "2010-09-09"
Description: DT CloudWatch stack
Parameters:
  #------------------------
  # Deployment Information
  #------------------------
  environment:
    Type: String
    Description: Name of the environment
    Default: production

  #----------------
  # ECS Information
  #----------------
  Cluster:
    Description: The ECS cluster to watch
    Type: String

  #-------------------
  # Lambda Information
  #-------------------
  LambdaArn:
    Description: Lambda function to call upon ecs task state change
    Type: String

Resources:

  CloudWatchLambdaPermission:
    Type: AWS::Lambda::Permission
    Properties:
      Action: lambda:InvokeFunction
      Principal: events.amazonaws.com
      SourceArn: !GetAtt TaskListRule.Arn
      FunctionName: !Ref LambdaArn

  TaskListRule:
    Type: AWS::Events::Rule                                                                       
    Properties:
      EventPattern:
        source:
        - "aws.ecs"
        detail-type:
        - "ECS Task State Change"
        detail:
          clusterArn:
          - !Ref Cluster
      Targets:
      - Id: RedisUpdater
        Arn: !Ref LambdaArn