service: unity-lambda-api
# Create an optimized package for our functions
package:
  individually: true

plugins:
  - serverless-bundle # Package our functions with Webpack
  - serverless-offline
  - serverless-dotenv-plugin # Load .env as environment variables

provider:
  name: aws
  runtime: nodejs12.x
  stage: prod
  region: eu-west-1
  profile: unity
  #vpc:
  #  securityGroupIds:
  #    - ${env:SECURITYGROUPIDS}
  #  subnetIds:
  #    - ${env:SUBNETIDS1}
  #    - ${env:SUBNETIDS2}
  memorySize: 512

  # These environment variables are made available to our functions
  # under process.env.
  environment:
    tableName: loging
    databaseHost: ${env:DBHOST}
    databaseUser: ${env:DBUSERNAME}
    databasePass: ${env:DBPASSWORD}
    databaseName: ${env:DBNAME}

  # 'iamRoleStatements' defines the permission policy for the Lambda function.
  # In this case Lambda functions are granted with permissions to access DynamoDB.
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:DescribeTable
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
      Resource: "arn:aws:dynamodb:eu-west-1:*:*"

functions:
  # Defines an HTTP API endpoint that calls the main function in create.js
  # - path: url path is /notes
  # - method: POST request
  # - cors: enabled CORS (Cross-Origin Resource Sharing) for browser cross
  #     domain api call
  # - authorizer: authenticate using the AWS IAM role
  create:
    handler: create.main
    events:
      - http:
          path: unity/create
          method: post
          cors: true
          authorizer: aws_iam
  global:
    handler: global.main
    events:
      - http:
          path: unity
          method: post
          cors: true
          authorizer: aws_iam
  saveimport:
    handler: saveimport.main
    events:
      - http:
          path: unity/import
          method: post
          cors: true
          authorizer: aws_iam
  get:
    # Defines an HTTP API endpoint that calls the main function in get.js
    # - path: url path is /unity/{id}
    # - method: GET request
    handler: get.main
    events:
      - http:
          path: unity/{id}
          method: get
          cors: true
          authorizer: aws_iam
  getuser:
    # Defines an HTTP API endpoint that calls the main function in getuser.js
    # - path: url path is /unity/user/{id}
    # - method: GET request
    handler: getuser.main
    events:
      - http:
          path: unity/user/{id}
          method: get
          cors: true
          authorizer: aws_iam
  getusers:
    # Defines an HTTP API endpoint that calls the main function in getusers.js
    # - path: url path is /unity/users
    # - method: GET request
    handler: getusers.main
    events:
      - http:
          path: unity/getusers/{id}
          method: post
          cors: true
          authorizer: aws_iam
  getdetails:
    # Defines an HTTP API endpoint that calls the main function in getdetails.js
    # - path: url path is /unity/getdetails
    # - method: POST request      
    handler: getdetails.main
    events:
      - http:
          path: unity/getdetails
          method: post
          cors: true
          authorizer: aws_iam
  getuseruuid:
    # Defines an HTTP API endpoint that calls the main function in getuseruuid.js
    # System user's basic details is recalled.
    # - path: url path is /unity/getuseruuid
    # - method: GET request
    # - cognito must be logged in
    handler: getuseruuid.main
    events:
      - http:
          path: unity/uuid/{id}
          method: get
          cors: true
          authorizer: aws_iam
  tableall:
    # Defines an HTTP API endpoint that calls the main function in getuseruuid.js
    # System user's basic details is recalled.
    # - path: url path is /unity/getuseruuid
    # - method: POST request
    # - cognito must be logged in
    handler: tableall.main
    events:
      - http:
          path: unity/tableall
          method: POST
          cors: true
          authorizer: aws_iam
  addcalendarevent:
    # Defines an HTTP API endpoint that calls the main function in addcalendarevent.js
    # System user's basic details is recalled.
    # - path: url path is /unity/addcalendarevent
    # - method: POST request
    # - cognito must be logged in
    handler: addcalendarevent.main
    events:
      - http:
          path: unity/addcalendarevent
          method: POST
          cors: true
          authorizer: aws_iam
  getcalendarevents:
    # Defines an HTTP API endpoint that calls the main function in getcalendarevents.js
    # System user's basic details is recalled.
    # - path: url path is /unity/getcalendarevents
    # - method: POST request
    # - cognito must be logged in
    handler: getcalendarevents.main
    events:
      - http:
          path: unity/getcalendarevents
          method: POST
          cors: true
          authorizer: aws_iam
  removecalendarevent:
    # Defines an HTTP API endpoint that calls the main function in removecalendarevent.js
    # System user's basic details is recalled.
    # - path: url path is /unity/removecalendarevent
    # - method: POST request
    # - cognito must be logged in
    handler: removecalendarevent.main
    events:
      - http:
          path: unity/removecalendarevent
          method: POST
          cors: true
          authorizer: aws_iam
  updatecalendarevent:
    # Defines an HTTP API endpoint that calls the main function in updatecalendarevent.js
    # System user's basic details is recalled.
    # - path: url path is /unity/updatecalendarevent
    # - method: POST request
    # - cognito must be logged in
    handler: updatecalendarevent.main
    events:
      - http:
          path: unity/updatecalendarevent
          method: POST
          cors: true
          authorizer: aws_iam
# Create our resources with separate CloudFormation templates
resources:
  # API Gateway Errors
  - ${file(resources/api-gateway-errors.yml)}