AWSTemplateFormatVersion: 2010-09-09
Parameters:
  InstanceType:
    Type: String
    Default: "m3.xlarge"
  ReleaseLabel:
    Type: String
    Default: "emr-5.36.1"
  SubnetId:
    Type: String
  TerminationProtected:
    Type: String
    Default: 'false'
  ElasticMapReducePrincipal:
    Type: String
    Default: "elasticmapreduce.amazonaws.com"
  Ec2Principal:
    Type: String
    Default: "ec2.amazonaws.com"
Resources:
  cluster:
    Type: AWS::EMR::Cluster
    Properties:
      LogUri: !Sub "s3://bdb-3070-griffin-datavalidation-blog-${AWS::AccountId}-${AWS::Region}/emrlogs"
      StepConcurrencyLevel: 1
      Applications:
        - Name: Hadoop
        - Name: Hive
        - Name: Hue
        - Name: Spark
      BootstrapActions:
        - Name: Bootstrap_Script
          ScriptBootstrapAction: 
            Path: !Sub "s3://bdb-3070-griffin-datavalidation-blog-${AWS::AccountId}-${AWS::Region}/bootstrap-bdb-3070-datavalidation.sh"
            Args:
              - !Sub "bdb-3070-griffin-datavalidation-blog-${AWS::AccountId}-${AWS::Region}"
      Configurations:
        - Classification: hive-site
          ConfigurationProperties:
            hive.metastore.client.factory.class: "com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory"
        - Classification: spark-hive-site
          ConfigurationProperties:
            hive.metastore.client.factory.class: "com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory"
      Instances:
        MasterInstanceGroup:
          InstanceCount: 1
          InstanceType: !Ref InstanceType
          Market: ON_DEMAND
          Name: cfnMaster
        CoreInstanceGroup:
          InstanceCount: 1
          InstanceType: !Ref InstanceType
          Market: ON_DEMAND
          Name: cfnCore
        TaskInstanceGroups:
          - InstanceCount: 1
            InstanceType: !Ref InstanceType
            Market: ON_DEMAND
            Name: cfnTask-1
        TerminationProtected: !Ref TerminationProtected
        Ec2SubnetId: !Ref SubnetId
      Name: Griffin_Data_Validation_Blog_Cluster
      JobFlowRole: !Ref emrEc2InstanceProfile
      ServiceRole: !Ref emrRole
      ReleaseLabel: !Ref ReleaseLabel
      VisibleToAllUsers: true
      Tags:
        - Key: tech-summit
          Value: 2023
  emrRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: !Sub emrRole-${AWS::StackName}
      AssumeRolePolicyDocument:
        Version: 2008-10-17
        Statement:
          - Sid: ''
            Effect: Allow
            Principal:
              Service: !Ref ElasticMapReducePrincipal
            Action: 'sts:AssumeRole'
      Path: /
      ManagedPolicyArns:
        - 'arn:aws:iam::aws:policy/service-role/AmazonElasticMapReduceRole'
  emrEc2Role:
    Type: AWS::IAM::Role
    Properties:
      RoleName: !Sub emrEc2Role-${AWS::StackName}
      AssumeRolePolicyDocument:
        Version: 2008-10-17
        Statement:
          - Sid: ''
            Effect: Allow
            Principal:
              Service: !Ref Ec2Principal
            Action: 'sts:AssumeRole'
      Path: /
      ManagedPolicyArns:
        - 'arn:aws:iam::aws:policy/service-role/AmazonElasticMapReduceforEC2Role'
  emrEc2InstanceProfile:
    Type: AWS::IAM::InstanceProfile
    Properties:
      InstanceProfileName: !Sub emrEc2InstanceProfile-${AWS::StackName}
      Path: /
      Roles:
        - !Ref emrEc2Role
  EmrStep:
    DependsOn: cluster
    Type: AWS::EMR::Step
    Properties:
      Name: Audit_table_creation
      JobFlowId: !Ref cluster
      ActionOnFailure: CONTINUE
      HadoopJarStep:
        Jar: command-runner.jar
        Args:
          - hive-script
          - --run-hive-script
          - --args
          - -f
          - !Sub s3://bdb-3070-griffin-datavalidation-blog-${AWS::AccountId}-${AWS::Region}/Validation_Metrics_Athena_tables.hql
          - -d
          - !Sub s3Bucket=bdb-3070-griffin-datavalidation-blog-${AWS::AccountId}-${AWS::Region}
  EmrStep2:
    DependsOn: EmrStep
    Type: AWS::EMR::Step
    Properties:
      Name: Total_Count
      JobFlowId: !Ref cluster
      ActionOnFailure: CONTINUE
      HadoopJarStep:
        Jar: s3://us-east-1.elasticmapreduce/libs/script-runner/script-runner.jar
        Args:
          - !Sub "s3://bdb-3070-griffin-datavalidation-blog-${AWS::AccountId}-${AWS::Region}/datavalidation/totalcount/total_count.sh"
          - !Sub "bdb-3070-griffin-datavalidation-blog-${AWS::AccountId}-${AWS::Region}"
  EmrStep3:
    DependsOn: EmrStep2
    Type: AWS::EMR::Step
    Properties:
      Name: accuracy
      JobFlowId: !Ref cluster
      ActionOnFailure: CONTINUE
      HadoopJarStep:
        Jar: s3://us-east-1.elasticmapreduce/libs/script-runner/script-runner.jar
        Args:
          - !Sub "s3://bdb-3070-griffin-datavalidation-blog-${AWS::AccountId}-${AWS::Region}/datavalidation/accuracy/accuracy.sh"
          - !Sub "bdb-3070-griffin-datavalidation-blog-${AWS::AccountId}-${AWS::Region}"