Parameters:
  CFNDatabaseName: 
    Type: String
    Default: s3-storagelens-db
    Description: Provide the Glue database name

  S3Path: 
    Type: String
    Description: Provide the S3 Path where the Storage Lens Data is stored.

  CrawlerName: 
    Type: String
    Default: s3-storage-lens-crawler
    Description: Provide the Crawler Name.
    
Resources:
  MyRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          -
            Effect: "Allow"
            Principal:
              Service:
                - "glue.amazonaws.com"
            Action:
              - "sts:AssumeRole"
      Path: "/"
      ManagedPolicyArns: 
        - "arn:aws:iam::aws:policy/service-role/AWSGlueServiceRole"
      Policies:
        -
          PolicyName: "GlueServiceRoleForS3StorageLens"
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              -
                Effect: "Allow"
                Action:
                  - s3:GetObject
                  - s3:PutObject
                Resource: !Join [ "", [ "arn:aws:s3:::" , !Select [ 2, !Split ["/", Ref: S3Path ]], "/*"] ] 
                

 
  MyDatabase:
    Type: AWS::Glue::Database
    Properties:
      CatalogId: !Ref AWS::AccountId
      DatabaseInput:
        Name:
          Ref: CFNDatabaseName
        Description: "Table to store the s3 analytics data"
 

 
  MyCrawler2:
    Type: AWS::Glue::Crawler
    Properties:
      Name:
        Ref: CrawlerName
      Role: !GetAtt MyRole.Arn
      DatabaseName: !Ref MyDatabase
      Targets:
        S3Targets:
          - Path: 
              Ref: S3Path
      SchemaChangePolicy:
        UpdateBehavior: "UPDATE_IN_DATABASE"
        DeleteBehavior: "LOG"
      Schedule:
        ScheduleExpression: "cron(0 22 * * ? *)"