-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle self
variable resolution
#49
Comments
Would love to have this one. Any works or considerations on how to have this one implemented?.. |
@roni-frantchi Can you describe your use case more? We are using this library in a lot of complex cases and haven't really had this requirement. |
@simlu In my case, I'm using the Serverless Framework, (in which to load, I use In Serverless, I have a DynamoDB set up like so: resources:
Resources:
ConfigurationTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${self:provider.environment.CONFIGURATION_TABLE_NAME}
... Which works great. For unit tests, I use jest-dynamodb, which requires an object identical in structure to the one I wrote on my I don't want to repeat this same setup, as I already have it in my Serverless yaml, so I figured I could just However, that doesn't work - Therefor my only option was to open this PR, and load the yaml using Serverless to be able to resolve the await serverless.init();
const service = await serverless.variables.populateService();
... It's works well enough, but is less than ideal because |
You should separate your config into multiple files and use the file require syntax. I don't think there is a reason here to use self? |
@simlu They are in fact separated to several files, but I can't see how that would help. provider:
environment:
CONFIGURATION_TABLE_NAME: MyTableName # this is resolved from a whole other `env.yml` file, inlining for simplicity of the example
# this is part is in `dynamodb.yaml` and is imported to `serverless.yml` using yaml-boost, which works great, I'm inlining it here just for simplicity
resources:
Resources:
ConfigurationTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${self:provider.environment.CONFIGURATION_TABLE_NAME}
... As I wrote the above, I'm guessing you suggest I refer from # in serverless.yml:
<<<:
- ${file(dynamodb.yml)}
provider:
environment:
CONFIGURATION_TABLE_NAME: MyTableName # this is resolved from a whole other `env.yml` file, inlining for simplicity of the example
# in dynamodb.yml:
resources:
Resources:
ConfigurationTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${file(serverless.yml).provider.environment.CONFIGURATION_TABLE_NAME}
... Even if it does work, I think in some cases you don't want to have separate files just for the sake of referencing a single variable - I usually separate base on other needs, not based on my need to refer between items, not sure that should be what drive my decision to split files. |
You have a weird use case as in that you want self to resolve, but not cf. How would it work when you mix them? Don't have a lot of time atm, will get back with more details later |
Spent some time thinking about how to best implement. Might find time to continue on a pr tomorrow. The change isn't hard, just also want to do some refactoring |
I'm thinking something like: module.exports.load = (filePath, vars = {}) => {
const result = loadRecursive(
path.dirname(filePath),
path.dirname(filePath),
yaml.safeLoad(fs.readFileSync(filePath, 'utf8')),
vars
);
// todo: resolve self rec
return result;
}; |
@roni-frantchi The biggest problem I have with the self resolution is that it's very complex since it can happen at the same time as file resolution. So one needs to continuously resolve. Example: provider:
name: aws
stage: ${opt:stage, 'dev'} # or this could be a file resolution
environment:
MY_SECRET: ${file(../config.${self:provider.stage}.json):CREDS} |
@simlu right, we would have to continue resolving file while not all self variables have been resolved or the same variable was already visited |
This is the missing feature that would make this package usable for me. I have various bits of configuration defined in my serverless.yml that I want to access in JS (instead of repeating the configuration). Example: custom:
cdn:
dev: https://dev.mycdn.com/${self:service}
prod: https://www.mycdn.com/${self:service} I suppose I could do something weird like putting the service name in a JS file, then using a variable for serverless's |
Should be relatively easy to implement. However can cause issues with recursion.
We might not want to support this and instead fix the readme?
The text was updated successfully, but these errors were encountered: