Skip to content

Use Stack Management

Learn how to manage your Pika infrastructure using AWS CDK, including deployment, updates, and best practices for maintaining your stacks.

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
  • A Pika project set up locally
  • AWS CLI configured with appropriate credentials
  • Node.js and pnpm installed
  • Basic understanding of AWS CDK

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

Pika projects typically have two main stacks:

  1. Pika Service Stack (services/pika/) - Backend infrastructure
  2. Pika Chat Stack (apps/pika-chat/infra/) - Frontend application

Before making changes, check the current state of your stacks.

Terminal window
cd services/pika
pnpm cdk:diff

This shows what would change if you deployed now.

Terminal window
# List all stacks
pnpm cdk list
# View stack details
aws cloudformation describe-stacks \
--stack-name pika-test \
--region us-east-1

Always preview changes before deploying.

Terminal window
cd services/pika
pnpm cdk:diff

Output shows:

  • Resources to be added (green +)
  • Resources to be removed (red -)
  • Resources to be modified (yellow ~)
  • No changes (no output)

Example output:

Stack pika-test
Resources
[~] AWS::Lambda::Function ConverseFunction
└─ [~] Environment
└─ [~] .Variables:
└─ [+] Added: NEW_FEATURE_FLAG = "true"
[+] AWS::DynamoDB::Table NewFeatureTable

Deploy your infrastructure changes.

Terminal window
cd services/pika
pnpm cdk:deploy
Terminal window
cd apps/pika-chat
pnpm cdk:deploy

For production, require manual approval:

Terminal window
pnpm cdk:deploy --require-approval any-change
Terminal window
# If you have multiple stacks
pnpm cdk:deploy pika-test
pnpm cdk:deploy pika-chat-test

Watch the deployment progress.

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 | NewFeatureTable
pika-test: 1/15 | 10:30:48 AM | CREATE_COMPLETE | AWS::DynamoDB::Table | NewFeatureTable
...
  1. Open AWS ConsoleCloudFormation
  2. Find your stack (e.g., pika-test)
  3. View Events tab for detailed progress
  4. Check Resources tab for created resources

If deployment fails, CDK automatically rolls back:

pika-test: deployment failed: Error: Resource creation failed
pika-test: rolling back...
pika-test: rollback complete

To rollback to previous version using git:

Terminal window
# Find previous working commit
git log --oneline
# Checkout previous version
git checkout <previous-hash>
# Redeploy old version
cd services/pika
pnpm cdk:deploy

For severe issues:

Terminal window
# Delete stack (WARNING: destroys data!)
pnpm cdk:destroy
# Redeploy from scratch
pnpm cdk:deploy
Terminal window
# From project root
pika sync --dry-run # Preview updates
pika sync # Apply updates

Edit stack definition files:

Locations:

  • services/pika/bin/pika.ts - Service stack
  • apps/pika-chat/infra/bin/pika-chat.ts - Chat app stack

Example change:

services/pika/bin/pika.ts
const stack = new PikaStack(app, `pika-${stage}`, {
env: {
account: '123456789012',
region: 'us-east-1' // Change region
},
stage: stage,
// ... other props
});
services/pika/infra/lib/stacks/custom-stack-defs.ts
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);
}
}

When breaking changes affect infrastructure:

Check Migration Guides for version-specific instructions.

Terminal window
# Backup DynamoDB tables
aws dynamodb create-backup \
--table-name chat-messages-pika-test \
--backup-name messages-backup-$(date +%Y%m%d)
# Export to S3
aws 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/

Always test breaking changes in dev/staging first:

Terminal window
# Deploy to test environment
export STAGE=test
cd services/pika
pnpm cdk:deploy

Execute migration steps carefully, in order.

Terminal window
# Check stack status
pnpm cdk:diff
# Verify resources
aws cloudformation describe-stacks \
--stack-name pika-prod \
--query "Stacks[0].StackStatus"
Terminal window
# Always commit infrastructure changes
git add services/pika/
git commit -m "feat: add custom table for feature X"

Document custom changes:

services/pika/infra/lib/stacks/custom-stack-defs.ts
/**
* 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(/* ... */);
Terminal window
# Always run diff first
pnpm cdk:diff
# Review changes carefully
# Deploy only if changes look correct
pnpm cdk:deploy
  • Watch CloudFormation events in AWS Console
  • Check for errors or warnings
  • Verify resources created correctly
  • Test deployed changes
Terminal window
# Regular framework updates
pika sync
# Check for new releases
# Review release notes
# Test in non-production first

Use different stacks for different environments:

Terminal window
# Development
export STAGE=dev
pnpm cdk:deploy
# Staging
export STAGE=staging
pnpm cdk:deploy
# Production
export STAGE=prod
pnpm cdk:deploy --require-approval any-change

Verify stack management working correctly:

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

Symptoms: "Resource already exists" errors

Solutions:

  • Check for naming conflicts
  • Verify resource identifiers are unique
  • Review manual AWS console changes
  • Consider using cdk:destroy and redeploying (with caution)

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

Symptoms: Stack state doesn't match deployed resources

Solutions:

Terminal window
# Detect drift
aws cloudformation detect-stack-drift \
--stack-name pika-test
# View drift results
aws cloudformation describe-stack-resource-drifts \
--stack-name pika-test