AWS, Lambdas, Simple Parameter Store

How To Access Environment Variables For AWS Lambda

Introduction

How To Access Environment Variables For AWS Lambda
How To Access Environment Variables For AWS Lambda

We will go over How To Access Environment Variables For AWS Lambda.

Did you know that there’s two easy ways to do this?

We will break down this in the following sections:

  • How to access/list all AWS environment variables from Python
  • How to set new AWS environment variables from Python
  • How to edit existing AWS environment variables from Python

I have used this successfully in various projects and it works very well and has saved me a ton of time getting the outline draft for content creation. I’m going to give you two easy ways to do this.

We will go point by point on getting you up and running in less than 5mins, you do not need to have any programming knowledge to use the tool we are going to be developing here.

This is a complete guide and should cover all your questions on working with AWS environment variables to read, write, list and edit environment variables.

All code and examples on how to do this can be found in the Github link here.

Both methodologies will be described in detail. In this guide I’m going to skip the part on how you can upload and run your AWS Lambda. For this I have written a detailed guide with step by step instructions which you can find at the end of this article as reference if you need it.

As mentioned earlier we will be using the Python dotenv library file to help us with this. The load env file function will basically load everything from our env to the environment upon startup. This will then allow us to print out and dump all variables that are stored.

The code works as follows:

  • Load dotenv library into our file
  • Run the function that loads our .env file into the environment
  • Print out our environment to make sure this has gotten loaded properly.
import os
from dotenv import load_dotenv

def lambda_entry(event, context):
    load_dotenv()
    print(os.environ)

As seen above this code would work in both the AWS and locally in your Python environment.

We can also do the same thing by logging into the AWS console and checking the lambda tab. In the Lambda tab you will basically see a section called ‘Environment Variables‘. If you navigate to it you should be able to list all the environment variables.

How to List All Environment Variables In AWS Lambda Using AWS Console
How to List All Environment Variables In AWS Lambda Using AWS Console

As shown above we have added a test environment variable called TEST_VARIABLE_2. This off course will be used later on to start testing our code and see how it reacts to it.

In order to do this with Python we will be using the code as outline below. The process for this works as follows and it’s very similar to the code that lists all the environment variables.

The steps for this are the following:

  • Load the dotenv library
  • Run the library to attach the environment variables defined in our env file into the environment
  • Call putenv to add a variable in the environment.
import os
from dotenv import load_dotenv

def lambda_entry(event, context):
    load_dotenv()
    os.putenv('TEST_VARIABLE', 3)

By running this you will see the output that looks below. Basically it shows the variable being set when executed.

$ python ./add_variable.py
TEST_VARIABLE=3

The above verifies the TEST_VARIABLE got added in our environment path.

How to Add Environment Variables In AWS Lambda Using AWS Console

Similarly we can add an environment variable via the AWS console. If you navigate to Lambda -> Functions -> Your Lambda -> Edit environment variables as explained earlier you should be able to click to add a variable into your path.

This can be shown in the below screen.

The difference here is that you can make this variable sticky and you will be able to control it directly via the AWS environment. Furthermore since AWS offers KMS it allows you to add encryption in this variable which can cover in a separate article as this is beyond the scope of this. It’s also important to note that this is basically controlled entirely by AWS so that code will not work outside that environment. So if you are debugging this lambda locally it will not be exposed for you.

First we will cover with Python code how to delete environment variables in AWS Lambda Python functions. To do this we will need to follow similar steps as we did in the previous examples with a slight variation. The process of this is outlined below along with the code:

  • Load dotenv library into the Python file
  • Call the function that loads our .env file variables into the environment memory of our execution context
  • Call unsetenv to remove the variable from the environment memory.
import os
from dotenv import load_dotenv

def lambda_entry(event, context):
    load_dotenv()
    os.unsetenv('TEST_VARIABLE')

The code above demonstrates this and it’s important to know here that this works outside an AWS context too, so you can re-use it in your local running applications.

