serverless / compose

Orchestrate Serverless Framework in monorepos
https://serverless.com/framework/docs/guides/compose
MIT License
110 stars 15 forks source link

Variable Reference fails for files in service root (not at project root) #181

Closed amsand11 closed 1 year ago

amsand11 commented 1 year ago

Are you certain it's a bug?

Are you using the latest version?

Is there an existing issue for this?

Issue description

I have the following setup

# serverless-compose.yml
services:
  my-cool-service:
    path: my-cool-service
  another-service:
    path: another-service

# /my-cool-service/serverless.yml
provider:
  environment: 
    ENV_FOO: ${file(my-cool-service/serverless.localdev.yml):CONFIG_FOO}
    # ENV_FOO: ${file(/my-cool-service/serverless.localdev.yml):CONFIG_FOO}    # same error
    # ENV_FOO: ${file(./my-cool-service/serverless.localdev.yml):CONFIG_FOO}   # same error

# /my-cool-service/serverless.localdev.yml
CONFIG_FOO: foo

And this yields the following error:

"Cannot resolve serverless.yml: Variables resolution errored with: 
 - Cannot resolve variable at "provider.environment.ENV_FOO": Value not found at "file" source.

I would generally expect to have service specific config files, but thus far the only resolution I've been able to get working are files that live at the project root. This is a fine workaround for my limited/simple use case right now, but I often prefer to keep configuration items isolated to the specific "thing" (i.e. service) that needs them.

Service configuration (serverless-compose.yml) content

# serverless-compose.yml
services:
  my-cool-service:
    path: my-cool-service
  another-service:
    path: another-service

# /my-cool-service/serverless.yml
provider:
  environment: 
    ENV_FOO: ${file(my-cool-service/serverless.localdev.yml):CONFIG_FOO}
    # ENV_FOO: ${file(/my-cool-service/serverless.localdev.yml):CONFIG_FOO}    # same error
    # ENV_FOO: ${file(./my-cool-service/serverless.localdev.yml):CONFIG_FOO}   # same error

# /my-cool-service/serverless.localdev.yml
CONFIG_FOO: foo

Command name and used flags

serverless my-cool-service:print --stage localdev

Command output

Cannot resolve serverless.yml: Variables resolution errored with: 
 - Cannot resolve variable at "provider.environment.ENV_FOO": Value not found at "file" source.
amsand11 commented 1 year ago

Update: The problem is definitely between my ears.

It appears the root of the component service's execution in my example is actually at /my-cool-service.

So my file variable references should look like this for my-cool-service specific configuration:

provider:
  environment: 
    ENV_FOO: ${file(serverless.localdev.yml):CONFIG_FOO} 

and this to reference the project level configuration (config that might be shared across component services):

provider:
  environment: 
    ENV_BAR: ${file(../global.localdev.yml):CONFIG_BAR}