AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: 'CloudFormation template for Asset Legacy Notification StepFunction deployment.'
Parameters:
  MediaInfoLambdaName:
    Type: String
    Description: The name of madiainfo-lambda.
  LegacySyncNotificationLambdaName:
    Type: String
    Description: The name of legacy sync notification lambda.
  BucketName:
    Type: String
    Description: Bucket name with synchronized assets
Resources:
  StatesExecutionRole:
    Type: "AWS::IAM::Role"
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service: !Sub states.${AWS::Region}.amazonaws.com
            Action: "sts:AssumeRole"
      Path: "/"
      Policies:
        - PolicyName: StatesExecutionPolicy
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: Allow
                Action:
                  - "lambda:InvokeFunction"
                Resource: "*"
  ExecuteStateMachineRole:
    Type: "AWS::IAM::Role"
    Properties:
        AssumeRolePolicyDocument:
          Version: "2012-10-17"
          Statement:
            -
              Sid: "AllowCWEServiceToAssumeRole"
              Effect: "Allow"
              Action:
                - "sts:AssumeRole"
              Principal:
                Service:
                  - "events.amazonaws.com"
        Path: "/"
        Policies:
          -
            PolicyName: "ExecuteStateMachine"
            PolicyDocument:
              Version: "2012-10-17"
              Statement:
                -
                  Effect: "Allow"
                  Action:
                    - "states:StartExecution"
                  Resource: "*"
  LegacyNotificationSF:
    Type: 'AWS::StepFunctions::StateMachine'
    Properties:
      DefinitionString: !Sub
        |-
        {
          "Comment": "Following state machine describe notification workflow after asset synchronization",
          "StartAt": "MediaInfo",
          "States": {
            "MediaInfo": {
              "Type": "Task",
              "Resource": "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${MediaInfoLambdaName}",
              "Next": "LegacySyncNotification"
            },
            "LegacySyncNotification": {
              "Type": "Task",
              "Resource": "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${LegacySyncNotificationLambdaName}",
              "End": true
            }
          }
        }
      RoleArn: !GetAtt StatesExecutionRole.Arn
  ExecuteStateMachineRole:
    Type: "AWS::IAM::Role"
    Properties:
        AssumeRolePolicyDocument:
          Version: "2012-10-17"
          Statement:
            -
              Sid: "AllowCWEServiceToAssumeRole"
              Effect: "Allow"
              Action:
                - "sts:AssumeRole"
              Principal:
                Service:
                  - "events.amazonaws.com"
        Path: "/"
        Policies:
          -
            PolicyName: "ExecuteStateMachine"
            PolicyDocument:
              Version: "2012-10-17"
              Statement:
                -
                  Effect: "Allow"
                  Action:
                    - "states:StartExecution"
                  Resource: "*"
  CloudWatchEventRule:
    Type: "AWS::Events::Rule"
    Properties:
      Description: ""
      EventPattern:
        detail-type:
          - "AWS API Call via CloudTrail"
        source:
          - "aws.s3"
        detail:
          eventSource:
            - "s3.amazonaws.com"
          eventName:
            - "PutObject"
            - "PutObjectAcl"
            - "UploadPart"
            - "UploadPartCopy"
            - "CopyObject"
          requestParameters:
            bucketName:
              - Ref: BucketName
      Name: "S3PutObjectFilteringRule"
      State: "ENABLED"
      Targets:
        -
          Arn: !Ref LegacyNotificationSF
          Id: SFN_Target
          RoleArn: !GetAtt ExecuteStateMachineRole.Arn
Outputs:
  StateMachineArn:
    Description: ARN for the Step Functions state machine
    Value: !Ref LegacyNotificationSF
  MonitoredBucket:
    Description: The monitored bucket name
    Value: !Ref BucketName
  CWETriggerRoleArn:
    Description: ARN for the IAM role to execute the state machine
    Value: !GetAtt ExecuteStateMachineRole.Arn