This guide provides comprehensive information about configuring the React Starter application and its AWS CDK infrastructure.
The React application configuration is managed through environment variables prefixed with VITE_. These variables are available at build time and runtime in your React components.
The following environment variables are available for configuring the React application:
| Variable | Type | Description | Default | Required |
|---|---|---|---|---|
VITE_BASE_URL_API |
string | Base URL for API requests | - | Yes |
VITE_TOAST_AUTO_DISMISS_MILLIS |
number | Auto-dismiss duration for toast notifications (ms) | 5000 |
No |
VITE_BUILD_DATE |
string | Build date (YYYY-MM-DD format) | - | No |
VITE_BUILD_TIME |
string | Build time (HH:MM:SS format) | - | No |
VITE_BUILD_TS |
string | Build timestamp (ISO 8601 format) | - | No |
VITE_BUILD_COMMIT_SHA |
string | Git commit SHA of the build | - | No |
VITE_BUILD_ENV_CODE |
string | Environment code (e.g., local, dev, qa, prd) |
- | No |
VITE_BUILD_WORKFLOW_NAME |
string | CI/CD workflow name | - | No |
VITE_BUILD_WORKFLOW_RUN_NUMBER |
string | CI/CD workflow run number | - | No |
VITE_BUILD_WORKFLOW_RUN_ATTEMPT |
string | CI/CD workflow run attempt number | - | No |
-
Copy the example configuration file:
cp .env.example .env
-
Update variables for your environment:
VITE_BASE_URL_API=https://your-api.example.com VITE_TOAST_AUTO_DISMISS_MILLIS=5000
-
Build information (typically set by CI/CD pipeline):
VITE_BUILD_DATE=2026-02-10 VITE_BUILD_TIME=14:30:00 VITE_BUILD_TS=2026-02-10T14:30:00Z VITE_BUILD_COMMIT_SHA=abc123def456 VITE_BUILD_ENV_CODE=dev VITE_BUILD_WORKFLOW_NAME=Build VITE_BUILD_WORKFLOW_RUN_NUMBER=42 VITE_BUILD_WORKFLOW_RUN_ATTEMPT=1
Application configuration values are accessed through the config utility, which provides type-safe, validated configuration throughout your React components and utilities. The utility validates all environment variables at runtime using Zod schema validation, ensuring type safety and early error detection.
Import the config object from the common utilities:
import { config } from 'common/utils/config';
// API base URL (type-safe string)
const apiUrl = config.VITE_BASE_URL_API;
// Toast settings (type-safe number)
const toastDuration = config.VITE_TOAST_AUTO_DISMISS_MILLIS;
// Build information (type-safe strings)
const buildDate = config.VITE_BUILD_DATE;
const buildCommit = config.VITE_BUILD_COMMIT_SHA;
const envCode = config.VITE_BUILD_ENV_CODE;Benefits of using the config utility:
- Type Safety: All configuration values are validated against a Zod schema, ensuring correct types
- Validation: Environment variables are validated on application startup, catching missing or invalid configuration early
- IDE Support: Full TypeScript autocomplete and type checking for configuration values
- Single Source of Truth: Configuration is centralized and consistently accessed throughout the application
Configuration Schema Location:
The Zod schema that validates all environment variables is defined in src/common/utils/config.ts. This file also exports the Config type for use in type annotations when needed.
For local development, create a .env file in the root directory with local values:
VITE_BASE_URL_API=http://localhost:3000
VITE_TOAST_AUTO_DISMISS_MILLIS=5000
VITE_BUILD_DATE=1970-01-01
VITE_BUILD_TIME=00:00:00
VITE_BUILD_TS=1970-01-01T00:00:00Z
VITE_BUILD_COMMIT_SHA=local
VITE_BUILD_ENV_CODE=local
VITE_BUILD_WORKFLOW_NAME=local
VITE_BUILD_WORKFLOW_RUN_NUMBER=1
VITE_BUILD_WORKFLOW_RUN_ATTEMPT=1For running unit tests, create a .env.test.local file in the root directory with values optimized for the test environment. This configuration is used by Vitest when running unit tests.
-
Copy the example configuration file:
cp .env.example .env.test.local
-
Update with test-specific values:
# Provided by Pipeline (Simulated) VITE_BUILD_DATE=1970-01-01 VITE_BUILD_TIME=00:00:00 VITE_BUILD_TS=1970-01-01T00:00:00Z VITE_BUILD_COMMIT_SHA=test VITE_BUILD_ENV_CODE=test VITE_BUILD_WORKFLOW_NAME=test VITE_BUILD_WORKFLOW_RUN_NUMBER=1 VITE_BUILD_WORKFLOW_RUN_ATTEMPT=1 # API Configuration # Use JSONPlaceholder for mock API testing VITE_BASE_URL_API=https://jsonplaceholder.typicode.com # Toasts Configuration # Use shorter duration in tests for faster test execution VITE_TOAST_AUTO_DISMISS_MILLIS=1500
- API Base URL: Uses JSONPlaceholder - a free fake REST API for testing. In actual test environments, API calls are typically mocked using MSW (Mock Service Worker).
- Toast Duration: Reduced to 1500ms for faster test execution while still allowing time for async operations.
- Build Information: All build-related variables are set to static test values, ensuring consistent snapshots and reproducible test results.
- Environment Code: Set to
testto distinguish test runs from development and production.
The test configuration works seamlessly with MSW (Mock Service Worker), which intercepts HTTP requests in tests. The VITE_BASE_URL_API can point to a test server, and MSW handlers will intercept and mock responses:
// Example MSW handler in tests
import { http, HttpResponse } from 'msw';
import { setupServer } from 'msw/node';
const server = setupServer(
http.get('https://jsonplaceholder.typicode.com/users', () => {
return HttpResponse.json([{ id: 1, name: 'Test User' }]);
}),
);
beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());The infrastructure configuration is managed through environment variables prefixed with CDK_. These variables control how AWS resources are provisioned and deployed using the AWS CDK.
The following environment variables are available for configuring the infrastructure:
| Variable | Type | Description | Default | Required |
|---|---|---|---|---|
CDK_APP_NAME |
string | Application name used in resource naming and tagging | react-starter |
No |
CDK_ENV |
enum | Deployment environment: dev, qa, prd |
- | Yes |
CDK_ACCOUNT |
string | AWS account ID for deployment | Auto-detected from AWS CLI | No |
CDK_REGION |
string | AWS region for deployment (e.g., us-east-1) |
Auto-detected from AWS CLI | No |
CDK_OU |
string | Organizational Unit for resource tagging | unknown |
No |
CDK_OWNER |
string | Owner tag for resource tracking | unknown |
No |
CDK_ASSET_PATH |
string | Path to application build artifacts (relative to infrastructure directory) | ../dist |
No |
CDK_DOMAIN_NAME |
string | Custom domain name for the application CDN (e.g., app.example.com) |
- | No |
CDK_CERTIFICATE_ARN |
string | ACM certificate ARN for custom domain (must be in us-east-1 for CloudFront) |
- | No |
CDK_HOSTED_ZONE_ID |
string | Route53 hosted zone ID for DNS records | - | No |
CDK_HOSTED_ZONE_NAME |
string | Route53 hosted zone name (e.g., example.com) |
- | No |
CDK_STORYBOOK_ASSET_PATH |
string | Path to Storybook build artifacts (relative to infrastructure directory) | ../storybook-static |
No |
CDK_STORYBOOK_DOMAIN_NAME |
string | Custom domain name for Storybook CDN | - | No |
CDK_STORYBOOK_CERTIFICATE_ARN |
string | ACM certificate ARN for Storybook custom domain | - | No |
CDK_STORYBOOK_HOSTED_ZONE_ID |
string | Route53 hosted zone ID for Storybook DNS records | - | No |
CDK_STORYBOOK_HOSTED_ZONE_NAME |
string | Route53 hosted zone name for Storybook | - | No |
-
Navigate to the infrastructure directory:
cd infrastructure -
Copy the example configuration file:
cp .env.example .env
-
Update variables for your environment:
CDK_ENV=dev CDK_APP_NAME=react-starter CDK_ACCOUNT=123456789012 CDK_REGION=us-east-1 CDK_OU=software-engineering CDK_OWNER=your-team
Infrastructure configuration is loaded and validated through the getConfig() function in infrastructure/utils/config.ts:
import { getConfig, getTags, getEnvironmentConfig } from './utils/config';
const config = getConfig();
const tags = getTags(config);
const envConfig = getEnvironmentConfig(config);
console.log(`Environment: ${config.CDK_ENV}`);
console.log(`App name: ${config.CDK_APP_NAME}`);
console.log(`Tags:`, tags);Infrastructure configuration can be provided through:
- Environment variables - Set directly in your shell or CI/CD pipeline (highest priority)
- .env file - Create a
.envfile in theinfrastructure/directory for local development - Default values - Specified in the Zod schema (lowest priority)
All AWS resources created by the CDK are automatically tagged with the following tags:
| Tag | Description | Source |
|---|---|---|
App |
Application name | CDK_APP_NAME |
Env |
Environment (dev, qa, prd) |
CDK_ENV |
OU |
Organizational Unit | CDK_OU |
Owner |
Team or individual responsible | CDK_OWNER |
These tags are used for cost allocation, resource management, and identifying resources in AWS.
Create infrastructure/.env for development deployment:
###############################################
## Development Environment Configuration
###############################################
CDK_APP_NAME=react-starter
CDK_ENV=dev
CDK_ACCOUNT=123456789012
CDK_REGION=us-east-1
### Resource Tagging ###
CDK_OU=software-engineering
CDK_OWNER=frontend-team
### CDN Configuration ###
CDK_ASSET_PATH=../dist
CDK_DOMAIN_NAME=react-starter.dev.example.com
CDK_CERTIFICATE_ARN=arn:aws:acm:us-east-1:123456789012:certificate/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
CDK_HOSTED_ZONE_ID=Z1234567890ABC
CDK_HOSTED_ZONE_NAME=dev.example.com
### Storybook CDN Configuration ###
CDK_STORYBOOK_ASSET_PATH=../storybook-static
CDK_STORYBOOK_DOMAIN_NAME=react-starter-storybook.dev.example.com
CDK_STORYBOOK_CERTIFICATE_ARN=arn:aws:acm:us-east-1:123456789012:certificate/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
CDK_STORYBOOK_HOSTED_ZONE_ID=Z1234567890ABC
CDK_STORYBOOK_HOSTED_ZONE_NAME=dev.example.com###############################################
## QA Environment Configuration
###############################################
CDK_APP_NAME=react-starter
CDK_ENV=qa
CDK_ACCOUNT=123456789012
CDK_REGION=us-east-1
CDK_OU=software-engineering
CDK_OWNER=frontend-team
CDK_ASSET_PATH=../dist
CDK_DOMAIN_NAME=react-starter.qa.example.com
CDK_CERTIFICATE_ARN=arn:aws:acm:us-east-1:123456789012:certificate/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
CDK_HOSTED_ZONE_ID=Z1234567890ABC
CDK_HOSTED_ZONE_NAME=qa.example.com
CDK_STORYBOOK_ASSET_PATH=../storybook-static
CDK_STORYBOOK_DOMAIN_NAME=react-starter-storybook.qa.example.com
CDK_STORYBOOK_CERTIFICATE_ARN=arn:aws:acm:us-east-1:123456789012:certificate/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
CDK_STORYBOOK_HOSTED_ZONE_ID=Z1234567890ABC
CDK_STORYBOOK_HOSTED_ZONE_NAME=qa.example.com###############################################
## Production Environment Configuration
###############################################
CDK_APP_NAME=react-starter
CDK_ENV=prd
CDK_ACCOUNT=123456789012
CDK_REGION=us-east-1
CDK_OU=software-engineering
CDK_OWNER=frontend-team
CDK_ASSET_PATH=../dist
CDK_DOMAIN_NAME=react-starter.example.com
CDK_CERTIFICATE_ARN=arn:aws:acm:us-east-1:123456789012:certificate/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
CDK_HOSTED_ZONE_ID=Z1234567890ABC
CDK_HOSTED_ZONE_NAME=example.com
CDK_STORYBOOK_ASSET_PATH=../storybook-static
CDK_STORYBOOK_DOMAIN_NAME=react-starter-storybook.example.com
CDK_STORYBOOK_CERTIFICATE_ARN=arn:aws:acm:us-east-1:123456789012:certificate/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
CDK_STORYBOOK_HOSTED_ZONE_ID=Z1234567890ABC
CDK_STORYBOOK_HOSTED_ZONE_NAME=example.comThe CDK uses the following precedence for account and region:
-
Explicit CDK variables (highest priority)
CDK_ACCOUNT- AWS account IDCDK_REGION- AWS region
-
AWS CDK environment defaults
CDK_DEFAULT_ACCOUNT- Auto-detected from AWS credentialsCDK_DEFAULT_REGION- Auto-detected from AWS profile
-
AWS CLI configuration
- Uses the configured AWS profile and region
If neither explicit CDK variables nor CDK_DEFAULT environment variables are set, the CDK will fail with an error requesting explicit account and region configuration.
To use a custom domain for your application:
-
Create an ACM certificate in
us-east-1(required for CloudFront):aws acm request-certificate \ --domain-name react-starter.example.com \ --region us-east-1
-
Verify the certificate in the ACM console
-
Configure in
.env:CDK_DOMAIN_NAME=react-starter.example.com CDK_CERTIFICATE_ARN=arn:aws:acm:us-east-1:123456789012:certificate/xxxxx CDK_HOSTED_ZONE_ID=Z1234567890ABC CDK_HOSTED_ZONE_NAME=example.com
-
Deploy the CDK stack
Both application and infrastructure configurations are validated at runtime:
- Application: Environment variables are available in Vite at build time
- Infrastructure: Configuration is validated using Zod schema when
getConfig()is called
If validation fails, you'll see detailed error messages indicating which variables are missing or invalid.
CDK_ENV must be one of: dev, qa, prd
Ensure CDK_ENV is set to one of the allowed values.
CDK configuration validation failed: CDK_ASSET_PATH: Invalid type. Expected string
Ensure all required configuration variables are set as the correct type.
- Use auto-detected AWS account and region from AWS CLI
- Deploy to a development AWS account
- Enable detailed logging in your application
- Use minimal resource sizes
- Consider using S3 + CloudFront for cost-effective hosting
- Use dedicated QA AWS account
- Configure proper domain names for testing
- Use production-like configurations
- Enable monitoring and alerting
- Use dedicated production AWS account
- Configure custom domains with SSL/TLS certificates
- Use appropriate resource sizes
- Enable comprehensive monitoring, logging, and alerting
- Implement backup and disaster recovery plans
- Consider using CloudFront with WAF for protection
Problem: CDK deployment fails with an error about missing account or region.
Solution:
- Set
CDK_ACCOUNTandCDK_REGIONexplicitly in.env - Ensure AWS CLI is configured with
aws configure - Verify with:
aws sts get-caller-identity
Problem: Application fails to start with a configuration validation error message.
Solution:
- Ensure all required variables in
.envare present (see Environment Variables table above) - Ensure variables are prefixed with
VITE_for Vite compatibility - Restart the development server after changing
.env - Verify variables are set:
echo $VITE_BASE_URL_API - The config utility will provide detailed error messages indicating which variables are missing or invalid
Note: The application uses the config utility from common/utils/config which validates all environment variables at startup using Zod schema validation. If any required variables are missing or have invalid values, the application will fail with a clear error message indicating the issue.
Problem: CDK fails to load configuration with validation errors.
Solution:
- Check
.envfile syntax (no quotes around values unless necessary) - Verify required variables are set
- Check for typos in variable names
- Ensure enum values match allowed options
Problem: CDK cannot access AWS account.
Solution:
- Configure AWS credentials:
aws configure - Verify access:
aws sts get-caller-identity - Use AWS SSO if available:
aws sso login --profile YOUR_PROFILE - Ensure IAM user/role has CDK permissions
- Never commit
.envfiles - Use.gitignoreto exclude them - Use environment-specific
.envfiles - Keep configurations separate per environment - Document configuration changes - Update this guide when adding new variables
- Validate early - Run CDK synthesis before deployment:
cdk synth - Use AWS CDK best practices:
- Always tag resources for cost allocation
- Use stack outputs for important values
- Implement proper error handling
- Test in lower environments first
- Secure sensitive data:
- Use AWS Secrets Manager for secrets
- Never commit credentials to version control
- Use IAM roles instead of access keys when possible
- Monitor and log:
- Enable CloudWatch logging for CDN and application
- Set up CloudWatch alarms for critical metrics
- Review logs regularly for issues
For more information about this project, see the main README or visit the documentation.