service: FanSifter

plugins:
   - serverless-appsync-plugin
   - serverless-pseudo-parameters


provider:
  name: aws
  runtime: nodejs10.x
  stage: dev
  region: ${opt:region, 'eu-west-1'} #Default is us-east-1
  iamRoleStatements:
    - Effect: "Allow"
      Action:
        - "s3:ListBucket"
      Resource: "arn:aws:s3:::userfilesbucket-${self:provider.stage}"
    - Effect: "Allow"
      Action:
        - "s3:*"
      Resource: 
        - arn:aws:s3:::*
    - Effect: "Allow"
      Action:
       - "s3:GetObject"
       - "s3:PutObject"
      Resource: "arn:aws:s3:::userfilesbucket-${self:provider.stage}/*"
    - Effect: "Allow"
      Action:
       - "s3:GetBucketLocation"
       - "s3:GetObject"
       - "s3:AbortMultipartUpload"
       - "s3:PutObject"
      Resource: "arn:aws:s3:::${self:custom.awsathenalocation}/*"
    - Effect: "Allow"
      Action:
        - dynamodb:DescribeTable
        - dynamodb:Query
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
      Resource:
        - { "Fn::GetAtt": [ CustomerFilesTable, Arn ] }
        - Fn::Join:
          - "/"
          -
            - { "Fn::GetAtt": [ CustomerFilesTable, Arn ] }
            - "index/*"
        - { "Fn::GetAtt": [ CachedQueriesTable, Arn ] }
        - { "Fn::GetAtt": [ PiplDataTable, Arn ] }
    - Effect: "Allow"
      Action:
        - "glue:*"
      Resource:
        - "*"
    - Effect: "Allow"
      Action:
        - "athena:*"
      Resource:
        - "*"
    - Effect: Allow
      Action:
        - cognito-idp:AdminGetUser
      Resource: arn:aws:cognito-idp:*:*:*
    - Effect: 'Allow'
      Action:
        - ecs:RunTask
      Resource: '*'
    - Effect: Allow
      Action:
        - iam:PassRole
      Resource: ${self:custom.execRoleArn}
