Learn how to manage your Pika infrastructure using AWS CDK, including deployment, updates, and best practices for maintaining your stacks.
What You'll Accomplish
Section titled “What You'll Accomplish”By the end of this guide, you will:
- Understand Pika's infrastructure organization
- Deploy and update stacks with AWS CDK
- Check stack status and preview changes
- Handle rollbacks and breaking changes
- Follow best practices for stack management
Prerequisites
Section titled “Prerequisites”- A Pika project set up locally
- AWS CLI configured with appropriate credentials
- Node.js and pnpm installed
- Basic understanding of AWS CDK
Understanding Pika Infrastructure
Section titled “Understanding Pika Infrastructure”Pika uses AWS CDK for infrastructure as code, providing:
- Automated Deployment: CDK handles resource provisioning
- Type Safety: TypeScript-based infrastructure definitions
- Version Control: Infrastructure code lives in your repository
- Rollback Support: CDK tracks stack state for rollbacks
Stack Organization
Section titled “Stack Organization”Pika projects typically have two main stacks:
- Pika Service Stack (
services/pika/) - Backend infrastructure - Pika Chat Stack (
apps/pika-chat/infra/) - Frontend application
Step 1: Check Stack Status
Section titled “Step 1: Check Stack Status”Before making changes, check the current state of your stacks.
View Current Stack
Section titled “View Current Stack”cd services/pikapnpm cdk:diffThis shows what would change if you deployed now.
Check Deployed Resources
Section titled “Check Deployed Resources”# List all stackspnpm cdk list
# View stack detailsaws cloudformation describe-stacks \ --stack-name pika-test \ --region us-east-1Step 2: Preview Changes
Section titled “Step 2: Preview Changes”Always preview changes before deploying.
Using CDK Diff
Section titled “Using CDK Diff”cd services/pikapnpm cdk:diffOutput shows:
- Resources to be added (green +)
- Resources to be removed (red -)
- Resources to be modified (yellow ~)
- No changes (no output)
Example output:
Stack pika-testResources[~] AWS::Lambda::Function ConverseFunction └─ [~] Environment └─ [~] .Variables: └─ [+] Added: NEW_FEATURE_FLAG = "true"[+] AWS::DynamoDB::Table NewFeatureTableStep 3: Deploy Changes
Section titled “Step 3: Deploy Changes”Deploy your infrastructure changes.
Deploy Service Stack
Section titled “Deploy Service Stack”cd services/pikapnpm cdk:deployDeploy Chat App Stack
Section titled “Deploy Chat App Stack”cd apps/pika-chatpnpm cdk:deployDeploy with Approval
Section titled “Deploy with Approval”For production, require manual approval:
pnpm cdk:deploy --require-approval any-changeDeploy Specific Stack
Section titled “Deploy Specific Stack”# If you have multiple stackspnpm cdk:deploy pika-testpnpm cdk:deploy pika-chat-testStep 4: Monitor Deployment
Section titled “Step 4: Monitor Deployment”Watch the deployment progress.
In Terminal
Section titled “In Terminal”CDK shows real-time progress:
pika-test: deploying...pika-test: creating CloudFormation changeset...pika-test: 0/15 | 10:30:45 AM | CREATE_IN_PROGRESS | AWS::DynamoDB::Table | NewFeatureTablepika-test: 1/15 | 10:30:48 AM | CREATE_COMPLETE | AWS::DynamoDB::Table | NewFeatureTable...In AWS Console
Section titled “In AWS Console”- Open AWS Console → CloudFormation
- Find your stack (e.g.,
pika-test) - View Events tab for detailed progress
- Check Resources tab for created resources
Step 5: Handle Deployment Issues
Section titled “Step 5: Handle Deployment Issues”Rollback Failed Deployment
Section titled “Rollback Failed Deployment”If deployment fails, CDK automatically rolls back:
pika-test: deployment failed: Error: Resource creation failedpika-test: rolling back...pika-test: rollback completeManual Rollback
Section titled “Manual Rollback”To rollback to previous version using git:
# Find previous working commitgit log --oneline
# Checkout previous versiongit checkout <previous-hash>
# Redeploy old versioncd services/pikapnpm cdk:deployDelete and Recreate Stack
Section titled “Delete and Recreate Stack”For severe issues:
# Delete stack (WARNING: destroys data!)pnpm cdk:destroy
# Redeploy from scratchpnpm cdk:deployCommon Stack Management Tasks
Section titled “Common Stack Management Tasks”Update Framework
Section titled “Update Framework”# From project rootpika sync --dry-run # Preview updatespika sync # Apply updatesModify Stack Configuration
Section titled “Modify Stack Configuration”Edit stack definition files:
Locations:
services/pika/bin/pika.ts- Service stackapps/pika-chat/infra/bin/pika-chat.ts- Chat app stack
Example change:
const stack = new PikaStack(app, `pika-${stage}`, { env: { account: '123456789012', region: 'us-east-1' // Change region }, stage: stage, // ... other props});Add Custom Resources
Section titled “Add Custom Resources”export class PikaCustomStackDefs { public static addStackResourcesAfterPikaConstructIsCreated( scope: PikaStack ): void { // Add custom DynamoDB table const customTable = new dynamodb.Table(scope, 'CustomTable', { partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING }, billingMode: dynamodb.BillingMode.PAY_PER_REQUEST });
// Grant Lambda access customTable.grantReadWriteData(scope.converseFunction); }}Handling Breaking Changes
Section titled “Handling Breaking Changes”When breaking changes affect infrastructure:
1. Read Migration Guide
Section titled “1. Read Migration Guide”Check Migration Guides for version-specific instructions.
2. Backup Critical Data
Section titled “2. Backup Critical Data”# Backup DynamoDB tablesaws dynamodb create-backup \ --table-name chat-messages-pika-test \ --backup-name messages-backup-$(date +%Y%m%d)
# Export to S3aws dynamodb export-table-to-point-in-time \ --table-arn arn:aws:dynamodb:us-east-1:123456789012:table/chat-messages-pika-test \ --s3-bucket my-backups \ --s3-prefix dynamodb-exports/3. Test in Non-Production
Section titled “3. Test in Non-Production”Always test breaking changes in dev/staging first:
# Deploy to test environmentexport STAGE=testcd services/pikapnpm cdk:deploy4. Follow Migration Steps
Section titled “4. Follow Migration Steps”Execute migration steps carefully, in order.
5. Verify After Migration
Section titled “5. Verify After Migration”# Check stack statuspnpm cdk:diff
# Verify resourcesaws cloudformation describe-stacks \ --stack-name pika-prod \ --query "Stacks[0].StackStatus"Best Practices
Section titled “Best Practices”Version Control
Section titled “Version Control”# Always commit infrastructure changesgit add services/pika/git commit -m "feat: add custom table for feature X"Documentation
Section titled “Documentation”Document custom changes:
/** * Custom DynamoDB table for storing feature X data * * Added: 2025-01-15 * Purpose: Store user preferences for feature X * Dependencies: Used by Lambda function processFeatureX */const featureXTable = new dynamodb.Table(/* ... */);Preview Before Deploying
Section titled “Preview Before Deploying”# Always run diff firstpnpm cdk:diff
# Review changes carefully# Deploy only if changes look correctpnpm cdk:deployMonitor Deployments
Section titled “Monitor Deployments”- Watch CloudFormation events in AWS Console
- Check for errors or warnings
- Verify resources created correctly
- Test deployed changes
Keep Framework Updated
Section titled “Keep Framework Updated”# Regular framework updatespika sync
# Check for new releases# Review release notes# Test in non-production firstEnvironment Separation
Section titled “Environment Separation”Use different stacks for different environments:
# Developmentexport STAGE=devpnpm cdk:deploy
# Stagingexport STAGE=stagingpnpm cdk:deploy
# Productionexport STAGE=prodpnpm cdk:deploy --require-approval any-changeTesting Checklist
Section titled “Testing Checklist”Verify stack management working correctly:
Troubleshooting
Section titled “Troubleshooting”Deployment Stuck
Section titled “Deployment Stuck”Symptoms: Deployment hangs for extended period
Solutions:
- Check CloudFormation events in AWS Console
- Look for resource creation failures
- Check AWS service limits
- Verify IAM permissions
- Review CloudWatch logs
Resource Conflicts
Section titled “Resource Conflicts”Symptoms: "Resource already exists" errors
Solutions:
- Check for naming conflicts
- Verify resource identifiers are unique
- Review manual AWS console changes
- Consider using
cdk:destroyand redeploying (with caution)
Permission Errors
Section titled “Permission Errors”Symptoms: "Access Denied" or "Insufficient permissions"
Solutions:
- Verify AWS credentials configured
- Check IAM user/role has necessary permissions
- Review CloudFormation service role permissions
- Ensure cross-stack references work
Stack Drift
Section titled “Stack Drift”Symptoms: Stack state doesn't match deployed resources
Solutions:
# Detect driftaws cloudformation detect-stack-drift \ --stack-name pika-test
# View drift resultsaws cloudformation describe-stack-resource-drifts \ --stack-name pika-testNext Steps
Section titled “Next Steps”- Configure Sync System - Keep framework updated
- Deploy to AWS with CDK - Complete deployment guide
- Troubleshooting - Debug deployment issues
Related Documentation
Section titled “Related Documentation”- AWS CDK Documentation - Official CDK docs
- Pika Releases - Version history
- Migration Guides - Upgrade guides