cfn/text-cm-update.yaml [77:239]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  RESULTBUCKET1:
    Type: 'AWS::S3::Bucket'
  CUSTOMLAMBDA:
    Type: 'AWS::CloudFormation::CustomResource'
    Properties:
      ServiceToken: !GetAtt 
        - CREATELAMBDA
        - Arn
      S3BucketName: !Ref RESULTBUCKET1
      Urls:
        - 'https://github.com/aws-samples/amazon-textract-and-amazon-comprehend-medical-claims-example/raw/master/artifacts/parse-desc.zip'
        - 'https://github.com/aws-samples/amazon-textract-and-amazon-comprehend-medical-claims-example/raw/master/artifacts/extract-queue.zip'
        - 'https://github.com/aws-samples/amazon-textract-and-amazon-comprehend-medical-claims-example/raw/master/artifacts/validate.zip'
  CREATELAMBDA:
    Type: 'AWS::Lambda::Function'
    Properties:
      Code:
        ZipFile: |
          import os
          import boto3
          import urllib.request
          import json
          import cfnresponse
          from urllib.parse import urlparse

          s3 = boto3.resource('s3')

          def save_to_local(url):
            urlPath = urlparse(url).path
            fileName = os.path.basename(urlPath)
            filePath = '/tmp/' + fileName
            urllib.request.urlretrieve(url, filePath)
            return filePath
            
          def upload_to_s3(filePath, bucket):
            fileName = os.path.basename(filePath)
            s3.Bucket(bucket).upload_file(filePath, fileName) 
            

          def copy_to_s3(url, bucket):
            filePath = save_to_local(url)
            upload_to_s3(filePath, bucket)

          def lambda_handler(event, context):
            properties = event['ResourceProperties']
            urls = properties['Urls']
            bucket = properties['S3BucketName']
            try:
              for url in urls:
                print(url)
                copy_to_s3(url, bucket)
                
            except Exception as e:
              print(e)
              responseData = {}
              responseData['Data'] = "Failure"
              cfnresponse.send(event, context, cfnresponse.FAILED,responseData, "CustomResourcePhysicalID")
              return
            responseData = {}
            responseData['Data'] = "Success"
            cfnresponse.send(event, context, cfnresponse.SUCCESS,responseData, "CustomResourcePhysicalID")
      Description: upload lambda zips
      FunctionName: blog-uploadzips10
      Handler: index.lambda_handler
      Role: !GetAtt 
        - BLOGROLE
        - Arn
      Timeout: 180
      Runtime: python3.7
      
  VALIDATE:
    Type: 'AWS::Lambda::Function'
    Properties:
      Code:
        S3Bucket: !Ref RESULTBUCKET1
        S3Key: validate.zip
      Description: validate lambda function
      FunctionName: blog-validate
      Handler: blog-validate.lambda_handler
      Environment:
        Variables:
          invalidqueue: !Ref INVALIDATEQUEUE
          resultbucket: !Ref RESULTBUCKET
          invalidsns: !Ref BLOGSNS
      Role: !GetAtt BLOGROLE.Arn
      Timeout: 120
      Runtime: python3.7

  PARSE:
    Type: 'AWS::Lambda::Function'
    Properties:
      Code:
        S3Bucket: !Ref RESULTBUCKET1
        S3Key: parse-desc.zip
      Description: parse lambda function
      FunctionName: blog-parse
      Handler: blog-parse.lambda_handler
      Role: !GetAtt BLOGROLE.Arn
      Timeout: 120
      Runtime: python3.7

  QUEUE:
    Type: 'AWS::Lambda::Function'
    Properties:
      Code:
        S3Bucket: !Ref RESULTBUCKET1
        S3Key: extract-queue.zip
      Description: extract lambda function
      FunctionName: blog-extract
      Handler: blog-extract.lambda_handler
      Environment:
        Variables:
          allqueue: !Ref ALLEQUEUE
      Role: !GetAtt BLOGROLE.Arn
      Timeout: 120
      Runtime: python3.7

  QUEUETRIGGER:
    Type: 'AWS::Lambda::EventSourceMapping'
    Properties:
      BatchSize: 10
      Enabled: true
      EventSourceArn: !GetAtt ALLEQUEUE.Arn
      FunctionName: !GetAtt VALIDATE.Arn


  BucketPermission:
    Type: 'AWS::Lambda::Permission'
    Properties:
      Action: 'lambda:InvokeFunction'
      FunctionName: !Ref QUEUE
      Principal: s3.amazonaws.com
      SourceAccount: !Ref 'AWS::AccountId'
      SourceArn: !GetAtt RESULTBUCKET.Arn
      

  ResultBucketPermission:
    Type: 'AWS::Lambda::Permission'
    Properties:
      Action: 'lambda:InvokeFunction'
      FunctionName: !Ref PARSE
      Principal: s3.amazonaws.com
      SourceAccount: !Ref 'AWS::AccountId'
      SourceArn: !GetAtt RESULTBUCKET.Arn



Outputs:
  LambdaFunctionExtract:
    Description: Lambda Function to extract key-value from form
    Value: !Ref QUEUE
    
  LambdaFunctionValidate:
    Description: Lambda Function to validate form
    Value: !Ref VALIDATE
    
  LambdaFunctionComprehendMedical:
    Description: Lambda Function to detect medical entities using comprehend medical
    Value: !Ref PARSE
    
  S3Bucket:
    Description: Bucket for input and Outputs
    Value: !Ref RESULTBUCKET
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



