AWS Lambda
Node.js
Environment Variables
AWS SDK
Cloud Functions

How get Environment Variables from lambda nodejs aws-sdk

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Inside a Node.js Lambda function, you do not use the AWS SDK to read the function's own environment variables. You read them directly from process.env. The AWS SDK is only needed if you want to fetch configuration from another AWS service, such as Systems Manager Parameter Store or Secrets Manager.

The Direct Answer: Use process.env

Lambda injects environment variables into the runtime process. In Node.js, that means they are available through process.env.

javascript
1export const handler = async () => {
2  const stage = process.env.STAGE;
3  const tableName = process.env.TABLE_NAME;
4
5  if (!stage || !tableName) {
6    throw new Error("Missing required environment variables");
7  }
8
9  return {
10    statusCode: 200,
11    body: JSON.stringify({ stage, tableName }),
12  };
13};

That is the normal and correct way to access Lambda environment variables. No AWS SDK call is required.

When the AWS SDK Is Relevant

The AWS SDK becomes relevant in two different situations:

  1. you want to set or update environment variables from outside the function
  2. you want to fetch secrets or configuration from another service at runtime

For example, an administrative script could inspect a function's configured environment variables through Lambda's control plane:

javascript
1import { LambdaClient, GetFunctionConfigurationCommand } from "@aws-sdk/client-lambda";
2
3const client = new LambdaClient({ region: "us-east-1" });
4const command = new GetFunctionConfigurationCommand({
5  FunctionName: "my-function",
6});
7
8const response = await client.send(command);
9console.log(response.Environment?.Variables);

That code is useful for tooling or deployment automation. It is not how your function should read its own runtime variables during normal execution.

Why process.env Is Better Inside the Function

Reading from process.env is immediate and local. It does not involve an extra network request, additional IAM permissions, or a dependency on the Lambda control plane.

That matters for both latency and reliability. If a configuration value already exists in the function environment, there is no reason to make an API call to ask AWS what the current process already knows.

Handling Missing Variables Safely

A common production pattern is to validate required variables during cold start or at the beginning of the handler.

javascript
1const required = ["STAGE", "TABLE_NAME"];
2
3for (const key of required) {
4  if (!process.env[key]) {
5    throw new Error(`Missing environment variable: ${key}`);
6  }
7}

Failing early is better than letting the function continue with undefined configuration and producing harder-to-debug downstream errors.

What About Secrets

Environment variables are convenient, but not every secret belongs there. For sensitive credentials that rotate or need centralized management, Secrets Manager or Parameter Store may be a better fit.

If you do fetch secrets dynamically, that is where the AWS SDK belongs.

javascript
1import { SSMClient, GetParameterCommand } from "@aws-sdk/client-ssm";
2
3const ssm = new SSMClient({ region: "us-east-1" });
4
5const result = await ssm.send(
6  new GetParameterCommand({
7    Name: "/my-app/db-password",
8    WithDecryption: true,
9  })
10);
11
12console.log(result.Parameter?.Value);

That example is not reading Lambda environment variables. It is reading a parameter from another AWS service.

Common Pitfalls

The most common mistake is reaching for the AWS SDK when process.env is already the right answer.

Another mistake is assuming a missing variable will become an empty string. In Node.js, an unset variable is typically undefined, so validate explicitly.

A third pitfall is storing too much configuration there. Lambda environment variables are useful, but they are not a replacement for a proper secret-management strategy.

Summary

  • Inside a Node.js Lambda function, read environment variables with process.env.
  • You do not need the AWS SDK to access the function's own runtime environment.
  • Use the AWS SDK only when managing function configuration externally or reading from other AWS services.
  • Validate required variables early so configuration mistakes fail fast.
  • Use Secrets Manager or Parameter Store when configuration needs stronger secret handling.

Course illustration
Course illustration

All Rights Reserved.