AWSTemplateFormatVersion: '2010-09-09'
Resources:           
  GlueCrawlerRole:
    Type: AWS::IAM::Role
    Properties :
      RoleName: !Sub "AWSGlueServiceRole-RedshiftAutoMount"
      Description: IAM Role to crawl the files in the Amazon S3 data lake
      AssumeRolePolicyDocument:
        Version : 2012-10-17
        Statement :
          -
            Effect : Allow
            Principal :
              Service :
                - glue.amazonaws.com
            Action :
              - sts:AssumeRole
      Path: /
      Policies:              
        - PolicyName: AWSGlueServiceRole-RedshiftAutoMount-s3Policy
          PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Effect: Allow
                Action:
                  - 's3:PutObject'
                  - 's3:GetObject'
                Resource:
                  - !Sub "arn:aws:s3:::redshift-demos/data/NY-Pub/*"        
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AWSGlueServiceRole
  GlueExternalDatabase:
    Type: AWS::Glue::Database
    Properties:
      CatalogId:
        Ref: AWS::AccountId
      DatabaseInput:
        Name: !Sub "automountdb"
        Description: "awsdatacatalog automatic mounting feature demo database"        
  GlueCrawlerNyPub:
    Type: AWS::Glue::Crawler
    DependsOn:
       - GlueCrawlerRole
    Properties:
        Role: !Sub "arn:aws:iam::${AWS::AccountId}:role/AWSGlueServiceRole-RedshiftAutoMount"
        Name: !Sub "NYTaxiCrawler"
        DatabaseName: !Ref GlueExternalDatabase
        Targets:
          S3Targets:
            - Path: !Sub "s3://redshift-demos/data/NY-Pub/"
Outputs:
  GlueExternalDatabaseName:
    Description: "The name of the external database"
    Value: !Ref GlueExternalDatabase 