cfn/text-cm.yaml [59:221]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  RESULTBUCKET1:
    Type: 'AWS::S3::Bucket'
  CUSTOMLAMBDA:
    Type: 'AWS::CloudFormation::CustomResource'
    Properties:
      ServiceToken: !GetAtt 
        - CREATELAMBDA
        - Arn
      S3BucketName: !Ref RESULTBUCKET1
      Urls:
        - 'https://github.com/aws-samples/amazon-textract-and-amazon-comprehend-medical-claims-example/raw/master/artifacts/parse-desc.zip'
        - 'https://github.com/aws-samples/amazon-textract-and-amazon-comprehend-medical-claims-example/raw/master/artifacts/extract-queue.zip'
        - 'https://github.com/aws-samples/amazon-textract-and-amazon-comprehend-medical-claims-example/raw/master/artifacts/validate.zip'
  CREATELAMBDA:
    Type: 'AWS::Lambda::Function'
    Properties:
      Code:
        ZipFile: |
          import os
          import boto3
          import urllib.request
          import json
          import cfnresponse
          from urllib.parse import urlparse

          s3 = boto3.resource('s3')

          def save_to_local(url):
            urlPath = urlparse(url).path
            fileName = os.path.basename(urlPath)
            filePath = '/tmp/' + fileName
            urllib.request.urlretrieve(url, filePath)
            return filePath
            
          def upload_to_s3(filePath, bucket):
            fileName = os.path.basename(filePath)
            s3.Bucket(bucket).upload_file(filePath, fileName) 
            

          def copy_to_s3(url, bucket):
            filePath = save_to_local(url)
            upload_to_s3(filePath, bucket)

          def lambda_handler(event, context):
            properties = event['ResourceProperties']
            urls = properties['Urls']
            bucket = properties['S3BucketName']
            try:
              for url in urls:
                print(url)
                copy_to_s3(url, bucket)
                
            except Exception as e:
              print(e)
              responseData = {}
              responseData['Data'] = "Failure"
              cfnresponse.send(event, context, cfnresponse.FAILED,responseData, "CustomResourcePhysicalID")
              return
            responseData = {}
            responseData['Data'] = "Success"
            cfnresponse.send(event, context, cfnresponse.SUCCESS,responseData, "CustomResourcePhysicalID")
      Description: upload lambda zips
      FunctionName: blog-uploadzips10
      Handler: index.lambda_handler
      Role: !GetAtt 
        - BLOGROLE
        - Arn
      Timeout: 180
      Runtime: python3.7
      
  VALIDATE:
    Type: 'AWS::Lambda::Function'
    Properties:
      Code:
        S3Bucket: !Ref RESULTBUCKET1
        S3Key: validate.zip
      Description: validate lambda function
      FunctionName: blog-validate
      Handler: blog-validate.lambda_handler
      Environment:
        Variables:
          invalidqueue: !Ref INVALIDATEQUEUE
          resultbucket: !Ref RESULTBUCKET
          invalidsns: !Ref BLOGSNS
      Role: !GetAtt BLOGROLE.Arn
      Timeout: 120
      Runtime: python3.7

  PARSE:
    Type: 'AWS::Lambda::Function'
    Properties:
      Code:
        S3Bucket: !Ref RESULTBUCKET1
        S3Key: parse-desc.zip
      Description: parse lambda function
      FunctionName: blog-parse
      Handler: blog-parse.lambda_handler
      Role: !GetAtt BLOGROLE.Arn
      Timeout: 120
      Runtime: python3.7

  QUEUE:
    Type: 'AWS::Lambda::Function'
    Properties:
      Code:
        S3Bucket: !Ref RESULTBUCKET1
        S3Key: extract-queue.zip
      Description: extract lambda function
      FunctionName: blog-extract
      Handler: blog-extract.lambda_handler
      Environment:
        Variables:
          allqueue: !Ref ALLEQUEUE
      Role: !GetAtt BLOGROLE.Arn
      Timeout: 120
      Runtime: python3.7

  QUEUETRIGGER:
    Type: 'AWS::Lambda::EventSourceMapping'
    Properties:
      BatchSize: 10
      Enabled: true
      EventSourceArn: !GetAtt ALLEQUEUE.Arn
      FunctionName: !GetAtt VALIDATE.Arn


  BucketPermission:
    Type: 'AWS::Lambda::Permission'
    Properties:
      Action: 'lambda:InvokeFunction'
      FunctionName: !Ref QUEUE
      Principal: s3.amazonaws.com
      SourceAccount: !Ref 'AWS::AccountId'
      SourceArn: !GetAtt RESULTBUCKET.Arn
      

  ResultBucketPermission:
    Type: 'AWS::Lambda::Permission'
    Properties:
      Action: 'lambda:InvokeFunction'
      FunctionName: !Ref PARSE
      Principal: s3.amazonaws.com
      SourceAccount: !Ref 'AWS::AccountId'
      SourceArn: !GetAtt RESULTBUCKET.Arn



Outputs:
  LambdaFunctionExtract:
    Description: Lambda Function to extract key-value from form
    Value: !Ref QUEUE
    
  LambdaFunctionValidate:
    Description: Lambda Function to validate form
    Value: !Ref VALIDATE
    
  LambdaFunctionComprehendMedical:
    Description: Lambda Function to detect medical entities using comprehend medical
    Value: !Ref PARSE
    
  S3Bucket:
    Description: Bucket for input and Outputs
    Value: !Ref RESULTBUCKET
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



