serverless / compose

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

Injecting two params with ${env: xxx} breaks compose #144

Closed mdial89f closed 2 years ago

mdial89f commented 2 years 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 created a public repo to demonstrate the issue: https://github.com/mdial89f/env-issue

To summarize: a compose file with the following contents will FAIL with "(node:69391) UnhandledPromiseRejectionWarning: ServerlessError: Unrecognized configuration variable sources: "env":

services:
  service-a:
    path: service-a
    params:
      one: ${env:USER}
      two: ${env:USER}

But if you comment out one of the params calling env, like the following contents, deployment succeeds:

services:
  service-a:
    path: service-a
    params:
      one: ${env:USER}
      # two: ${env:USER}

So, it seems you can only call env once in the compose file, for whatever reason.

To demonstrate the issue:

Service configuration (serverless-compose.yml) content

services:
  service-a:
    path: service-a
    params:
      one: ${env:USER}
      two: ${env:USER}

See repo linked above for more info.


### Command name and used flags

sls deploy --stage test

### Command output

```shell
(node:69391) UnhandledPromiseRejectionWarning: ServerlessError: Unrecognized configuration variable sources: "env"
    at resolveConfigurationVariables (/Users/mike/Projects/github/acehole123/env-issue/node_modules/@serverless/compose/src/configuration/resolve-variables.js:79:11)
    at resolveConfigurationVariables (/Users/mike/Projects/github/acehole123/env-issue/node_modules/@serverless/compose/src/configuration/resolve-variables.js:51:12)
    at resolveConfigurationVariables (/Users/mike/Projects/github/acehole123/env-issue/node_modules/@serverless/compose/src/configuration/resolve-variables.js:51:12)
    at runComponents (/Users/mike/Projects/github/acehole123/env-issue/node_modules/@serverless/compose/src/index.js:109:9)```
See repo linked above for more info.
pgrzesik commented 2 years ago

Hey @mdial89f - thanks a lot for reporting and sorry you've run into trouble. We've had a report of this issue already and it has been fixed today - I just published a new version that includes the fix - https://github.com/serverless/compose/releases/tag/v1.2.3

Let me know if that solves the problem for you.

mdial89f commented 2 years ago

Hey @mdial89f - thanks a lot for reporting and sorry you've run into trouble. We've had a report of this issue already and it has been fixed today - I just published a new version that includes the fix - https://github.com/serverless/compose/releases/tag/v1.2.3

Let me know if that solves the problem for you.

That fixed it! Shame on me, it turns out I was NOT on the latest version. Closing. Thanks for all the work you put into this project.

pgrzesik commented 2 years ago

Ha, you actually were on the latest at the time, as I published the v1.2.3 release a few minutes after your ticket was created.

Thanks for the confirmation that everything now works as expected 🙇

ada-cienciala commented 7 months ago

@mdial89f @pgrzesik I'm tagging here because it seems envs worked for you.

I'm trying to use the env:XXX syntax. I know there's already an issue #185 asking about useDotenv support but here I see you did not use it (and compose will cry if you try to).

Did serverless compose stop supporting this syntax? The documentation says it still can use it but after creating .env file, it does not recognize defined values.

# .env 
AWS_REGION=us-west-2
# serverless-compose.yml
services:
  serviceA:
    path: ...
    region: ${env:AWS_REGION}

I'm only getting the The environment variable "AWS_REGION" is referenced but is not defined error. Can you share if you did anything else to get this working?

I'm on serverless 3.34.0 and serverless-compose 1.3.0

mdial89f commented 7 months ago

@ada-cienciala What you have should work, provided that your .env file is being properly loaded. I'm not sure if serverless-compose has built in support for loading a .env file located in the current directory. We use a tool called direnv to load our .env files.. can't recommend it enough! It hooks into your shell and keeps people correctly configured.

ada-cienciala commented 7 months ago

Oh, I assumed serverless loads the .env files because of the To automatically load environment variables from .env files (with the help of the dotenv package), set useDotenv: true in serverless.yml line in the documentation.

But it makes sense they would not load it if you cannot use the useDonenv in the compose file. So you just load it yourself. That's super useful info, I did not think of that, thank you so much! 🤍