Wisdom of Jim
  • Blog
  • About Jim

A place to obtain the wisdom of Jim.

Environment Variable Quirks With Serverless And AWS Lambda

7/7/2019

Comments

 
Recently I have been working on some AWS Lambda functions, and I am using the serverless framework to help build and deploy them. However, once I wanted to have multiple stages with different environment variables I was not sure the best way to do it. Here are some things I tried and the command I ended up going with in the end. 

Environment Vars Not Deploying?

Initially I tried implementing a solution similar to the one found in this article. Initially, I thought, "wow, this is excellent because all I have to do is use the '--stage' flag and my environment variables will be taken care of for me since they are included as custom variables in the serverless.yaml config file". However, for some reason when I deployed my variables would never show up in the AWS Lambda console, and more importantly they weren't being loaded into the functions when they were run!

Serverless-Dotenv-Plugin

After some digging into forums and stack overflow posts the only working solution I was able to come up with was to use the serverless-dotenv-plugin. This nifty little npm package allows you to send a "--env" flag with your serverless commands and it will read from that corresponding .env.<environment> file. The annoying thing here is that you need to have a bunch of these .env files for each environment, but I guess it's probably safer to have your secret keys in there than being pushed up to git in your serverless.yaml file. 

Both --Stage And --Env

With the serverless-dotenv-plugin we need to pass the "--env" flag, and this is to specify from which .env file we want it to pull the environment variables. Although the "--stage" flag is no longer needed for environment variables, the serverless framework by default concatenates it onto the end of the function name and uses it for a few other things so I like to pass it in as a command line flag.  So, Here are examples of how I would run serverless commands with this setup:
sls deploy --stage prod --env prod
^ Deploys function to AWS with environment variables located in the .env.prod ​file.
sls offline start --port 5001 --stage local --env local
^ Starts a local server hosting your lambda functions with environment variables from .env.local ​file.
Comments
comments powered by Disqus

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Java
    Javascript
    Lean
    Life
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim

WoJ © 2015 - 2016