HypeXR
HypeXR

Reputation: 711

Using env variables in serverless s-resources-cf.json

Is there a way to use serverless env variable in s-resources-cf.json?

I create a RDS instance in s-resources-cf.json that's used by some of my lambdas. Instead of putting the db name and password into s-project.json or s-variables-env.json I’d like to reference env vars and have them filled in as part of the deployment, similar to how vars in s-variables-env.json can be references in s-resources-cf.json using ${}.

Upvotes: 3

Views: 1537

Answers (1)

Matt D
Matt D

Reputation: 3496

You can put the ${rdsInstanceName} in the environment section of a function's s-function.json file, then access it using the process.env.MyRdsInstanceName within Lambda:

"environment": {
  "MyRdsInstanceName": "${rdsInstanceName}"
  ...
}

and reference this stage/region specific variable in your Lambda using something like:

var myRdsInstanceName = process.env.MyRdsInstanceName;

Hope this helps

Upvotes: 3

Related Questions