Skip to content

Latest commit

 

History

History
378 lines (271 loc) · 11.2 KB

GETTING_STARTED.md

File metadata and controls

378 lines (271 loc) · 11.2 KB

Getting Started - KernelWorx

Prerequisites

  • Python 3.14 with uv package manager — pinned in .python-version, so uv provisions and uses the same interpreter CI and AWS Lambda run (3.14); requires-python = ">=3.14" is a floor, not a license to develop on a newer minor.
  • Node.js 24+ with npm
  • AWS CLI v2 configured with appropriate credentials
  • AWS Account with permissions for IAM, DynamoDB, S3, Cognito, AppSync, ACM, Route 53, CloudFront, Lambda, SNS, CloudFormation, and CloudWatch/Billing
  • Git for version control
  • OpenTofu >= 1.8.0

Initial Setup

1. Clone and Install Dependencies

# Clone the repository
git clone https://github.com/dmeiser/kernelworx.git
cd kernelworx

# Install Python dependencies (Lambda functions)
uv sync

# Install root Node.js dependencies (AppSync resolver bundling tooling)
npm install

# Install OpenTofu (macOS with Homebrew)
brew install opentofu

# Or on Linux
curl -fsSL https://get.opentofu.org | bash

2. Configure AWS Credentials

# Configure AWS CLI with your dev account
aws configure --profile dev

# Set as default profile (optional)
export AWS_PROFILE=dev

3. Bootstrap the OpenTofu State Bucket

OpenTofu stores state in S3. The backend bucket must exist before the first tofu init:

# Deploy the bootstrap CloudFormation stack (adjust Environment if needed)
aws cloudformation deploy \
  --template-file tofu/bootstrap/state-bucket.yaml \
  --stack-name kernelworx-tofu-state \
  --parameter-overrides Environment=dev \
  --region us-east-1

Verify the bucket name matches the backend configured in tofu/application/environments/dev/main.tf (default: kernelworx-tofu-state-us-east-1-dev). If you changed regions or naming conventions, update the backend config or the template parameters accordingly.

4. Configure Deployment Settings

Important: Create a .env file in the repository root with your configuration:

cp .env.example .env

Edit .env with your settings:

# Encryption passphrase for state files (generate a secure random string)
TF_VAR_encryption_passphrase=your-secure-passphrase-here

The deployment helper scripts source this root .env file. If you run tofu directly, export the variable instead:

export TF_VAR_encryption_passphrase='your-passphrase'

5. Verify Environment

# Check Python version
python --version  # Should be 3.14 (the .python-version pin)

# Check uv
uv --version

# Check Node.js
node --version  # Should be 24+

# Check AWS credentials
aws sts get-caller-identity

Deployment

Quick Start Deployment (Recommended)

Use the deployment helper, which builds the Lambda layer, bundles the AppSync resolver JavaScript, and runs OpenTofu:

# Initialize OpenTofu (first time only)
./tofu/application/scripts/deploy.sh dev init

# Preview changes
./tofu/application/scripts/deploy.sh dev plan

# Apply changes
./tofu/application/scripts/deploy.sh dev apply

The helper will:

  1. Source the root .env for required TF_VAR_* values
  2. Build .build/lambda-layer from production Python dependencies
  3. Bundle the AppSync resolver JavaScript (npm run build:resolvers)
  4. Run tofu in tofu/application/environments/dev

If you run tofu directly, export TF_VAR_encryption_passphrase, build the layer, and bundle the resolver JavaScript first (npm install at the repo root, then npm run build:resolvers); a plain tofu apply fails because the layer archive and the resolver bundle (tofu/application/appsync/dist/) are missing.

Manual Deployment

If you prefer more control:

# Set encryption passphrase
export TF_VAR_encryption_passphrase='your-passphrase'

# Build the Lambda layer first (required by the lambda module)
rm -rf .build/lambda-layer
mkdir -p .build/lambda-layer/python
uv export --no-dev --format requirements.txt --no-hashes > .build/lambda-layer/requirements.txt
uv pip install --requirement .build/lambda-layer/requirements.txt --target .build/lambda-layer/python

# Bundle the AppSync resolver JavaScript (required by the appsync module)
npm install
npm run build:resolvers

# Run OpenTofu from the environment directory
cd tofu/application/environments/dev
tofu init
tofu plan -out=tfplan
tofu apply tfplan

Deployment with Existing Resources (Import)

If you have existing resources from a previous deployment, you can import them into OpenTofu state:

cd tofu/application/environments/dev

# Import existing DynamoDB table
tofu import 'module.dynamodb.aws_dynamodb_table.accounts' table-name

# Import existing S3 bucket
tofu import 'module.s3.aws_s3_bucket.static' bucket-name

Finding existing resources:

# Find DynamoDB tables
aws dynamodb list-tables --query 'TableNames[?contains(@, `kernelworx`)]'

# Find S3 buckets
aws s3 ls | grep kernelworx

# Find Cognito User Pools
aws cognito-idp list-user-pools --max-results 60 | grep kernelworx

# Find AppSync APIs
aws appsync list-graphql-apis --query 'graphqlApis[?contains(name, `kernelworx`)].id'

Environment-Specific Deployments

Each environment has its own directory:

# For dev environment
cd tofu/application/environments/dev
tofu apply

# For prod environment (when ready)
cd tofu/application/environments/prod
tofu apply

Each environment gets its own isolated resources with environment-specific naming.

Social Authentication (Optional)

To enable social login providers, set environment variables before deployment:

