Using environment variables

Environment variables in Lambda functions are exactly what they sound like: operating system-level key-value pairs that are dynamically passed to a function and set during instantiation for availability during runtime.

The idea behind environment variables is that you can modify the operational behaviors of the function's execution between your various environments. For example, you might have a different source database for development, test, and production environments that your function needs to communicate with. By abstracting the connection string into a variable, you are allowing the rest of the cost to be reused across each deployment. 

Environment variables are encrypted at rest by default using an AWS KMS key, and the Lambda service decrypts this information when it is passed in during the instantiation phase. You can also specify your own KMS key if you wish. It might sound like this would be a good place to store secrets to pass into the function, but there's a better method to do that, which we'll get to in the next section secrets management. Environment variables are best used to dynamically modify configuration.

Let's have a look at where to add an environment variable to our function using the management console:

  1. Jump into the Lambda console and find a function to use for this example.
  2. Scroll down your function page. You will find the following section: 

Screenshot of the environment variable configuration in the Lambda console

You can also set environment variables using the CLI. We can use the following command to add environment variables to an existing function:

aws lambda update-function-configuration 
--function-name MyPythonFunction
--environment Variables={dbPort=5432}

You can also specify environment variables when you're creating the function by using the same --environment option.

There's no limit to the number of environment variables you can set, but the total size of all key-pairs combined cannot exceed 4 KB.

Now, let's have a look at how we might access and set these variables for use during runtime. Each language is slightly different:

  • Node.js:
const DB_HOST = process.env.dbHost;
const DB_PORT = process.env.dbPort;
  • Python:
import os
DB_HOST = os.environ['dbHost']
DB_PORT = os.environ['dbPort']
  • Java:
final String dbhost = System.getenv("dbHost");
final String dbport = System.getenv("dbPort");
  • C#:
var dbHost = Environment.GetEnvironmentVariable("dbHost");
var dbPort = Environment.GetEnvironmentVariable("dbPort");

Using these simple steps is a great way to add extra environmental context to a function's execution. Now, you should have all you need to start using environment variables in your Lambda functions!

Next, we'll learn how to deal with a similar element of environmental context, that is, secrets management. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.189.228