Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

12.6 KB
Raw Blame
/*
 * ClickHouse on AWS: one private instance this stack owns, or an endpoint it
 * only records.
 *
 * The self-hosted path is a single EC2 instance with a separate, retained EBS
 * volume for its data. Separate on purpose — the instance is replaced whenever
 * its user data changes, and telemetry that lived on the root disk would go
 * with it. `retainOnDelete` on the volume is the other half: a stage removed by
 * mistake keeps the history it collected.
 *
 * Three accounts rather than two. The admin account is the one the host
 * configures itself with and no workload ever holds; the writer and the reader
 * are what the collector and the API are given. Three because the reconcile
 * step below has to be able to create and re-grant the other two, which a
 * writer cannot do.
 *
 * The schema and the retention are reconciled by a local command rather than by
 * a resource, because neither is something the AWS API can express: they are
 * SQL, executed on the instance through SSM. It is triggered on everything that
 * would change the answer — the instance, the volume, the user data, and each
 * secret's version — so a rotated password re-grants rather than locking the
 * collector out until someone notices.
 */

import {
  CLICKHOUSE_IMAGE,
  OPENAPI_READER_USERNAME,
  encodeClickHouseUserData,
  renderClickHouseSchema,
  renderClickHouseLogReaderSql,
} from '../../clickhouse-host.ts'
import type { ClickHouse, ClickHouseProvider, ClickHouseRequest } from '../../clickhouse.ts'
import type { NetworkBinding } from '../../network.ts'
import { instanceFor } from 'naming'

/** What each requested size answers to. */
const INSTANCE = { small: 'm6a.large', medium: 'm6a.xlarge' } as const

/** The HTTP interface. The only port anything speaks to it on. */
const HTTP_PORT = 8123

/** Ubuntu's own publisher, and the image line the runner uses too. */
const UBUNTU_OWNER = '099720109477'
const UBUNTU_NAME = 'ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04-amd64-server-*'

type ManagedSecret = { resource: CloudResource; version: CloudResource }

const clickHouseSecret = (resourceName: string, name: string): ManagedSecret => {
  const password = new random.RandomPassword(`${resourceName}Password`, { length: 32, special: false })
  const resource = new aws.secretsmanager.Secret(resourceName, {
    namePrefix: `${instanceFor({ app: $app.name, stage: $app.stage, artifact: name })}-`,
    recoveryWindowInDays: 7,
  })
  const version = new aws.secretsmanager.SecretVersion(`${resourceName}Value`, {
    secretId: resource.id,
    secretString: $util.secret(password.result),
  })
  return { resource, version }
}

/**
 * The version currently marked AWSCURRENT for a secret this stack did not
 * create.
 *
 * A managed stage names two secrets by ARN and nothing else; the version is
 * what makes a rotation visible to a running container, so it is read back
 * rather than assumed. Exactly one has to be current — more than one means a
 * rotation is mid-flight, and picking either would deploy a credential that is
 * about to stop working.
 */
const currentVersion = (name: string, secretId: string, region: string): $util.Output<string> =>
  aws.secretsmanager.getSecretVersionsOutput({ secretId, region }).versions.apply((versions: any[]) => {
    const current = versions.filter((version) => version.versionStages.includes('AWSCURRENT'))
    if (current.length !== 1) throw new Error(`${name} must have exactly one AWSCURRENT version`)
    return current[0].versionId as string
  })

export const awsClickHouseProvider =
  ({
    network,
    region,
    managed,
    otelOpenapiEnabled = false,
  }: {
    network: Extract<NetworkBinding, { cloud: 'aws' }>
    region: string
    /** The endpoint and its two secrets, for a stage that runs none of its own. */
    managed: { url: string; writerSecretArn: string; readerSecretArn: string } | null
    otelOpenapiEnabled?: boolean
  }): ClickHouseProvider =>
  (request: ClickHouseRequest): ClickHouse => {
    if (otelOpenapiEnabled && (request.mode !== 'self-hosted' || request.database !== 'otel' ||
      [request.writerUsername, request.readerUsername].includes(OPENAPI_READER_USERNAME))) {
      throw new Error('OTEL_OPENAPI_ENABLED requires self-hosted otel and a separate reader account')
    }
    if (request.mode === 'disabled') return { active: false, mode: 'disabled' }

    if (request.mode === 'managed') {
      if (!managed) {
        throw new Error(
          'CLICKHOUSE_MODE=managed needs CLICKHOUSE_URL, CLICKHOUSE_WRITER_PASSWORD_SECRET_ARN and ' +
            'CLICKHOUSE_READER_PASSWORD_SECRET_ARN in this stage’s store',
        )
      }
      return {
        active: true,
        mode: 'managed',
        url: $util.output(managed.url),
        database: request.database,
        writer: {
          username: request.writerUsername,
          passwordRef: $util.output(managed.writerSecretArn),
          credentialVersion: currentVersion('CLICKHOUSE_WRITER_PASSWORD_SECRET_ARN', managed.writerSecretArn, region),
        },
        reader: {
          username: request.readerUsername,
          passwordRef: $util.output(managed.readerSecretArn),
          credentialVersion: currentVersion('CLICKHOUSE_READER_PASSWORD_SECRET_ARN', managed.readerSecretArn, region),
        },
        id: $util.output(managed.url),
        ready: [],
      }
    }

    // The schema, the readiness environment and its trigger all read the one
    // resolved value, so the readiness check never compares a schema rendered
    // at one number against a TTL asserted at another.
    const { retentionHours } = request
    const admin = clickHouseSecret('ClickHouseAdminSecret', 'clickhouse-admin')
    const writer = clickHouseSecret('ClickHouseWriterSecret', 'clickhouse-writer')
    const reader = clickHouseSecret('ClickHouseReaderSecret', 'clickhouse-reader')
    const openapiReader = otelOpenapiEnabled
      ? clickHouseSecret('ClickHouseOpenapiReaderSecret', 'clickhouse-openapi-reader') : null
    const logReaderSql = openapiReader ? renderClickHouseLogReaderSql() : null

    /*
     * An IAM role name, written out rather than taken from `naming`.
     *
     * `identityFor` would spell it from `appShort` and rename it, and AWS has
     * no rename: the role would be deleted and recreated, taking the instance
     * profile and the host attached to it. dev and prod run on this account
     * today, so the name is held — argued here rather than left as a literal
     * somebody tidies away. The same exception as `storage.ts`'s vending role
     * and as `bootstrap/environment.ts`'s deploy role.
     */
    const role = new aws.iam.Role('ClickHouseRole', {
      name: `${$app.name}-${$app.stage}-clickhouse-instance`,
      assumeRolePolicy: JSON.stringify({
        Version: '2012-10-17',
        Statement: [{ Effect: 'Allow', Principal: { Service: 'ec2.amazonaws.com' }, Action: 'sts:AssumeRole' }],
      }),
    })
    // The reconcile step below reaches the host through SSM rather than SSH, so
    // there is no key to distribute and no inbound port to open for it.
    new aws.iam.RolePolicyAttachment('ClickHouseSsmPolicy', {
      role: role.name,
      policyArn: 'arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore',
    })
    const secretPolicy = new aws.iam.RolePolicy('ClickHouseSecretPolicy', {
      role: role.name,
      policy: $resolve([admin.resource.arn, writer.resource.arn, reader.resource.arn, ...(openapiReader ? [openapiReader.resource.arn] : [])]).apply(([...arns]) =>
        JSON.stringify({
          Version: '2012-10-17',
          Statement: [{ Effect: 'Allow', Action: ['secretsmanager:GetSecretValue'], Resource: arns }],
        }),
      ),
    })
    // The role's own name, for the same reason it is written out above: the
    // profile is what the instance carries, so the two rename together or not
    // at all.
    const profile = new aws.iam.InstanceProfile('ClickHouseProfile', {
      name: `${$app.name}-${$app.stage}-clickhouse-instance`,
      role: role.name,
    })

    const securityGroup = new aws.ec2.SecurityGroup('ClickHouseSecurityGroup', {
      vpcId: network.vpcId,
      description: 'Private ClickHouse HTTP access from BoxLite services',
      ingress: [{ protocol: 'tcp', fromPort: HTTP_PORT, toPort: HTTP_PORT, securityGroups: network.vpc.securityGroups }],
      egress: [{ protocol: '-1', fromPort: 0, toPort: 0, cidrBlocks: ['0.0.0.0/0'] }],
    })

    const subnet = aws.ec2.getSubnetOutput({ id: network.privateSubnets[0] })
    const volume = new aws.ebs.Volume(
      'ClickHouseData',
      {
        availabilityZone: subnet.availabilityZone,
        size: request.dataGb,
        type: 'gp3',
        encrypted: true,
        tags: { Name: instanceFor({ app: $app.name, stage: $app.stage, artifact: 'clickhouse-data' }) },
      },
      // Retained on purpose: the instance is replaced whenever its user data
      // changes, and the history must not go with it.
      { retainOnDelete: true },
    )

    const ami = aws.ec2.getAmiOutput({
      mostRecent: true,
      owners: [UBUNTU_OWNER],
      filters: [
        { name: 'name', values: [UBUNTU_NAME] },
        { name: 'architecture', values: ['x86_64'] },
      ],
    })

    const userData = $resolve([volume.id, admin.resource.arn, writer.resource.arn, reader.resource.arn]).apply(
      ([volumeId, adminSecretArn, writerSecretArn, readerSecretArn]) =>
        encodeClickHouseUserData({ region, volumeId, adminSecretArn, writerSecretArn, readerSecretArn }),
    )
    const instance = new aws.ec2.Instance(
      'ClickHouse',
      {
        ami: ami.id,
        instanceType: INSTANCE[request.instanceSize],
        subnetId: network.privateSubnets[0],
        associatePublicIpAddress: false,
        vpcSecurityGroupIds: [securityGroup.id],
        iamInstanceProfile: profile.name,
        metadataOptions: { httpEndpoint: 'enabled', httpTokens: 'required', httpPutResponseHopLimit: 1 },
        userDataBase64: userData,
        userDataReplaceOnChange: true,
        rootBlockDevice: { encrypted: true, volumeType: 'gp3', volumeSize: 20 },
        tags: { Name: instanceFor({ app: $app.name, stage: $app.stage, artifact: 'clickhouse' }) },
      },
      {
        // A newer Ubuntu image must not replace a running telemetry host on
        // an unrelated deploy; upgrading is a deliberate act.
        ignoreChanges: ['ami'],
        deleteBeforeReplace: true,
        dependsOn: [secretPolicy, admin.version, writer.version, reader.version],
      },
    )
    const attachment = new aws.ec2.VolumeAttachment(
      'ClickHouseDataAttachment',
      { deviceName: '/dev/sdf', instanceId: instance.id, volumeId: volume.id, stopInstanceBeforeDetaching: true },
      { deleteBeforeReplace: true },
    )
    const ready = new command.local.Command(
      'ClickHouseDatabaseReady',
      {
        dir: $cli.paths.root,
        create: 'node apps/infra/scripts/clickhouse-ops.mjs reconcile',
        update: 'node apps/infra/scripts/clickhouse-ops.mjs reconcile',
        environment: {
          AWS_REGION: region,
          CLICKHOUSE_INSTANCE_ID: instance.id,
          CLICKHOUSE_ADMIN_SECRET_ARN: admin.resource.arn,
          CLICKHOUSE_WRITER_SECRET_ARN: writer.resource.arn,
          CLICKHOUSE_READER_SECRET_ARN: reader.resource.arn,
          CLICKHOUSE_EXPECTED_IMAGE: CLICKHOUSE_IMAGE,
          CLICKHOUSE_SCHEMA_BASE64: Buffer.from(renderClickHouseSchema(retentionHours)).toString('base64'),
          CLICKHOUSE_RETENTION_HOURS: String(retentionHours),
          ...(openapiReader ? {
            CLICKHOUSE_OPENAPI_READER_SECRET_ARN: openapiReader.resource.arn,
            CLICKHOUSE_OPENAPI_READER_SQL_BASE64: Buffer.from(logReaderSql!).toString('base64'),
          } : {}),
        },
        // Every input that changes the answer. A rotated password re-grants
        // rather than locking the collector out until someone notices.
        triggers: [
          retentionHours,
          instance.id,
          volume.id,
          userData,
          admin.version.id,
          writer.version.id,
          reader.version.id,
          ...(openapiReader ? [logReaderSql, openapiReader.version.id] : []),
        ],
      },
      { dependsOn: [attachment, ...(openapiReader ? [openapiReader.version] : [])] },
    )

    return {
      active: true as const,
      mode: 'self-hosted' as const,
      url: $interpolate`http://${instance.privateIp}:${HTTP_PORT}`,
      database: request.database,
      writer: {
        username: request.writerUsername,
        passwordRef: writer.resource.arn,
        credentialVersion: writer.version.versionId,
      },
      reader: {
        username: request.readerUsername,
        passwordRef: reader.resource.arn,
        credentialVersion: reader.version.versionId,
      },
      id: instance.id,
      ready: [ready],
    }
  }