# This template is used to update the stack created by canary1_001.yaml
AWSTemplateFormatVersion: 2010-09-09
Description: EMR Serverless Application stack
Resources:
  ## Resources for Production Deployments - EMR Serverless Spark app and S3 bucket for artifacts/logs
  SparkApplication:
    Type: AWS::EMRServerless::Application
    Properties:
      Name: emr-cli-demo
      ReleaseLabel: emr-6.9.0
      Type: Spark
      InitialCapacity:
        - Key: Driver
          Value:
            WorkerCount: 1
            WorkerConfiguration:
              Cpu: 4 vCPU
              Memory: 16 GB
              Disk: 20 GB
        - Key: Executor
          Value:
            WorkerCount: 4
            WorkerConfiguration:
              Cpu: 4 vCPU
              Memory: 16 GB
              Disk: 20 GB
      MaximumCapacity:
        Cpu: 400 vCPU
        Memory: 3000 GB

  EMRServerlessArtifacts:
    Type: AWS::S3::Bucket

  ### IAM Role with the following permissions:
  # - read/write access to the Glue Data Catalog
  # - read access to the S3 sample data bucket
  # - write access to the prod/test buckets
  EMRServerlessJobRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - emr-serverless.amazonaws.com
            Action:
              - "sts:AssumeRole"
      Description: "Service role for EMR Serverless jobs"
      RoleName: !Sub emr-cli-demo-${AWS::AccountId}
      Policies:
        - PolicyName: GlueAccess
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: Allow
                Action:
                  - "glue:GetDatabase"
                  - "glue:GetDataBases"
                  - "glue:CreateTable"
                  - "glue:GetTable"
                  - "glue:GetTables"
                  - "glue:GetPartition"
                  - "glue:GetPartitions"
                  - "glue:CreatePartition"
                  - "glue:BatchCreatePartition"
                  - "glue:GetUserDefinedFunctions"
                Resource: "*"
        - PolicyName: S3Access
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: Allow
                Action:
                  - "s3:ListBucket"
                Resource:
                  - arn:aws:s3:::noaa-gsod-pds
                  - !GetAtt EMRServerlessArtifacts.Arn
              - Effect: Allow
                Action:
                  - "s3:GetObject"
                Resource:
                  - arn:aws:s3:::noaa-gsod-pds/*
                  - !Sub "${EMRServerlessArtifacts.Arn}/*"
              - Effect: Allow
                Action:
                  - "s3:PutObject"
                Resource:
                  - !Sub "${EMRServerlessArtifacts.Arn}/*"

Outputs:
  ApplicationId:
    Value: !Ref SparkApplication
    Description: APPLICATION_ID
  S3BucketName:
    Value: !Ref EMRServerlessArtifacts
    Description: S3_BUCKET
  JobRoleArn:
    Value: !GetAtt EMRServerlessJobRole.Arn
    Description: JOB_ROLE_ARN