package:
  include:
    - constants/**
  exclude:
    - third-party/**
  

functions:
  getUploadUrl:
    handler: functions/getUploadUrl.handler
    environment:
      userUploadedFileBucket: userfilesbucket-${self:provider.stage}
      noCompiledFolder: nonCompiledData
      region: ${self:provider.region}
      customerFilesTable: ${self:custom.customerFilesTable}
      customerFilesIndexName: ${self:custom.customerFilesIndexName}
      USER_POOL_ID: # The name of the env var in your lambda
        Ref: CognitoUserPool # The name of your resource in Resources section

  getFileContents:
    handler: functions/getFileContents.handler
    environment:
      userUploadedFileBucket: userfilesbucket-${self:provider.stage}
      customerFilesTable: ${self:custom.customerFilesTable}
      USER_POOL_ID:
        Ref: CognitoUserPool
  
  dataLabels:
    handler: functions/dataLabels.handler

  mapFileWithLabels:
    handler: functions/mapFileWithLabels.handler
    environment:
      userUploadedFileBucket: userfilesbucket-${self:provider.stage}
      awsathenalocation: ${self:custom.awsathenalocation}
      customerFilesTable: ${self:custom.customerFilesTable}
      databasename: fansiftertestdatabase
      fileIdIndexName: ${self:custom.fileIdIndexName}

  linkFileOnUpload:
    handler: functions/linkFileOnUpload.handler
    events: 
      - s3:
          bucket: userfilesbucket-${self:provider.stage}
          event: s3:ObjectCreated:*
          existing: true
    environment:
      customerFilesTable: ${self:custom.customerFilesTable}
      customerFilesIndexName: ${self:custom.customerFilesIndexName}

  allDatasets:
    handler: functions/allDatasets.handler
    environment:
      awsathenalocation: ${self:custom.awsathenalocation}
      customerFilesTable: ${self:custom.customerFilesTable}
      databasename: fansiftertestdatabase
      USER_POOL_ID:
        Ref: CognitoUserPool
    timeout: 20
  
  dataFilters:
    handler: functions/dataFilters.handler
    environment:
      awsathenalocation: ${self:custom.awsathenalocation}
      customerFilesTable: ${self:custom.customerFilesTable}
      databasename: fansiftertestdatabase
      cachedQueriesTable: ${self:custom.cachedQueriesTable}
      USER_POOL_ID:
        Ref: CognitoUserPool
    timeout: 20
  
  dataAnalytics:
    handler: functions/dataAnalytics.handler
    environment:
      awsathenalocation: ${self:custom.awsathenalocation}
      customerFilesTable: ${self:custom.customerFilesTable}
      databasename: fansiftertestdatabase
      cachedQueriesTable: ${self:custom.cachedQueriesTable}
      USER_POOL_ID:
        Ref: CognitoUserPool
    timeout: 30
  
  saveSegment:
    handler: functions/saveSegment.handler
    environment:
      userUploadedFileBucket: userfilesbucket-${self:provider.stage}
      region: ${self:provider.region}
      awsathenalocation: ${self:custom.awsathenalocation}
      customerFilesTable: ${self:custom.customerFilesTable}
      databasename: fansiftertestdatabase
      USER_POOL_ID:
        Ref: CognitoUserPool
    timeout: 30
  
  dataSet:
    handler: functions/dataSet.handler
    environment:
      awsathenalocation: ${self:custom.awsathenalocation}
      customerFilesTable: ${self:custom.customerFilesTable}
      databasename: fansiftertestdatabase
      USER_POOL_ID:
        Ref: CognitoUserPool
  
  dataSummary:
    handler: functions/dataSummary.handler
    environment:
      awsathenalocation: ${self:custom.awsathenalocation}
      customerFilesTable: ${self:custom.customerFilesTable}
      databasename: fansiftertestdatabase
      cachedQueriesTable: ${self:custom.cachedQueriesTable}
      piplDataTable: ${self:custom.piplDataTable}
      USER_POOL_ID:
        Ref: CognitoUserPool
    timeout: 60
  
  piplSummary:
    handler: functions/piplSummary.handler
    environment:
      userUploadedFileBucket: userfilesbucket-${self:provider.stage}
      awsathenalocation: ${self:custom.awsathenalocation}
      customerFilesTable: ${self:custom.customerFilesTable}
      databasename: fansiftertestdatabase
      piplSummaryKey: ${ssm:pipl_demo_summary_key}
      piplDataTable: ${self:custom.piplDataTable}
      USER_POOL_ID:
        Ref: CognitoUserPool
    timeout: 10

  renameSegment:
    handler: functions/renameSegment.handler
    environment:
      databasename: fansiftertestdatabase
      customerFilesTable: ${self:custom.customerFilesTable}
      USER_POOL_ID:
        Ref: CognitoUserPool

  deleteSegment:
    handler: functions/deleteSegment.handler
    environment:
      databasename: fansiftertestdatabase
      customerFilesTable: ${self:custom.customerFilesTable}
      userUploadedFileBucket: userfilesbucket-${self:provider.stage}
      USER_POOL_ID:
        Ref: CognitoUserPool

  piplEnrich:
    handler: functions/piplEnrich.handler
    environment:
      userUploadedFileBucket: userfilesbucket-${self:provider.stage}
      awsathenalocation: ${self:custom.awsathenalocation}
      customerFilesTable: ${self:custom.customerFilesTable}
      databasename: fansiftertestdatabase
      piplBusinessKey: ${ssm:pipl_business_key}
      piplDataTable: ${self:custom.piplDataTable}
      fileIdIndexName: ${self:custom.fileIdIndexName}
      cachedQueriesTable: ${self:custom.stage}-cachedqueriestable
      USER_POOL_ID:
        Ref: CognitoUserPool
    timeout: 10
      
    


custom:
  accountId: #{AWS::AccountId}
  stage: ${opt:stage, self:provider.stage}
  execRoleArn: { "Fn::Join" : ["", [ "arn:aws:iam::", { "Ref" : "AWS::AccountId" }, ":role/ecsTaskExecutionRole" ] ] }

  customerFilesTable: ${self:custom.stage}-customerfilestable
  cachedQueriesTable: ${self:custom.stage}-cachedqueriestable
  piplDataTable: ${self:custom.stage}-piplDataTable
  customerFilesIndexName: customerfilestable-s3uri
  fileIdIndexName: customerfilestable-id
  awsathenalocation: athena-query-files
  
  appSync:
    name: sls-appsync-fansifter-api
    authenticationType: AMAZON_COGNITO_USER_POOLS
    userPoolConfig:
      awsRegion:  eu-west-1
      defaultAction: ALLOW
      userPoolId:  { Ref: CognitoUserPool } 
    mappingTemplates:
      - dataSource: awsLambdaGetUploadUrlDS
        type: Query #Query, Mutation, Subscription
        field: getUploadUrl
        request: "getUploadUrl-request-mapping-template.vtl"
        response: "getUploadUrl-response-mapping-template.vtl"
      - dataSource: awsLambdaGetFileContentDS
        type: Query #Query, Mutation, Subscription
        field: getUploadedFileContent
        request: "parseFile-request-mapping-template.vtl"
        response: "parseFile-response-mapping-template.vtl"
      - dataSource: awsLambdaMapFileWithLablelsDS
        type: Mutation
        field: mapFile
        request: "mapFile-request-mapping-template.vtl"
        response: "mapFile-response-mapping-template.vtl"
      - dataSource: awsLambdaallDatasets
        type: Query
        field: allDatasets
        request: "allDatasets-request-mapping-template.vtl"
        response: "allDatasets-response-mapping-template.vtl"
      - dataSource: awsLambdadataLabels
        type: Query
        field: dataLabels
        request: "dataLabels-request-mapping-template.vtl"
        response: "dataLabels-response-mapping-template.vtl"
      - dataSource: awsLambdadataFilters
        type: Query
        field: dataFilters
        request: "dataFilters-request-mapping-template.vtl"
        response: "dataFilters-response-mapping-template.vtl"
      - dataSource: awsLambdadataAnalytics
        type: Query
        field: dataAnalytics
        request: "dataAnalytics-request-mapping-template.vtl"
        response: "dataAnalytics-response-mapping-template.vtl"
      - dataSource: awsLambdasaveSegment
        type: Mutation
        field: saveSegment
        request: "saveSegment-request-mapping-template.vtl"
        response: "saveSegment-response-mapping-template.vtl"
      - dataSource: awsLambdadataSet
        type: Query
        field: dataSet
        request: "dataSet-request-mapping-template.vtl"
        response: "dataSet-response-mapping-template.vtl"
      - dataSource: awsLambdadataSummary
        type: Query
        field: dataSummary
        request: "dataSummary-request-mapping-template.vtl"
        response: "dataSummary-response-mapping-template.vtl"
      - dataSource: awsLambdapiplSummary
        type: Mutation
        field: piplSummary
        request: "piplSummary-request-mapping-template.vtl"
        response: "piplSummary-response-mapping-template.vtl"
      - dataSource: awsLambdapiplEnrich
        type: Mutation
        field: piplEnrich
        request: "piplEnrich-request-mapping-template.vtl"
        response: "piplEnrich-response-mapping-template.vtl"
      - dataSource: awsLambdarenameSegment
        type: Mutation
        field: renameSegment
        request: "renameSegment-request-mapping-template.vtl"
        response: "renameSegment-response-mapping-template.vtl"
      - dataSource: awsLambdadeleteSegment
        type: Mutation
        field: deleteSegment
        request: "deleteSegment-request-mapping-template.vtl"
        response: "deleteSegment-response-mapping-template.vtl"


    schema: appsyncSchema/schema.graphql
    serviceRole: "AppSyncServiceRole"
    dataSources:
      - type: AWS_LAMBDA
        name: awsLambdaGetUploadUrlDS
        description: 'Lambda DataSource'
        config:
          functionName: getUploadUrl
          #lambdaFunctionArn: { Fn::GetAtt: [GraphqlLambdaFunction, Arn] } # Where GraphqlLambdaFunction is the lambda function cloudformation resource created by serverless for the serverless function named graphql
          serviceRoleArn: { Fn::GetAtt: [AppSyncLambdaServiceRole, Arn] }
      - type: AWS_LAMBDA
        name: awsLambdaGetFileContentDS
        description: 'Lambda DataSource'
        config:
          functionName: getFileContents
          #lambdaFunctionArn: { Fn::GetAtt: [GraphqlLambdaFunction, Arn] } # Where GraphqlLambdaFunction is the lambda function cloudformation resource created by serverless for the serverless function named graphql
          serviceRoleArn: { Fn::GetAtt: [AppSyncLambdaServiceRole, Arn] }
      - type: AWS_LAMBDA
        name: awsLambdaMapFileWithLablelsDS
        description: 'Lambda DataSource'
        config:
          functionName: mapFileWithLabels
          #lambdaFunctionArn: { Fn::GetAtt: [GraphqlLambdaFunction, Arn] } # Where GraphqlLambdaFunction is the lambda function cloudformation resource created by serverless for the serverless function named graphql
          serviceRoleArn: { Fn::GetAtt: [AppSyncLambdaServiceRole, Arn] }
      - type: AWS_LAMBDA
        name: awsLambdaallDatasets
        description: 'Lambda DataSource'
        config:
          functionName: allDatasets
          #lambdaFunctionArn: { Fn::GetAtt: [GraphqlLambdaFunction, Arn] } # Where GraphqlLambdaFunction is the lambda function cloudformation resource created by serverless for the serverless function named graphql
          serviceRoleArn: { Fn::GetAtt: [AppSyncLambdaServiceRole, Arn] }
      - type: AWS_LAMBDA
        name: awsLambdadataLabels
        description: 'Lambda DataSource'
        config:
          functionName: dataLabels
          #lambdaFunctionArn: { Fn::GetAtt: [GraphqlLambdaFunction, Arn] } # Where GraphqlLambdaFunction is the lambda function cloudformation resource created by serverless for the serverless function named graphql
          serviceRoleArn: { Fn::GetAtt: [AppSyncLambdaServiceRole, Arn] }
      - type: AWS_LAMBDA
        name: awsLambdadataFilters
        description: 'Lambda DataSource'
        config:
          functionName: dataFilters
          #lambdaFunctionArn: { Fn::GetAtt: [GraphqlLambdaFunction, Arn] } # Where GraphqlLambdaFunction is the lambda function cloudformation resource created by serverless for the serverless function named graphql
          serviceRoleArn: { Fn::GetAtt: [AppSyncLambdaServiceRole, Arn] }
      - type: AWS_LAMBDA
        name: awsLambdadataAnalytics
        description: 'Lambda DataSource'
        config:
          functionName: dataAnalytics
          #lambdaFunctionArn: { Fn::GetAtt: [GraphqlLambdaFunction, Arn] } # Where GraphqlLambdaFunction is the lambda function cloudformation resource created by serverless for the serverless function named graphql
          serviceRoleArn: { Fn::GetAtt: [AppSyncLambdaServiceRole, Arn] }
      - type: AWS_LAMBDA
        name: awsLambdasaveSegment
        description: 'Lambda DataSource'
        config:
          functionName: saveSegment
          #lambdaFunctionArn: { Fn::GetAtt: [GraphqlLambdaFunction, Arn] } # Where GraphqlLambdaFunction is the lambda function cloudformation resource created by serverless for the serverless function named graphql
          serviceRoleArn: { Fn::GetAtt: [AppSyncLambdaServiceRole, Arn] }
      - type: AWS_LAMBDA
        name: awsLambdadataSet
        description: 'Lambda DataSource'
        config:
          functionName: dataSet
          #lambdaFunctionArn: { Fn::GetAtt: [GraphqlLambdaFunction, Arn] } # Where GraphqlLambdaFunction is the lambda function cloudformation resource created by serverless for the serverless function named graphql
          serviceRoleArn: { Fn::GetAtt: [AppSyncLambdaServiceRole, Arn] }
      - type: AWS_LAMBDA
        name: awsLambdadataSummary
        description: 'Lambda DataSource'
        config:
          functionName: dataSummary
          #lambdaFunctionArn: { Fn::GetAtt: [GraphqlLambdaFunction, Arn] } # Where GraphqlLambdaFunction is the lambda function cloudformation resource created by serverless for the serverless function named graphql
          serviceRoleArn: { Fn::GetAtt: [AppSyncLambdaServiceRole, Arn] }
      - type: AWS_LAMBDA
        name: awsLambdapiplSummary
        description: 'Lambda DataSource'
        config:
          functionName: piplSummary
          #lambdaFunctionArn: { Fn::GetAtt: [GraphqlLambdaFunction, Arn] } # Where GraphqlLambdaFunction is the lambda function cloudformation resource created by serverless for the serverless function named graphql
          serviceRoleArn: { Fn::GetAtt: [AppSyncLambdaServiceRole, Arn] }
      - type: AWS_LAMBDA
        name: awsLambdapiplEnrich
        description: 'Lambda DataSource'
        config:
          functionName: piplEnrich
          #lambdaFunctionArn: { Fn::GetAtt: [GraphqlLambdaFunction, Arn] } # Where GraphqlLambdaFunction is the lambda function cloudformation resource created by serverless for the serverless function named graphql
          serviceRoleArn: { Fn::GetAtt: [AppSyncLambdaServiceRole, Arn] }
      - type: AWS_LAMBDA
        name: awsLambdarenameSegment
        description: 'Lambda DataSource'
        config:
          functionName: renameSegment
          #lambdaFunctionArn: { Fn::GetAtt: [GraphqlLambdaFunction, Arn] } # Where GraphqlLambdaFunction is the lambda function cloudformation resource created by serverless for the serverless function named graphql
          serviceRoleArn: { Fn::GetAtt: [AppSyncLambdaServiceRole, Arn] }
      - type: AWS_LAMBDA
        name: awsLambdadeleteSegment
        description: 'Lambda DataSource'
        config:
          functionName: deleteSegment
          #lambdaFunctionArn: { Fn::GetAtt: [GraphqlLambdaFunction, Arn] } # Where GraphqlLambdaFunction is the lambda function cloudformation resource created by serverless for the serverless function named graphql
          serviceRoleArn: { Fn::GetAtt: [AppSyncLambdaServiceRole, Arn] }
      


      

resources:
  # Cognito
  - ${file(resources/cognito.yml)}
  # AppSync
  - ${file(resources/appsync.yml)}
  # S3
  - ${file(resources/s3-bucket.yml)}
  # DynamoDb
  - ${file(resources/dynamodb-table.yml)}
  - ${file(resources/dynamodb-table-cache.yml)}
  - ${file(resources/dynamodb-table-pipl-data.yml)}
      

