What is the correct way of using variables from Javascript files?
Suppose I have one variable that is a resource ID, then I use this variable to find all other resources related to the first resource.
I was able to achieve it by looking at the implementation, but I believe it is not a safe way. For example:
serverless.yml:
service: example
provider:
name: aws
runtime: nodejs12.x
custom:
example: ${opt:example, "fallback"}
reversed: ${file(loadVars.js):reverse}
functions:
helloWorld:
handler: handler.helloWorld
environment:
REVERSED: ${self:custom.reversed}
events:
- http:
path: hello-world
method: get
cors: true
loadVars.js:
async function reverse (serverless) {
const exampleVar = serverless.service.custom.example
const exampleValue = await serverless.variables.populateValue(exampleVar, true)
if (!exampleValue) {
throw new Error("Not defined")
}
return exampleValue.split('').reverse().join('')
}
module.exports = {
reverse
}