Getting AWS Lambda Environment Variables in all supported languages

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

ℹ️
Lambda now supports Docker images allowing you to use almost any language in a Lambda function. However, below, you'll find instructions on accessing environment variables in some popular programming languages.

I've been using Amazon AWS quite recently at my new job at Mercury FX, and I love it. I have been converting many functions 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 learned about was 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