Prisma1: Add environment variable support to functions

Created on 13 Sep 2017  路  11Comments  路  Source: prisma/prisma1

Env variables can be added to a function with this syntax:

functions:
  myFunction:
    handler:
      code:
        src: ./code/myFunction.js
        environment:
          SECRET: ${env:ENV_SECRET}

Most helpful comment

They will be available as an environment variable in node like this: ${process.env.SECRET}

All 11 comments

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 .env file along with the graphcool.yml file for some sweet env support:

  1. Create a .env file in the root of your project, and add some variables:
TWITTER_KEY=XXXXXXX
TWITTER_SECRET=XXXXXXXX
  1. Edit your graphcool.yml to 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
  1. Install the dotenv npm module: yarn add --dev dotenv/npm install --save-dev dotenv
  2. Load the .env when you run graphcool deploy:
cd graphcool-dir
node -r dotenv/config $(which graphcool) deploy dotenv_config_path=../.env
  1. In your code, reference the env vars with process.env.TWITTER_KEY
Was this page helpful?
0 / 5 - 0 ratings

Related issues

notrab picture notrab  路  3Comments

MitkoTschimev picture MitkoTschimev  路  3Comments

marktani picture marktani  路  3Comments

thomaswright picture thomaswright  路  3Comments

sorenbs picture sorenbs  路  3Comments