Skip to main content

Environment Variables

MBC CQRS serverless framework comes with built-in support for environment variables, which allows you to do the following:

  • Use .env to load environment variables
  • Validate environment variables

Loading Environment Variables

MBC CQRS serverless framework has built-in support for loading environment variables from .env* files into process.env.

Core vs Application Variables

The environment variables listed below are categorized into two types:

  • Core Variables: Variables validated by the framework's base EnvironmentVariables class. These are essential for the framework to function.
  • Application Variables: Variables that your application may need based on which features you use. Add validation for these in your custom EnvValidation class.

The "Required" column indicates the general expectation for typical applications. You should add validation rules for all variables your specific application depends on.

Core Configuration

VariableDescriptionRequiredDefaultExample
NODE_ENVRunning environment: local, dev, stg, prodYes-local
APP_NAMEApplication name used for table prefixesYes-demo
APP_PORTApplication port for non-Lambda environmentsNo30003000
LOG_LEVELLog level: debug, verbose, info, warn, error, fatalYes-verbose
EVENT_SOURCE_DISABLEDDisable event source route for API Gateway integrationYes-false
REQUEST_BODY_SIZE_LIMITRequest body size limit for JSON and URL-encoded dataNo100kb100kb

AWS Credentials

VariableDescriptionRequiredExample
AWS_ACCESS_KEY_IDAWS access key ID for local developmentNolocal
AWS_SECRET_ACCESS_KEYAWS secret access key for local developmentNolocal
AWS_DEFAULT_REGIONDefault AWS regionNoap-northeast-1

DynamoDB Configuration

VariableDescriptionRequiredExample
DYNAMODB_ENDPOINTDynamoDB endpoint URL for local developmentNohttp://localhost:8000
DYNAMODB_REGIONDynamoDB regionNoap-northeast-1
ATTRIBUTE_LIMIT_SIZEMaximum size in bytes for DynamoDB item attributesYes389120

S3 Configuration

VariableDescriptionRequiredExample
S3_ENDPOINTS3 endpoint URL for local developmentNohttp://localhost:4566
S3_REGIONS3 regionNoap-northeast-1
S3_BUCKET_NAMES3 bucket name for storing large DynamoDB attributesYeslocal-bucket

Step Functions Configuration

VariableDescriptionRequiredExample
SFN_ENDPOINTStep Functions endpoint URL for local developmentNohttp://localhost:8083
SFN_REGIONStep Functions regionNoap-northeast-1
SFN_COMMAND_ARNStep Functions state machine ARN for command processingYesarn:aws:states:ap-northeast-1:101010101010:stateMachine:command

SNS Configuration

VariableDescriptionRequiredExample
SNS_ENDPOINTSNS endpoint URL for local developmentNohttp://localhost:4002
SNS_REGIONSNS regionNoap-northeast-1
SNS_TOPIC_ARNDefault SNS topic ARN for event notificationsYesarn:aws:sns:ap-northeast-1:101010101010:CqrsSnsTopic
SNS_ALARM_TOPIC_ARNSNS topic ARN for alarm notifications (error alerts)Noarn:aws:sns:ap-northeast-1:101010101010:AlarmSnsTopic

Cognito Configuration

VariableDescriptionRequiredExample
COGNITO_URLCognito endpoint URL for local developmentNohttp://localhost:9229
COGNITO_USER_POOL_IDCognito User Pool IDYeslocal_2G7noHgW
COGNITO_USER_POOL_CLIENT_IDCognito User Pool Client IDYesdnk8y7ii3wled35p3lw0l2cd7
COGNITO_REGIONCognito regionNoap-northeast-1

AppSync Configuration

VariableDescriptionRequiredExample
APPSYNC_ENDPOINTAppSync GraphQL endpoint URLNohttp://localhost:4001/graphql
APPSYNC_API_KEYAppSync API key for local developmentNoda2-fakeApiId123456

SES Email Configuration

VariableDescriptionRequiredExample
SES_ENDPOINTSES endpoint URL for local developmentNohttp://localhost:8005
SES_REGIONSES regionNoap-northeast-1
SES_FROM_EMAILDefault sender email addressYesemail@example.com

Database Configuration (Prisma)

VariableDescriptionRequiredExample
DATABASE_URLDatabase connection URL for Prisma ORMNomysql://root:RootCqrs@localhost:3306/cqrs?schema=public&connection_limit=1

Example .env file

# AWS Credentials
AWS_ACCESS_KEY_ID=local
AWS_SECRET_ACCESS_KEY=local
AWS_DEFAULT_REGION=ap-northeast-1

# Core Configuration
NODE_ENV=local
APP_NAME=demo
APP_PORT=3000
LOG_LEVEL=verbose
EVENT_SOURCE_DISABLED=false
REQUEST_BODY_SIZE_LIMIT=100kb

# DynamoDB Configuration
DYNAMODB_ENDPOINT=http://localhost:8000
DYNAMODB_REGION=ap-northeast-1
ATTRIBUTE_LIMIT_SIZE=389120

# S3 Configuration
S3_ENDPOINT=http://localhost:4566
S3_REGION=ap-northeast-1
S3_BUCKET_NAME=local-bucket

# Step Functions Configuration
SFN_ENDPOINT=http://localhost:8083
SFN_REGION=ap-northeast-1
SFN_COMMAND_ARN=arn:aws:states:ap-northeast-1:101010101010:stateMachine:command

# SNS Configuration
SNS_ENDPOINT=http://localhost:4002
SNS_REGION=ap-northeast-1
SNS_TOPIC_ARN=arn:aws:sns:ap-northeast-1:101010101010:CqrsSnsTopic
SNS_ALARM_TOPIC_ARN=arn:aws:sns:ap-northeast-1:101010101010:AlarmSnsTopic

# Cognito Configuration
COGNITO_URL=http://localhost:9229
COGNITO_USER_POOL_ID=local_2G7noHgW
COGNITO_USER_POOL_CLIENT_ID=dnk8y7ii3wled35p3lw0l2cd7
COGNITO_REGION=ap-northeast-1

# AppSync Configuration
APPSYNC_ENDPOINT=http://localhost:4001/graphql
APPSYNC_API_KEY=da2-fakeApiId123456

# SES Configuration
SES_ENDPOINT=http://localhost:8005
SES_REGION=ap-northeast-1
SES_FROM_EMAIL=email@example.com

# Database Configuration
DATABASE_URL="mysql://root:RootCqrs@localhost:3306/cqrs?schema=public&connection_limit=1"

Validate Environment Variables

It is standard practice to throw an exception during application startup if required environment variables haven't been provided or if they don't meet certain validation rules. The @mbc-cqrs-serverless/core package makes this easy to do.

First, we have to define:

  • a class with validation constraints,
  • extend the EnvironmentVariables class.
// env.validation.ts
import { EnvironmentVariables } from "@mbc-cqrs-serverless/core";
import { IsUrl } from "class-validator";

export class EnvValidation extends EnvironmentVariables {
@IsUrl({
require_tld: false,
})
FRONT_BASE_URL: string;
}

With this in place, you pass the EnvValidation class as a configuration argument of the createHandler function, as follows:

import { createHandler } from "@mbc-cqrs-serverless/core";

import { EnvValidation } from "./env.validation";
import { MainModule } from "./main.module";

export const handler = createHandler({
rootModule: MainModule,
envCls: EnvValidation,
});