Pass in serverless variable to external js file

I have a the following serverless.yaml

functions: ${file(./generateFunctions.js):generate}

generateFunctions.js file looks something like this

let generate = () => {
      // This is what i need here
      // #{AWS::Region}
      // #{AWS::AccountId}
     // ${self:service}
     // ${opt:stage}

module.exports.generate = generate


I want this too. I don’t think thats how the js variable resolution works tho

@horike37 what do you think about passing in the current this.serverless context into the JS files?

1 Like

This is how i ended up solving it, but its a nasty hack i don’t like.

This is part of my serverless.yaml

service:  ${file(./../settingsSeverless.js):serviceName}

  name: aws
  runtime: nodejs6.10
  profile: impbots
  stage: ${file(./../settingsSeverless.js):apiVersion}
  region: ${file(./../settingsSeverless.js):region}
  role: arn:aws:iam:111:role/someRole
  # ./../settingsServerless.js also contains AWS accountId

That allows me to generate arns on the fly.

Interesting. Does it work? Could you share the file settingsSeverless.js?

@bill here it is, it works.

ic, you hardcoded it, not get the region name and account id on-fly.

With this, why not set directly, such as:

$ cat dev.yml
region: eu-central-1
serviceName: changeBot-api
accountId: 1111
apiVersion: v01

Then you will easily reference them, for example${self:custom.region} if you add below line in serverless.yml

custom: ${file(${opt:stage, self:provider.stage}.yml)}

By the way, region and accountid can be referenced directly

    - 'Fn::Join':
      - ':'
        - 'arn:aws:logs'
        - Ref: 'AWS::Region'
        - Ref: 'AWS::AccountId'
        - 'log-group:/aws/lambda/*:*:*'

That allows me to generate arns on the fly.

I agree your approach is a bit cleaner (but still a dirty hack)