Loading Environment Variables into Pydantic Settings in AWS Lambda

Posted by Miguel Lopez on Tue 29 April 2025 in serverless

Introduction

Environment variables are a standard way to pass configuration to AWS Lambda functions. Pydantic's BaseSettings class makes it easy to load and validate these variables in your Python code. In this post, you'll learn how to use Pydantic to manage environment variables, and how to set them using Terraform.

Why Use Environment Variables with Pydantic?

  • Separation of config and code: Keep secrets and settings out of your codebase.
  • Validation: Pydantic ensures your environment variables are present and correctly typed.
  • Convenience: Access all your config in a single, well-typed object.

Example: Defining a Pydantic Settings Class

from typing import Union
from pydantic import BaseSettings

class _Settings(BaseSettings):
    SSM_RDS_HOST: Union[str, None] = None
    ENVIRONMENT: str

# Instantiate settings at module level
settings = _Settings()

With this setup, SSM_RDS_HOST and ENVIRONMENT will be loaded from environment variables if present.

Passing Environment Variables with Terraform

Here's how you can set environment variables for your Lambda function using Terraform:

resource "aws_lambda_function" "example" {
  function_name = "my_lambda"
  # ...existing code...
  environment {
    variables = {
      ENVIRONMENT    = "production"
      SSM_RDS_HOST   = "my-db-host.example.com"
    }
  }
}

Reading Environment Variables in Lambda Code

Now, in your Lambda handler, you can access these variables via your Pydantic settings:

from .settings import settings

def lambda_handler(event, context):
    print(f"Running in environment: {settings.ENVIRONMENT}")
    print(f"RDS Host: {settings.SSM_RDS_HOST}")
    # ...rest of your logic...

Conclusion

Using Pydantic's BaseSettings with environment variables in AWS Lambda is a clean and robust way to manage configuration. Combine this with Terraform to keep your infrastructure and application settings in sync. Try this pattern to simplify your Lambda deployments and configuration management!