Similarly as before we can delete environment variables from the AWS Lambda function using the AWS console. The screenshot below shows to remove the variable from our path and this will make the change permanent.

As we described earlier when we add/remove variables using the AWS console this only reflects inside the AWS environment and ecosystem. Also the changes you make there are permanent and cannot be restored so be very careful when performing such operations. A recommendation will be to do this in a dev environment first or to ensure you take a backup or a screenshot of the existing variables so you can easily restore them if you need too.

How to Delete Environment Variables In AWS Lambda Using AWS Console
How to Delete Environment Variables In AWS Lambda Using AWS Console

Finally we will be covering how to edit environment variables in the AWS Lambda environment with both Python and AWS console methods.

How to Edit Environment Variables In AWS Lambda Using Python

The code to implement this operation is very similar to using two components that we used before:

  • First remove the environment variable
  • Second put it back in the environment

You can also directly edit this in your path and update it but this is the most reliable method that works in all operating systems so we will go with this.

We will be doing the same exact steps as before with the difference that in this case we will be calling unsetenv followed by putenv. The variable we will be changer in the code below is TEST_VARIABLE and we will be setting it to the value 3.

import os
from dotenv import load_dotenv

def lambda_entry(event, context):
    load_dotenv()
    os.unsetenv('TEST_VARIABLE')
    os.putenv('TEST_VARIABLE', '3')
    print(f'Test variable after edit: {os.getenv("TEST_VARIABLE")}')

How to Edit Environment Variables In AWS Lambda Using AWS Console

Below we will show how you can edit environment variables using the AWS console for your AWS Lambda function. In order to do this navigate to the edit environment variables as described above in the list environment variables section.

Once you are there you can select the environment variable you want to edit and simply go ahead and click on the edit button and make the adjustment in the value that you need to it. When the adjustment is made you can go ahead and save it and that change will permanently take place. Your environment variable will now be pointing to the new value that you have just set.

To verify this you can simply execute and test your AWS Lambda function which we are going to do below.

How to Edit Environment Variables In AWS Lambda Using AWS Console
How to Edit Environment Variables In AWS Lambda Using AWS Console

Testing Setting Environment Variables In AWS Lambda Using Python

As mentioned earlier we will now demonstrate how you can test this to ensure two things:
  • The environment variables we set earlier via our code is displayed properly
  • The environment variables we set via the AWS console in our AWS Lambda function is showing correctly too.
Testing Setting Environment Variables In AWS Lambda Using Python
Testing Setting Environment Variables In AWS Lambda Using Python
As shown above the variables are printed out properly in both cases. To ensure both of them worked we used different variables name above. If we examine the code we posted earlier both TEST_VARIABLE and TEST_VARIABLE_2 is there and they both contain different values. This successfully ensures that both methods that we discussed are functional and applicable in the AWS ecosystem.
In your projects you should use whichever method better suits your approach.
If you want a more dynamic/pythonic approach then you can use with the dotenv library as this will keep things native and your code will also work in your local environment without changes so testing and debugging will be easier.
If you want a more AWS specific route and you like features such as encryption using KMS then your best bet is to use the AWS environment to store your variables.
I personally switch between the two based on the project requirements and integrations we have. Generally speaking AWS is a better go to solution for me due to the extra features and it works with other programming languages too in my Lambdas.

Conclusion

We were able to successfully go over How To Access Environment Variables For AWS Lambda, hopefully I answered any questions you may have had and helped you get started on your quest on accessing environment variables in AWS.

If you found this useful and you think it may have helped you please drop me a cheer below I would appreciate it.

If you have any questions, comments please post them below or send me a note on my twitter. I check periodically and try to answer them in the priority they come in. Also if you have any corrections please do let me know and I’ll update the article with new updates or mistakes I did.

Which one you like better SSM or ENV parameters in AWS?

I personally still think both have it’s place but I prefer SSM parameters as they are embedded into the AWS ecosystem.

If you would like to visit the official Python Dotenv documentation here.

If you would like to find more articles related to AWS services: