# FIXME:
# - rename spacy layer to (s3fs + mypy_boto3) layer

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
  Converter

  SAM Template for a function to convert an Amazon SageMaker GroundTruth NER
  augmented manifest to the Amazon Comprehend format.

Parameters:
  AccountId:
    Description: "12-digit AWS account id. Default is default. NOTE: 'sam local ...' must use --parameter-overrides to specify a valid one."
    Type: String
    Default: default

  # Lambda functions add a policy to this name rather than S3Bucket to prevent circular dependency.
  BucketName:
    Description: "Name of a new S3 bucket to create. Default is gtner-blog."
    Type: String
    Default: gtner-blog

Metadata:
  AWS::CloudFormation::Interface:
    ParameterLabels:
      AccountId:
        default: 12-digit AWS account id (or "default")
      BucketName:
        default: S3 Bucket Name

    ParameterGroups:
      - Label:
          default: General
        Parameters:
          - BucketName
          - AccountId

Conditions:
  SamLocal: !Not [!Equals [!Ref AccountId, default]]

Globals:
  Function:
    Timeout: 900
    MemorySize: 3008

Resources:
  S3Bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Ref BucketName
      PublicAccessBlockConfiguration:
        BlockPublicAcls: true
        IgnorePublicAcls: true
        BlockPublicPolicy: true
        RestrictPublicBuckets: true

  S3fsP38Layer:
    Type: AWS::Lambda::LayerVersion
    Properties:
      CompatibleRuntimes:
        - python3.8
      Content:
        S3Bucket: blog-groundtruth-comprehend-ner
        S3Key: lambda-layer-s3fs-p38.zip
      Description: "s3fs-0.4.2 and boto3_stubs[s3]-1.13.16.0"
      LicenseInfo: MIT

  ConverterFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: converter/
      Handler: app.lambda_handler
      Runtime: python3.8
      Layers:
        # Use the following form for actual deployment on AWS.
        - !Ref S3fsP38Layer

        # Use the following form for sam local.
        #- arn:aws:lambda:REGION:AAAAxxxxCCCC:layer:s3fs-p38-dev:1
      Policies:
        - S3FullAccessPolicy:
            BucketName: !Ref BucketName
      Events:
        NewAugmentedManifestEvent:
          Type: S3
          Properties:
            Bucket: !Ref S3Bucket
            Events: s3:ObjectCreated:*
            Filter:
              S3Key:
                # GroundTruth output: s3://<bucket>/<my-prefix>/<jobname>/manifests/output/output.manifest
                Rules:
                  - Name: suffix
                    Value: manifests/output/output.manifest

Outputs:
  ConverterFunction:
    Description: "Converter Lambda Function ARN"
    Value: !GetAtt ConverterFunction.Arn
  ConverterFunctionIamRole:
    Description: "Implicit IAM Role created for Converter function"
    Value: !GetAtt ConverterFunctionRole.Arn