Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing an entire property in multiple serverless files - [object Object] does not exist

One of the biggest challenges that I've faced using serverless is in deploying AWS Lambda functions in a micro-service fashion (Each lambda individually - I've already tried individual packages, Webpack, and so on...).

I'm currently breaking my serverless app into multiple sub-serverless files and I'm trying to reference a main config serverless one. I'd like to inherit entire object trees so I don't have to be retyping them one by one (In addition, if there's a change, I can propagate it throughout all the lambdas).

Here's my current structure:

| serverless.yml
| lambda/
|       /planning
|                index.ts
|                serverless.yml
|       /generator
|                index.ts
|                serverless.yml
|       /createStudents
|                index.ts
|                serverless.yml

Content of the main serverless file (Omitted for brevity):

## https://serverless.com/framework/docs/providers/aws/guide/serverless.yml/
service: backend-appsync

provider:
  name: aws
  stage: ${opt:stage, 'dev'}
  runtime: nodejs10.x
  region: us-east-2
  ## https://serverless.com/framework/docs/providers/aws/guide/iam/
  ## https://serverless.com/blog/abcs-of-iam-permissions/
  iamRoleStatements:
    - Effect: Allow
      Action:
        - "dynamodb:BatchGetItem"
        - "dynamodb:BatchWriteItem"
        - "dynamodb:ConditionCheckItem"
        - "dynamodb:GetItem"
        - "dynamodb:DeleteItem"
        - "dynamodb:PutItem"
        - "dynamodb:Query"
      Resource: "arn:aws:dynamodb:us-east-2:747936726382:table/SchonDB"

I'd like to read the entire provider object and insert it into the individual serverless.yml file.

Example: /lambda/planning/serverless.yml

service: "planning"
provider: ${file(../../serverless.yml):provider}
functions:
  planning:
    handler: ./index.handler
    name: ${self:provider.stage}-planning
    description: Handles the Planning of every teacher.
    memorySize: 128

I get the following error:

Serverless Error ---------------------------------------

The specified provider "[object Object]" does not exist. Get Support -------------------------------------------- Docs: docs.serverless.com Bugs: github.com/serverless/serverless/issues Issues: forum.serverless.com

Your Environment Information --------------------------- Operating System: win32 Node Version: 12.14.1 Framework Version: 1.61.2 Plugin Version: 3.2.7 SDK Version: 2.2.1 Components Core Version: 1.1.2 Components CLI Version: 1.4.0

I thought I could reference the entire property. Is this possible? What am I doing wrong? Thanks :)

like image 947
Jose A Avatar asked Jan 18 '20 12:01

Jose A


1 Answers

Serverless goes nuts when files are imported from outside the project directory. To solve this problem, you can now use projectDir:

service: "planning"
projectDir: ../..
provider: ${file(../../serverless.yml):provider}
functions:
  planning:
    handler: ./index.handler
    name: ${self:provider.stage}-planning
    description: Handles the Planning of every teacher.
    memorySize: 128
like image 62
Jean-Baptiste Martin Avatar answered Sep 23 '22 07:09

Jean-Baptiste Martin