Env variables can be added to a function with this syntax:
functions:
myFunction:
handler:
code:
src: ./code/myFunction.js
environment:
SECRET: ${env:ENV_SECRET}
How can they be used in a function?
They will be available as an environment variable in node like this: ${process.env.SECRET}
Not as a context variable? By the way, I can't find the issue anymore in graphcool or graphcool-cli, but I made a proposal somewhere to be able to add those variables to the .graphcoolrc file (for different environments) as well as directly to the project file. If you are able to find that, please link it... Update: it was on Slack.
The idea was that you can use different values for different environments by adding them to the .graphcoolrc file.
graphcool.yml
My Webhook:
isEnabled: true
handler:
code:
src: ./code/send-email.js
variables:
env: ["MAILCHIMP_KEY"]
inline:
something_else: 'not env specific'
type: operationBefore
operation: Card.create
.graphcoolrc:
default: dev
environments:
dev:
projectId: cj.....
MAILCHIMP_KEY: abcdefg
test:
projectId: cj.....
MAILCHIMP_KEY: hijklmn
That's a good point! I actually prefer the context because it offers a better experience across all function handlers. I don't see the process.env approach working with webhooks.
Related to https://github.com/graphcool/graphcool/issues/229 and https://github.com/graphcool/graphcool/issues/219.
Any news on this?
For now, you can define environment variables for managed funtions in the graphcool.yml file with the new CLI version, for example here: https://github.com/graphcool/modules/blob/master/authentication/github/graphcool.yml#L10-L12
For webhooks, you will need to configure the env vars at the respective webhook provider, which is actually the norm. We will look into a better integration with external function providers, but this is our current approach.
After having used the process.env approach, I have to say it works quite nice. It's a joy to use 馃檪
That's a good point! I actually prefer the context because it offers a better experience across all function handlers.
You can't win 'm all... Anyways, are the variables replaced during deployment, or actually added to process.env when running the function?
FWIW, I have been using serverless for a few of my projects and they support importing of files in their serverless.yml.
If we were able to do this with graphcool.yml (you may already be able to, but I haven't checked) we could import a file called env.yml into the server environment as well as the graphcool.yml.
In the serverless.yml, this looks like:
custom:
stage: "${opt:stage, self:provider.stage}"
myTable: "${self:custom.stage}-my-table"
provider:
environment: ${file(env.yml):${self:custom.stage}}
resources:
Resources:
MyTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${self:custom.myTable}
The env.yml looks like:
dev:
MY_TABLE: "${self:custom.myTable}"
API_KEY: "Development API key"
prod:
MY_TABLE: "${self:custom.myTable}"
API_KEY: "Production API key"
This thread gives a more complete discussion of the topic.
This is a link to the serverless documentation on variables.
Edit: I guess I'm also wondering if we can configure graphcool.yml with environment variables... feel like this is a necessary feature for defining different environments in one config.
How do I handle different env variables across the same GC service definition deployed on two separate instances (ie. dev and production)? I also don't want to store sensitive information in graphcool.yml as it gets committed to source control.
For future googlers, I've found a solution I wrote up here: https://www.graph.cool/forum/t/how-to-use-environment-variables/1964/7?u=jesstelford
I've figured out how to use a
.envfile along with thegraphcool.ymlfile for some sweet env support:
- Create a
.envfile in the root of your project, and add some variables:TWITTER_KEY=XXXXXXX TWITTER_SECRET=XXXXXXXX
- Edit your
graphcool.ymlto add those env vars:functions: twitterAuth: handler: code: src: ./src/authentication/twitter.ts # Define environment variables to be used in function environment: TWITTER_KEY: ${env:TWITTER_KEY} TWITTER_SECRET: ${env:TWITTER_SECRET} type: resolver schema: ./src/authentication/twitter.graphql
- Install the
dotenvnpm module:yarn add --dev dotenv/npm install --save-dev dotenv- Load the
.envwhen you rungraphcool deploy:cd graphcool-dir node -r dotenv/config $(which graphcool) deploy dotenv_config_path=../.env
- In your code, reference the env vars with
process.env.TWITTER_KEY
Most helpful comment
They will be available as an environment variable in node like this:
${process.env.SECRET}