October 29, 2019 · AWS Lambda Serverless

Getting AWS Lambda Environment Variables in all supported languages

I've been using Amazon AWS quite a bit recently at my new job at Mercury FX and I am loving it.  I have been converting many functions that have been run from a single application into smaller Lambda functions that I can then schedule and trigger with other tools provided by AWS.

One of the first things I learnt about were Environment Variables.  They allow you to configure settings for a function without hardcoding them.  They look like this in the AWS Console:

Accessing these environment variables is different in every programming language supported by AWS Lambda as each language uses a native API for retrieving the environment variables instead of introducing a dependency for retrieving environment variables (yay no need for another dependency).  I have used a few languages in AWS Lambda while learning how to use it effectively and decided to create a post summarising how to retrieve environment variables in every language supported by AWS Lambda.

Table of Contents

C#

Environment.GetEnvironmentVariable("NAME_OF_VARIABLE");

Go

import ("os")
os.Getenv("NAME_OF_VARIABLE");

Java

System.getenv("NAME_OF_VARIABLE");

Node.js

process.env.NAME_OF_VARIABLE;

Powershell

$env:NAME_OF_VARIABLE

Python

import os
os.environ['NAME_OF_VARIABLE']

Ruby

ENV['NAME_OF_VARIABLE']

I hope you found this useful.  If I have left something out or made a mistake, please let me know in the comments!

References

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket