Repository navigation
Closed
Milestone
Description
This is a (Bug Report)
Description
For bug reports:
- What went wrong?
I'm trying to use dynamic variables in serverless.yml and it fails.
Basically, I have the following in my provider:
environment:
SERVICE_NAME: ${self:service.name}
DYNAMODB_TABLE: ${self:service.name}-test
The SERVICE_NAME is correctly resolved IF I comment the DYNAMODB_TABLE: "${self:service.name}-test", otherwise it fails with the following stack trace.
If I use ${self:service.name} alone it works, but if I add some string after, then it fails: ${self:service.name}-test
- What did you expect should have happened?
Resolving variables correctly
- What stacktrace or error message from your provider did you see?
➜ simulator-feedback git:(master) ✗ sls print
Serverless: Load command run
Serverless: Load command config
Serverless: Load command config:credentials
Serverless: Load command create
Serverless: Load command install
Serverless: Load command package
Serverless: Load command deploy
Serverless: Load command deploy:function
Serverless: Load command deploy:list
Serverless: Load command deploy:list:functions
Serverless: Load command invoke
Serverless: Load command invoke:local
Serverless: Load command info
Serverless: Load command logs
Serverless: Load command login
Serverless: Load command logout
Serverless: Load command metrics
Serverless: Load command print
Serverless: Load command remove
Serverless: Load command rollback
Serverless: Load command rollback:function
Serverless: Load command slstats
Serverless: Load command plugin
Serverless: Load command plugin
Serverless: Load command plugin:install
Serverless: Load command plugin
Serverless: Load command plugin:uninstall
Serverless: Load command plugin
Serverless: Load command plugin:list
Serverless: Load command plugin
Serverless: Load command plugin:search
Serverless: Load command emit
Serverless: Load command config
Serverless: Load command config:credentials
Serverless: Load command rollback
Serverless: Load command rollback:function
Serverless: Load command webpack
Serverless Warning --------------------------------------
A valid service attribute to satisfy the declaration 'self:service.name' could not be found.
Serverless Warning --------------------------------------
A valid service attribute to satisfy the declaration 'self:service.name' could not be found.
Serverless Error ---------------------------------------
Trying to populate non string value into a string for variable ${self:service.name}. Please make sure the value of the property is a string.
Stack Trace --------------------------------------------
ServerlessError: Trying to populate non string value into a string for variable ${self:service.name}. Please make sure the value of the property is a string.
at Variables.populateVariable (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:156:13)
at pendingMatches.push.pendingMatch.then.matchedValue (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:124:21)
at runCallback (timers.js:789:20)
at tryOnImmediate (timers.js:751:5)
at processImmediate [as _immediateCallback] (timers.js:722:5)
From previous event:
at property.match.forEach (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:122:40)
at Array.forEach (<anonymous>)
at Variables.populateProperty (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:111:41)
at forEachLeaf (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:85:14)
at forEachLeaf (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:75:14)
at addContext (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:62:42)
at /Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/lodash/lodash.js:13392:38
at /Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/lodash/lodash.js:4917:15
at baseForOwn (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/lodash/lodash.js:3002:24)
at Function.mapValues (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/lodash/lodash.js:13391:7)
at forEachLeaf (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:73:37)
at addContext (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:62:42)
at /Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/lodash/lodash.js:13392:38
at /Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/lodash/lodash.js:4917:15
at baseForOwn (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/lodash/lodash.js:3002:24)
at Function.mapValues (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/lodash/lodash.js:13391:7)
at forEachLeaf (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:73:37)
at addContext (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:62:42)
at /Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/lodash/lodash.js:13392:38
at /Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/lodash/lodash.js:4917:15
at baseForOwn (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/lodash/lodash.js:3002:24)
at Function.mapValues (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/lodash/lodash.js:13391:7)
at forEachLeaf (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:73:37)
at Variables.populateObject (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:79:5)
at Variables.populateService (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:47:17)
at Serverless.run (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/Serverless.js:89:27)
at serverless.init.then (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/bin/serverless:42:50)
at <anonymous>
Get Support --------------------------------------------
Docs: docs.serverless.com
Bugs: github.com/serverless/serverless/issues
Forums: forum.serverless.com
Chat: gitter.im/serverless/serverless
Your Environment Information -----------------------------
OS: darwin
Node Version: 8.9.4
Serverless Version: 1.26.0
Similar or dependent issues:
Additional Data
- Operating System: MacOS X
Reacted by Phebe S
Metadata
Metadata
Assignees
Labels
No labels
Activity
Vadorequest commentedon Feb 15, 2018
Additional information: This seems to be related to the
envand particularself:servicemostly, for instance:Resource: "arn:aws:dynamodb:${self:provider.region}:035907498810:table/simulator-feedback-${self:provider.stage}"Resource: "arn:aws:dynamodb:${self:provider.region}:035907498810:table/${self:service.name}-${self:provider.stage}"Resource: "arn:aws:dynamodb:${self:provider.region}:035907498810:table/${env:SERVICE_NAME}-${self:provider.stage}"When using
self:provider, it seems to work, but when usingenv:orself:service.name, it just fails. Maybe it's not possible to actually get the value fromself:service? But I was following this official example so I assume it's possible but broken https://github.com/serverless/examples/blob/master/aws-node-rest-api-with-dynamodb/serverless.ymlI debug using the
sls printcommand.chrisoverzero commentedon Feb 15, 2018
I've found the "name" value available at
${self:service}. I agree that it's odd that it doesn't match the shape of the YAML, but it matches the shape of the value of the "service" field in ".serverless/serverless-state.json", so I suspect that's the input being used.I'd guess that this is for backwards compatibility reasons? The presence of "serviceObject" with the field "name" seems like evidence of that.
Vadorequest commentedon Feb 16, 2018
Actually, I tried again and you're right. Both
SERVICE_NAME: ${self:service}andSERVICE_NAME: ${self:service.name}work.And both fail when used inside a string:
"${self:service}-${self:provider.stage}""${self:service.name}-${self:provider.stage}"Also, when defining an
SERVICE_NAMEenvironment variable, those fail as well:"${env:SERVICE_NAME}-${self:provider.stage}""${self:provider.environment.SERVICE_NAME}-${self:provider.stage}"Failures seems to be related to the fact it's mixed up with string.
Vadorequest commentedon Feb 20, 2018
Could this get labeled/assigned? If you need more information, please let me know.
HyperBrain commentedon Feb 20, 2018
Can you try the same with the current master? I ask because #4713 just was merged, which cleans up variabe dependencies and also respects the use variables for region etc.
Vadorequest commentedon Feb 20, 2018
Yup, I can. I guess I just have to change my package.json to link to the github master branch instead of the npm package to do so?
HyperBrain commentedon Feb 20, 2018
Yes
"serverless": "github:serverless/serverless#master"Vadorequest commentedon Feb 20, 2018
Sumary
The PR seem to have fixed one use case when using
"${self:service}-${self:provider.stage}"(see Scenario 1). Possible backward breaking change added (See Scenario 2).Running tests with the following setup:
sls: refers to my globally npm installed package1.26.6./node_modules/serverless/bin/serverless: refers to my locally installed package, based on master branch at 39caddcScenario 1
serverless.ymlfile:Test with sls 1.26.6 => Failure (expected)
Test with sls on master branch (success => 💯 )
Scenario 2
N.B: added "self:service**.name**" compared to Scenario 1
Test with sls (Failure, expected)
Same output as Scenario 1 sls
Test with sls on master branch (failure => 👎 )
Scenario 3
I tried adding
test2: "${env:SERVICE_NAME}-${self:provider.stage}"incustomandprovider, both failed with the following message:I don't know how variables are intended to be used, but it looks like creating new variables based on existing environment variables just fail.
Scenario 4
I tried adding
test2: "${self:custom.test}-${self:provider.stage}"incustomandprovider, both failed with the following message:HyperBrain commentedon Feb 20, 2018
@Vadorequest Wow 💯 . Thanks for the very detailed report. That's really helpful.
@erikerikson Please have a look at the previous summary. Especially scenarios 3 and 4 are interesting, while scenario 3 might be an issue before the variable cleanup, I think scenario 4 is still something that is broken now.
erikerikson commentedon Feb 20, 2018
Thank you very much for these cases. Will definitely dive into them and fix. There's a lot of untangling to do but we have a better position to do so from.
erikerikson commentedon Feb 21, 2018
Please note that I have made some changes in the meantime. There was a bug I had missed.
I believe that I've completely solved that issue. Please accept my apologies. If you would like to use the code I am testing with, you can update your package.json with (the source for #4754):
"serverless": "github:erikerikson/serverless#4713-follow-up-fixes"That said, I've tested this out with the scenarios that you provided...
Scenario 1: when I run
I get a failure that I expect (
${self.service}is the object{ name: 'a-name' }):regardless of whether the #4754 patch is applied. I am a little confused by this given your report. Am I missing something important?
Senario 2: If I change the test slightly:
the test succeeds.
Scenario 3: If I add the environment variable and reference it in addition:
The test again succeeds.
Please let me know if what I've reported here is in line with your expectations and if not what your expectations are. I'm afraid that you were hit with a temporary state of the variables resolution. That or I am missing the full configuration required for reproduction.
If you could provide me with a full set of materials for reproduction, it would help very much. If not, if you could guide me in reproducing this issue, I will get it resolved.
Thank you very much!
Vadorequest commentedon Feb 21, 2018
@erikerikson thanks for the quick feedback.
Scenario A
This works fine.
Scenario B
This fails. (I still don't know if that's the desired behavior, it may be)
Scenario C
This fails silently,
SERVICE_NAMEisn't printed. But there are no exception thrown.Scenario D
This fails with exception :
I think we need to clearly define what we want to achieve here. What are our specs?
${self:service}and${self:service.name}behave the same? Or should me only allow${self:service}despite the fact it's not developer friendly when using the following format?customvariables depending onenvironmentvariable and vice-versa, we're going to have a problem with circular dependencies. Maybe there is an existing workflow, but I ain't aware of none.envvariables depend on each other? If so, does the order matters? (see Scenario D)erikerikson commentedon Feb 21, 2018
Oh... Scenarios C and D...
The
${env:*}variables referenceprocess.env, not${self:provider.environment}So if you
export NODE_ENV=foo(or set, if appropriate) the errors around your environment use should clear up.Thanks for these, I will look closer at these today.
Vadorequest commentedon Feb 21, 2018
Ahhhh, that explains why I couldn't make it work since the beginning :/
Good to know. Some of my tests were biased due to this misunderstanding. I thought both
${env:NODE_ENV}and${self:provider.environment.NODE_ENV}were equivalent.Maybe I missed something in the doc, or maybe the docs are missing something. :)
erikerikson commentedon Feb 21, 2018
process.envis mentioned here:https://serverless.com/framework/docs/providers/aws/guide/variables/#referencing-environment-variables
though it is definitely not prominent. so many docs to read these days... 😄
erikerikson commentedon Feb 21, 2018
On my latest...
Scenario A fails with:
If I add:
Then I see:
Scenario B (adding the same provider config) results in:
Printing the state of the "self"/service object results in:
Which explains the results of scenarios A and B. It's an odd transformation that is happening during service loading. This is done here: https://github.com/serverless/serverless/blob/master/lib/classes/Service.js#L118. Unfortunately, the VM rejects the addition of the name attribute on a string due to being in "strict" mode ('use strict' is at the head of that file). You can see this by popping the following into your browser console:
In chrome, I get:
Are there any other issues that I am missing besides the cognitive dissonance of the
${self:service}vs${self:service.name}issue?I suppose that we could go through the effort to remove 'use strict' and deal with the resulting consequences. Not sure how to prioritize this.
erikerikson commentedon Feb 21, 2018
@HyperBrain have an opinion about my last sentence? Or what the right path forward is for this issue, pending @Vadorequest's confirmation that the issue is understood?
HyperBrain commentedon Feb 21, 2018
serviceis special. Since we introduced the dual nature (string vs. object) a long time ago, the correct way in Serverless and plugins is to use service.getServiceObject() which will return it as object, regardless how it is defined in your yaml.As the yaml definition is a raw state before the service is parsed and structured into an object by the Service class, it might be insecure to use the yaml representation here. Thus I'm not sure how we should define the expected behavior.
Anybody tried to use
${self:service.service.name}already?HyperBrain commentedon Feb 21, 2018
Regarding the
use strict: The CLI is still required to be Node 4 compliant. As far as I remember, only Node 6 supports classes without specifying strict?dschep commentedon Feb 21, 2018
Here's a trick I just figured out for using
${self:service.name}using YAML references:Then use
${self:custom.service.name}instead.HyperBrain commentedon Feb 21, 2018
@dschep That's a nice one that qualifies to be added to the docs 😄
erikerikson commentedon Feb 22, 2018
Wow. Yeah. But I'd rather just fix it. 😄
Fix 4744 and associated
erikerikson commentedon Feb 23, 2018
The following adds support for the expected-to-be valid references below:
This is available on #4754 if you would like to test it out. Others, feel free to object.