Environment Variables and Secrets
Prerequisites: npm install jaypie
Overview
Section titled âOverviewâJaypie uses environment variables with consistent naming conventions across infrastructure and runtime. Secrets are loaded from AWS Secrets Manager using a resolution pattern that supports local development.
Quick Reference
Section titled âQuick ReferenceâVariable Prefixes
Section titled âVariable Prefixesâ| Prefix | Purpose | Example |
|---|---|---|
PROJECT_ |
Application identity | PROJECT_ENV, PROJECT_KEY |
CDK_ENV_ |
Infrastructure resources | CDK_ENV_QUEUE_URL, CDK_ENV_BUCKET |
SECRET_ |
Secret references | SECRET_MONGODB_URI |
LOG_ |
Logging configuration | LOG_LEVEL, LOG_FORMAT |
Core Variables
Section titled âCore Variablesâ| Variable | Values | Description |
|---|---|---|
PROJECT_ENV |
local, sandbox, production | Environment identifier |
PROJECT_KEY |
string | Project identifier for logging |
PROJECT_NONCE |
string | Unique resource identifier |
NODE_ENV |
development, production, test | Node environment |
LOG_LEVEL |
trace, debug, info, warn, error | Logging threshold |
Environment Detection
Section titled âEnvironment Detectionâimport { isLocalEnv, isProductionEnv, isNodeTestEnv } from "jaypie";
if (isLocalEnv()) { // PROJECT_ENV === "local"}
if (isProductionEnv()) { // PROJECT_ENV === "production" OR PROJECT_PRODUCTION === "true"}
if (isNodeTestEnv()) { // NODE_ENV === "test"}Secrets Management
Section titled âSecrets ManagementâResolution Pattern
Section titled âResolution PatternâgetEnvSecret resolves secrets in order:
SECRET_{name}- Fetches from AWS Secrets Manager{name}_SECRET- Alternative, fetches from AWS Secrets Manager{name}- Direct value (used as-is)
import { getEnvSecret } from "jaypie";
// If SECRET_API_KEY="secrets/api-key", fetches from Secrets Manager// If API_KEY="sk-123", returns "sk-123" directlyconst apiKey = await getEnvSecret("API_KEY");Handler Secrets
Section titled âHandler SecretsâLoad secrets automatically in handlers:
import { expressHandler } from "jaypie";
export default expressHandler(handler, { secrets: ["MONGODB_URI", "API_KEY"],});// process.env.MONGODB_URI and process.env.API_KEY available in handlerManual Loading
Section titled âManual LoadingâLoad multiple secrets into process.env:
import { loadEnvSecrets } from "jaypie";
await loadEnvSecrets("MONGODB_URI", "API_KEY", "JWT_SECRET");// process.env.MONGODB_URI, etc. now populatedDirect Secret Fetch
Section titled âDirect Secret FetchâFetch a secret by its AWS Secrets Manager name:
import { getSecret } from "jaypie";
const dbUri = await getSecret("prod/mongodb-uri");CDK Infrastructure Variables
Section titled âCDK Infrastructure VariablesâQueue Configuration
Section titled âQueue Configurationâ// Runtimeimport { sendMessage } from "jaypie";
// Uses CDK_ENV_QUEUE_URL by defaultawait sendMessage({ event: "user.created", userId: "123" });// CDKnew JaypieQueuedLambda(this, "Worker", { code: "dist", handler: "worker.handler",});// Sets CDK_ENV_QUEUE_URL automaticallyBucket Configuration
Section titled âBucket Configurationâ// Environment variableprocess.env.CDK_ENV_BUCKET; // S3 bucket name
// CDKnew JaypieBucketQueuedLambda(this, "Processor", { bucketName: "uploads", // Sets CDK_ENV_BUCKET automatically});SNS Configuration
Section titled âSNS Configurationâ| Variable | Description |
|---|---|
CDK_ENV_SNS_TOPIC_ARN |
SNS topic ARN |
CDK_ENV_SNS_ROLE_ARN |
IAM role for SNS |
Local Development
Section titled âLocal DevelopmentâDirect Values
Section titled âDirect ValuesâFor local development, set values directly:
MONGODB_URI=mongodb://localhost:27017/myappAPI_KEY=test-api-keyPROJECT_ENV=localSecret References
Section titled âSecret ReferencesâIn deployed environments, use secret references:
# Environment (set by CDK)SECRET_MONGODB_URI=prod/mongodb-uriSECRET_API_KEY=prod/api-keyPROJECT_ENV=productionUsing env-cmd
Section titled âUsing env-cmdâ{ "scripts": { "dev": "env-cmd -f .env.local tsx watch src/server.ts" }}CDK Secrets Pattern
Section titled âCDK Secrets PatternâDeclaring Secrets
Section titled âDeclaring Secretsâimport { JaypieEnvSecret, JaypieLambda } from "@jaypie/constructs";
// Create secret referenceconst mongoSecret = new JaypieEnvSecret(this, "MongoSecret", { secretName: "prod/mongodb-uri", envName: "MONGODB_URI",});
// Lambda with secretnew JaypieLambda(this, "Api", { secrets: [mongoSecret],});// Lambda has SECRET_MONGODB_URI pointing to Secrets ManagerDatadog API Key
Section titled âDatadog API Keyânew JaypieLambda(this, "Api", { // CDK_ENV_DATADOG_API_KEY_ARN set automatically});Environment Variable Matrix
Section titled âEnvironment Variable Matrixâ| Variable | Local | Sandbox | Production |
|---|---|---|---|
PROJECT_ENV |
local | sandbox | production |
MONGODB_URI |
mongodb://localhost | (via secret) | (via secret) |
LOG_LEVEL |
trace | debug | info |
NODE_ENV |
development | production | production |
Testing Environment
Section titled âTesting Environmentâimport { isNodeTestEnv } from "jaypie";
// In tests, NODE_ENV=testif (isNodeTestEnv()) { // Skip external calls, use mocks}export default defineConfig({ test: { env: { NODE_ENV: "test", PROJECT_ENV: "local", }, },});Related
Section titled âRelatedâ- Handler Lifecycle - Secrets loading in handlers
- CDK Infrastructure - Setting up CDK constructs
- @jaypie/constructs - CDK construct reference
- CI/CD - Environment configuration in deployments