Skip to content
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

cannot load config from separate file #2169 s #2773

Closed
greendavegreen opened this issue Nov 22, 2016 · 9 comments
Closed

cannot load config from separate file #2169 s #2773

greendavegreen opened this issue Nov 22, 2016 · 9 comments

Comments

@greendavegreen
Copy link

I just installed 1.2 and was going to try using environment variables AND moving sensitive items into a separate file that I can .gitignore.

I created a config.yml file to store the items subject to change. and then tried loading them into my serverless.yml.

contents of config.yml:


dropBucket: lobby.worky
keepBucket: keep.worky


and the opening of my serverless.yml

service: sls-python-demo

cfg: ${file(../configVars.yml)}

custom:
  dropBucet: lobby.worky
  keepBucket: keep.worky


provider:
  name: aws
  runtime: python2.7

  iamRoleStatements:
    - Effect: Allow
      Action:
        - s3:GetObject
      Resource: arn:aws:s3:::${self:cfg.dropBucket}/*
    - Effect: Allow
      Action:
        - s3:PutObject
      Resource: arn:aws:s3:::${self:custom.keepBucket}/*
    - Effect: Allow
      Action:
        - "sns:*"
      Resource: "*"

results in the error message:

  Serverless Error ---------------------------------------
 
     Trying to populate non string value into a string for
     variable ${self:cfg.dropBucket}. Please make sure the
     value of the property is a string.
 
  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
 
  Your Environment Information -----------------------------
     OS:                 darwin
     Node Version:       6.6.0
     Serverless Version: 1.2.0

If I change the troubled Resource statement to use self.custom.dropBucket instead of self.cfg.dropBucket, my deploy works fine. I would like to get the custom items out of this file and safely into separate file of info likely to change from deploy to deploy.

Similar or dependent issues:

@pmuens pmuens added the bug label Nov 22, 2016
@pmuens
Copy link
Contributor

pmuens commented Dec 6, 2016

Hmm. This should work according to https://serverless.com/framework/docs/providers/aws/guide/variables#reference-variables-in-other-files

Is your filename correct? In the description it says that the name is config.yml but in the serverless.yml it says configVars.yml.

@pmuens
Copy link
Contributor

pmuens commented Dec 13, 2016

@greendavegreen any updates on this one? Have you tried the suggestion above?

We've merged some changes recently. You might also want to check against the current master branch.

@camhart
Copy link

camhart commented Dec 19, 2016

I'm running 1.4 on a windows 10 box and I cannot get this to work either.

service: supercoolsoftware
frameworkVersion: "=1.4.0"

provider:
  name: aws
  runtime: dotnetcore1.0
  cfLogs: true
  region: us-west-2
  environment:
    exampleConfigValue: ${file(./env.yml)} #loads appropriate stage file from configs/*.json

env.yml is located in the root of the project, alongside serverless.yml. It's contents are as follows:

dev:
  message: hello there!

Edit: I've just realized mine failed because I'm trying to load something other than a string into an environment variable. Environment variables must be string values and objects are not accepted. Maybe add a warning about this to the user?

@gmadole
Copy link

gmadole commented Dec 31, 2016

@camhart I just discovered this too! Objects aren't allowed...

I'd like for when processing environment-variables, that if the property is an object to stringify it before adding it to the CF template. That way we can still add in objects (that may be dynamic in size/property names) to environment variables. Inside the app code we could handle the stringified object.

@massimocode
Copy link

massimocode commented Jan 13, 2017

We are facing the same issue. Trying to import a JSON file. Our file looks roughly like this:

functions:
  foo: ${file(functions/foo/s-function.json)}

And we get the error:

  Type Error ---------------------------------------------

     Cannot create property 'events' on string '${file(functions/foo/s-function.json)}'

     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues

     Please report this error. We think it might be a bug.

  Your Environment Information -----------------------------
     OS:                 win32
     Node Version:       6.5.0
     Serverless Version: 1.5.0

The json file is literally just {}

@hassankhan
Copy link
Contributor

hassankhan commented May 22, 2017

Have you tried this:

// functions/foo/s-function.json
{
  foo: {
    events: {}
  }
}

// serverless.yml
functions:
  foo: ${file(functions/foo/s-function.json):foo}

@jveldboom
Copy link

jveldboom commented Jul 19, 2017

You have to include the colon and object reference within the file being included. For example in YAML, but the same will apply in JSON:

# serverless.yml
functions:
   testAuth: ${file(routes.yml):testAuth}
   postArticles: ${file(routes.yml):postArticles}
# routes.yml
testAuth:
   handler: routes/test.handler
postArticles:
   handler: routes/postArticles.handler

We use this all throughout the serverless.yml file - not just within functions. Really helps keep the serverless.yml file size small and manageable.

@pmuens
Copy link
Contributor

pmuens commented Jan 15, 2019

This should've been fixed in the meantime.

Feel free to reopen if this is still an issue.

@pmuens pmuens closed this as completed Jan 15, 2019
@filipesmedeiros
Copy link

Hi! It appears that when I try to reference a file that is higher up in the directory, it's not able to reference it. Is that intended?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants