CDK Stack for provisioning infrastructure for a PDS
README.md

pds-cdk #

AWS CDK app that provisions the infrastructure to host a self-managed Bluesky PDS on a single EC2 instance.

The stack only provisions infrastructure — installing the PDS itself is left to the official install script, which is run on the box after the stack is up.

What it builds #

  • VPC — single public subnet, no NAT gateway.
  • EC2 instance — Ubuntu 24.04 LTS (ARM64), t4g.small by default, with a 100 GB encrypted gp3 root volume. The Canonical AMI ships with the SSM agent preinstalled, so the box is reachable via Session Manager without an SSH key. IMDSv2 is enforced and termination protection is on.
  • Elastic IP — associated with the instance so the PDS hostname has a stable address.
  • Security group — 80/443 open to the world (HTTPS plus the Let's Encrypt ACME challenge), 22 optionally open to a CIDR you specify.
  • S3 blob bucket — backs PDS_BLOBSTORE_S3_BUCKET. Versioned, encrypted, no public access; CORS scoped to the PDS domain.
  • S3 Litestream bucket — destination for SQLite backups, per the Going to Production guide.
  • KMS key — ECC_SECG_P256K1 SIGN_VERIFY key for PDS_PLC_ROTATION_KEY_KMS_KEY_ID.
  • Route53 public hosted zone — apex A record + wildcard A record (so account handles like alice.pds.example.com resolve) pointing at the EIP.
  • IAM instance profile — read/write on both buckets, kms:Sign / GetPublicKey / DescribeKey on the rotation key, and the AmazonSSMManagedInstanceCore managed policy.

Layout #

pds-cdk/
├── bin/cdk.ts        # app entrypoint, stack instantiation
├── lib/pds-stack.ts  # the stack
├── cdk.json
├── package.json
└── tsconfig.json

Prerequisites #

  • Node 20+ and pnpm
  • AWS credentials in your shell (AWS_PROFILE or env vars)
  • CDK_DEFAULT_ACCOUNT and CDK_DEFAULT_REGION set
  • The target account must be cdk bootstrap'd in the chosen region.

Configuration #

pdsDomainName is required and is passed via CDK context — must be a domain you control. Route53 will become the authoritative hosted zone for it, and you'll need to delegate the NS records at your registrar after the first deploy.

pnpm cdk deploy -c pdsDomainName=pds.example.com --profile pds

Optional PdsStack props (set in bin/cdk.ts if you want to change defaults):

  • sshAllowedCidr — if set, opens port 22 to that CIDR. Omit to keep SSH closed and use SSM Session Manager.
  • instanceType — defaults to t4g.small.
  • rootVolumeSizeGb — defaults to 100.

The Ubuntu AMI is pinned to a specific dated SSM parameter (UBUNTU_AMI_SSM_PARAM in lib/pds-stack.ts). To roll the OS image, find a newer dated entry and edit the constant:

aws ssm get-parameters-by-path \
  --path "/aws/service/canonical/ubuntu/server/24.04/stable" \
  --recursive \
  --query "Parameters[?ends_with(Name, '/arm64/hvm/ebs-gp3/ami-id')].Name" \
  --output text | tr '\t' '\n' | sort -u | tail

Bumping the date is a deliberate code change — it will cause CloudFormation to replace the EC2 instance on the next deploy. (Termination protection is on, so you'll need to disable it first or take it down via the console.) Canonical prunes very old dated entries; if the pinned date eventually disappears, look up a current one with the command above.

Deploying #

Replace pds with whatever AWS profile name you use locally and pds.example.com with your domain.

pnpm install

# One-time per account/region: provision the CDK bootstrap stack.
pnpm cdk bootstrap --profile pds

# Sanity check — synthesizes to cdk.out without deploying.
pnpm build -c pdsDomainName=pds.example.com

# Deploy. CDK will print an IAM/security-group diff and prompt for approval.
pnpm cdk deploy -c pdsDomainName=pds.example.com --profile pds

Stack outputs include the EIP, blob bucket name, Litestream bucket name, KMS key ID, and the hosted zone nameservers. After the first deploy:

  1. Delegate pdsDomainName to the four nameservers from PdsHostedZoneNameservers at your registrar.
  2. Connect to the box (aws ssm start-session --target <PdsInstanceId>).
  3. Run the official PDS install script from the bluesky-social/pds repo.
  4. In pds.env, wire the stack outputs in:
    • PDS_BLOBSTORE_S3_BUCKET=<PdsBlobstoreBucket>
    • PDS_BLOBSTORE_S3_REGION=<your region>
    • PDS_PLC_ROTATION_KEY_KMS_KEY_ID=<PdsPlcRotationKeyId>
    • Set PDS_SQLITE_DISABLE_WAL_AUTO_CHECKPOINT=true and configure Litestream to ship /pds/actors/*.sqlite to the Litestream bucket.
    • Generate and safely store PDS_RECOVERY_DID_KEY separately — the corresponding private key is your last-resort recovery if the KMS key is ever lost.

TODO #

  • Replace Let's Encrypt with AWS-native TLS. The default PDS install relies on Caddy on the box for ACME. That works, but it puts cert renewal on a single instance and requires keeping port 80 open. Worth investigating whether to terminate TLS at an ALB/NLB with an ACM certificate instead, or front the PDS with CloudFront. Both shift renewal to AWS but add cost and complexity (and CloudFront in particular needs careful handling for the WebSocket firehose endpoint at /xrpc/com.atproto.sync.subscribeRepos).