# Set OAuth credentials as OpenTofu variables
export TF_VAR_google_client_id="your-client-id"
export TF_VAR_google_client_secret="your-client-secret"

# Deploy
cd tofu/application/environments/dev
tofu apply

Important: the dev environment currently enables Google IdP and WebAuthn by default. If you do not have Google OAuth credentials, set enable_google_idp = false and enable_webauthn = false in tofu/application/environments/dev/main.tf before running tofu apply, or the deployment may create a broken Cognito configuration. If these variables are not set and Google IdP remains enabled, only email/password authentication will work reliably.

Development Workflow

Running Tests

# Run all Lambda function tests with coverage
uv run pytest tests/unit --cov=src --cov-fail-under=100

# Run specific test file
uv run pytest tests/unit/test_transfer_profile_ownership.py -v

# Run with coverage report
uv run pytest tests/unit --cov=src --cov-report=html

E2E Smoke Tests

A Playwright-based e2e smoke suite exercises the deployed dev environment end-to-end (auth, profiles, campaigns, orders, sharing, signup). It requires the dev environment to be live and test users pre-created.

# Install Playwright browsers (first time only)
uv run playwright install chromium

# Run the full e2e suite
uv run pytest tests/e2e/ --ignore=tests/unit -v

See tests/e2e/README.md for full prerequisites, .env requirements, and troubleshooting.

Code Quality Checks

# Format code
uv run ruff check --select I --fix src/ tests/
uv run ruff format src/ tests/

# Type checking
uv run mypy src/

# Run all checks
uv run ruff check --select I --fix src/ tests/ && \
uv run ruff format src/ tests/ && \
uv run mypy src/ && \
uv run pytest tests/unit --cov=src --cov-fail-under=100

OpenTofu Commands

cd tofu/application/environments/dev

# Bundle the resolver JavaScript first (see Manual Deployment above);
# a plain tofu init/plan fails while tofu/application/appsync/dist is missing
# Initialize working directory
tofu init

# Show differences between deployed and local
tofu plan

# Deploy changes
tofu apply

# Destroy resources (WARNING: use with caution)
tofu destroy

Accessing Deployed Resources

After deployment, OpenTofu outputs key resource identifiers:

# Get outputs
cd tofu/application/environments/dev
tofu output

Key outputs include:

  • cognito_user_pool_id: Cognito User Pool ID
  • cognito_client_id: App client ID for authentication
  • appsync_api_url: AppSync GraphQL API endpoint
  • cloudfront_distribution_id: CloudFront distribution ID for invalidations
  • static_assets_bucket: S3 bucket for SPA hosting
  • exports_bucket: S3 bucket for generated reports
  • site_url: Public URL of the deployed application

Frontend Build and Deploy

After the backend is deployed, build and deploy the React SPA:

cd frontend

# Build the production bundle. For dev/prod the app is same-origin — it calls
# /graphql and the auth paths through the site's CloudFront distribution — so
# VITE_APPSYNC_ENDPOINT and VITE_COGNITO_DOMAIN must stay unset for the build.
# They are only for local `vite dev` and ephemeral environments (see
# frontend/.env.example); make sure no local .env / .env.local defining them
# is present, or Vite will bake the absolute endpoints into the bundle.
#
# The public order pages (API-key auth) instead need the AppSync API key baked
# into the same build — a replaced key with a stale bundle fails every public
# call with Unauthorized until the next deploy:
export VITE_APPSYNC_API_KEY=$(tofu -chdir=../tofu/application/environments/dev output -raw appsync_api_key)
npm run build

# Upload to S3
aws s3 sync dist s3://$(tofu -chdir=../tofu/application/environments/dev output -raw static_assets_bucket) --delete

# Invalidate the CloudFront distribution
aws cloudfront create-invalidation \
  --distribution-id $(tofu -chdir=../tofu/application/environments/dev output -raw cloudfront_distribution_id) \
  --paths "/*"

Troubleshooting

State lock issues

If a previous apply was interrupted:

tofu force-unlock <lock-id>

Cognito domain already exists

Delete the existing domain or use a different environment name:

# Delete domain
aws cognito-idp delete-user-pool-domain \
  --domain login.dev.kernelworx.app \
  --user-pool-id us-east-1_XXXXXXXXX

DynamoDB table already exists

Either:

  1. Import it using context: -c table_name=kernelworx-accounts-ue1-dev
  2. Delete it: aws dynamodb delete-table --table-name kernelworx-accounts-ue1-dev
  3. Rename in different environment: -c environment=prod

Cost Management

The deployed resources use serverless/on-demand pricing:

  • DynamoDB: Pay-per-request (PAY_PER_REQUEST)
  • S3: Pay for storage and requests
  • Cognito: Essentials tier (~$0.015 per MAU after 50 free MAUs)
  • AppSync: Pay per request and data transfer
  • Lambda: Pay per invocation

Monthly Budget: $10/month with alerts at 80% and 100% configured in CloudWatch Billing Alerts.

Next Steps

  1. Run Backend Tests - uv run pytest tests/unit --cov=src --cov-fail-under=100
  2. Deploy Frontend - Build the React SPA and deploy to S3 + CloudFront
  3. Configure Social Providers - Set up an OAuth app with Google (optional)
  4. Create an Admin Catalog - Required before campaigns can be created
  5. Create Test Users - bash scripts/create-test-users.sh
  6. Run E2E Smoke Tests - uv run pytest tests/e2e/ --ignore=tests/unit -v
  7. Set Up CI/CD - GitHub Actions for automated testing and deployment

Support