service: josh-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:
  create:
    # Defines an HTTP API endpoint that calls the main function in adduser.js
    # - path: url path is /josh/adduser
    # - method: GET request
    handler: adduser.main
    events:
      - http:
          path: josh/create
          method: post
          cors: true
          authorizer: aws_iam
  getusers:
    # Defines an HTTP API endpoint that calls the main function in getusers.js
    # - path: url path is /josh/getusers
    # - method: GET request
    handler: getusers.main
    events:
      - http:
          path: josh/getusers
          method: post
          cors: true
          authorizer: aws_iam
  update:
    # Defines an HTTP API endpoint that calls the main function in updateuser.js
    # - path: url path is /josh/updateuser
    # - method: GET request
    handler: updateuser.main
    events:
      - http:
          path: josh/update
          method: post
          cors: true
          authorizer: aws_iam
  get:
    # Defines an HTTP API endpoint that calls the main function in getdetails.js
    # - path: url path is /josh/getdetails
    # - method: POST request      
    handler: getdetails.main
    events:
      - http:
          path: josh/get
          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 /josh/getuseruuid
    # - method: GET request
    # - cognito must be logged in
    handler: getuseruuid.main
    events:
      - http:
          path: josh/getuuid
          method: post
          cors: true
          authorizer: aws_iam
  diagnose:
    # Defines an HTTP API endpoint that calls the main function in postxray.js
    # System user's basic details is recalled.
    # - path: url path is /josh/postxray
    # - method: GET request
    # - cognito must be logged in
    handler: postxray.main
    events:
      - http:
          path: josh/diagnose
          method: post
          cors: true
          authorizer: aws_iam
  
# Create our resources with separate CloudFormation templates
resources:
  # API Gateway Errors
  - ${file(resources/api-gateway-errors.yml)}