DEV Community

Dinesh_gowtham
Dinesh_gowtham

Posted on

How to Build a Secure AI Code‑Review Service on App Runner with VPC‑Connected DynamoDB Vector Search, Explained Simply

App Runner just got VPC connectors, opening the door to private, low‑latency AI services. Imagine a code‑review bot that looks up similar snippets in a private vector store and asks Claude for suggestions—all without exposing data to the internet. This post shows how to make it happen.


Why App Runner + VPC Connectors Unlock Secure AI Services

Why it matters – An AI service that touches proprietary source code must stay inside your corporate network. Traditional approaches spin up an ECS cluster, attach a NAT gateway, and spend time wiring security groups. App Runner’s new VPC connector gives a managed, container‑based platform the ability to reach resources that live inside a VPC (Virtual Private Cloud – an isolated network slice in AWS).

Think of a VPC connector like a private hallway that runs from a hotel’s front desk (App Runner) straight to the kitchen (your DynamoDB endpoint). No hallway traffic can be seen from the street, so only authorized staff can walk through.

What you need to know first

Term Plain definition
VPC connector A bridge that lets an App Runner service send traffic into a VPC without using the public internet.
Security group A virtual firewall attached to an ENI (Elastic Network Interface) that controls inbound and outbound traffic.
IAM service role An AWS identity that App Runner assumes to call other AWS services, like DynamoDB.
Cold start The time it takes for a brand‑new container instance to start up the first request.

Key takeaway: The VPC connector is the only piece that makes a managed service like App Runner capable of talking to a private DynamoDB table, and it does so without exposing credentials or data to the public internet.

Minimal code: create the connector with the SDK

import {
  AppRunnerClient,
  CreateVpcConnectorCommand,
} from "@aws-sdk/client-apprunner";

const client = new AppRunnerClient({ region: "us-east-1" });

async function createConnector() {
  const cmd = new CreateVpcConnectorCommand({
    VpcConnectorName: "code-review-connector",
    Subnets: [
      "subnet-0a1b2c3d4e5f6g7h", // replace with your private subnet IDs
      "subnet-1b2c3d4e5f6g7h8i",
    ],
    SecurityGroups: ["sg-0123abcd4567efgh"], // allow DynamoDB endpoint traffic
  });

  const resp = await client.send(cmd);
  console.log("Connector ARN:", resp.VpcConnector?.VpcConnectorArn);
}

createConnector().catch(console.error);
Enter fullscreen mode Exit fullscreen mode

Each line

  1. Import the App Runner client and the command used to create a connector.
  2. Build a client that talks to the us-east-1 region.
  3. CreateVpcConnectorCommand receives a name, a list of private subnets, and the security groups that will be attached to the ENI the connector creates.
  4. Send the command and log the resulting ARN (Amazon Resource Name) – you’ll need this when you create the service later.

Tip: Choose subnets that have a route to the DynamoDB VPC endpoint; otherwise the connector will only reach the internet.


Provisioning a Private DynamoDB Vector Store

Why a vector store? – Modern code‑review bots compare a new snippet to millions of previously‑reviewed examples. Storing each snippet as a vector (a list of numbers that captures meaning) lets DynamoDB’s built‑in vector‑search find the nearest neighbours in milliseconds.

Table design

Attribute Meaning
pk (partition key) REPO#<repo-id> – groups snippets by repository.
sk (sort key) SNIPPET#<uuid> – uniquely identifies a snippet.
code The raw source code text (optional, for debugging).
embedding L2 vector type (list of 1536 floats) that stores the embedding.
metadata JSON blob with language, line numbers, etc.

In plain English: The table is like a bookshelf where each shelf (pk) holds a bunch of cards (sk). The card’s picture (embedding) is what we search against.

Creating the table and VPC endpoint

import {
  DynamoDBClient,
  CreateTableCommand,
  DescribeTableCommand,
} from "@aws-sdk/client-dynamodb";
import {
  EC2Client,
  CreateVpcEndpointCommand,
} from "@aws-sdk/client-ec2";

const ddb = new DynamoDBClient({ region: "us-east-1" });
const ec2 = new EC2Client({ region: "us-east-1" });

async function createVectorTable() {
  const tableCmd = new CreateTableCommand({
    TableName: "CodeSnippetVectors",
    AttributeDefinitions: [
      { AttributeName: "pk", AttributeType: "S" },
      { AttributeName: "sk", AttributeType: "S" },
    ],
    KeySchema: [
      { AttributeName: "pk", KeyType: "HASH" }, // partition key
      { AttributeName: "sk", KeyType: "RANGE" }, // sort key
    ],
    BillingMode: "PAY_PER_REQUEST", // no need to manage capacity units now
    // Enable the new vector search feature
    TableClass: "STANDARD",
    // The new `VectorSearchConfiguration` is part of the Table creation API
    // (as of 2025, see AWS docs for exact syntax)
    // Here we request a 1536‑dimensional L2 vector index
    VectorSearchConfiguration: {
      VectorSearchIndex: [
        {
          IndexName: "EmbeddingIndex",
          VectorType: "L2",
          Dimension: 1536,
          // The attribute that holds the vector
          VectorAttributeName: "embedding",
        },
      ],
    },
  });

  await ddb.send(tableCmd);
  console.log("Table created.");
}

async function createDynamoVpcEndpoint(vpcId: string, subnetIds: string[]) {
  const endpointCmd = new CreateVpcEndpointCommand({
    VpcId: vpcId,
    ServiceName: "com.amazonaws.us-east-1.dynamodb",
    SubnetIds: subnetIds,
    SecurityGroupIds: ["sg-0123abcd4567efgh"], // same SG as the connector
    PrivateDnsEnabled: true,
    // Policy that *only* allows access to the vector table
    PolicyDocument: JSON.stringify({
      Version: "2012-10-17",
      Statement: [
        {
          Effect: "Allow",
          Principal: "*",
          Action: ["dynamodb:*"],
          Resource: [
            "arn:aws:dynamodb:us-east-1:123456789012:table/CodeSnippetVectors",
            "arn:aws:dynamodb:us-east-1:123456789012:table/CodeSnippetVectors/index/*",
          ],
        },
      ],
    }),
  });

  const resp = await ec2.send(endpointCmd);
  console.log("VPC endpoint ID:", resp.VpcEndpoint?.VpcEndpointId);
}

// Run the helpers (replace IDs with yours)
await createVectorTable();
await createDynamoVpcEndpoint("vpc-0a1b2c3d", [
  "subnet-0a1b2c3d4e5f6g7h",
  "subnet-1b2c3d4e5f6g7h8i",
]);
Enter fullscreen mode Exit fullscreen mode

Explanation of key lines

  • Table creation includes a VectorSearchConfiguration that tells DynamoDB to maintain an index on the embedding attribute. The index uses L2 distance (Euclidean) to compare vectors.
  • The VPC endpoint is a gateway that lives inside your VPC, so traffic never leaves AWS’s private network.
  • The PolicyDocument attached to the endpoint is crucial – without it, App Runner will get an AccessDenied error that looks like a bad credential.

Tip: Keep the endpoint policy as tight as possible. Granting dynamodb:* on the whole account is a common mistake that defeats the security goal.


Building the Node.js Express AI Service

Why Express? – It’s a tiny web framework that lets us expose a single POST /review endpoint with almost no ceremony. Because we’re writing in TypeScript, we get type safety for request bodies and DynamoDB responses.

High‑level flow

  1. Receive a JSON payload { code: string }.
  2. Convert the code to an embedding by calling a local helper (in production you’d use an embedding model; here we’ll stub it).
  3. Query DynamoDB with the VectorSearch operation to retrieve the three most similar stored snippets.
  4. Send those snippets to Claude (Anthropic’s LLM) via fetch.
  5. Return Claude’s review comment to the caller.

The Express app (complete, ready to run)

import express, { Request, Response } from "express";
import { DynamoDBDocumentClient, QueryCommand } from "@aws-sdk/lib-dynamodb";
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import fetch from "node-fetch"; // node >=18 has built‑in fetch, but keep for compatibility

// -------------------------------------------------------------------
// 1️⃣  Set up AWS SDK clients – they automatically pick up the
//     IAM role attached to the App Runner service.
// -------------------------------------------------------------------
const ddbClient = new DynamoDBClient({});
const ddbDoc = DynamoDBDocumentClient.from(ddbClient);

// -------------------------------------------------------------------
// 2️⃣  Helper: fake embedding generator (replace with real model later)
// -------------------------------------------------------------------
function fakeEmbedding(code: string): number[] {
  // A deterministic pseudo‑embedding: hash the code string and repeat
  // the hash value to fill a 1536‑dimensional vector.
  const hash = Array.from(code)
    .reduce((acc, ch) => acc + ch.charCodeAt(0), 0) % 1000;
  return new Array(1536).fill(hash / 1000);
}

// -------------------------------------------------------------------
// 3️⃣  Express app definition
// -------------------------------------------------------------------
const app = express();
app.use(express.json()); // parse JSON bodies

app.post("/review", async (req: Request, res: Response) => {
  const { code } = req.body as { code?: string };

  if (!code) {
    return res.status(400).json({ error: "Missing 'code' in request body" });
  }

  // ---------------------------------------------------------------
  // Step A – turn code into a vector
  // ---------------------------------------------------------------
  const embedding = fakeEmbedding(code);

  // ---------------------------------------------------------------
  // Step B – query DynamoDB vector index for similar snippets
  // ---------------------------------------------------------------
  const query = new QueryCommand({
    TableName: "CodeSnippetVectors",
    IndexName: "EmbeddingIndex", // the vector index we created
    // The new `VectorSearch` field tells DynamoDB to perform a k‑NN lookup
    VectorSearch: {
      K: 3, // we want the top 3 neighbours
      Vector: embedding,
      // Optional: you can filter by repo or language later
    },
    // We still need a partition key value; using a wildcard by specifying
    // a dummy that matches all items works because the vector index ignores it.
    KeyConditionExpression: "pk = :pk",
    ExpressionAttributeValues: {
      ":pk": "REPO#all", // a sentinel partition that contains every snippet
    },
  });

  let similarItems: { code: string; metadata: any }[] = [];

  try {
    const result = await ddbDoc.send(query);
    similarItems = (result.Items ?? []).map((item) => ({
      code: item.code,
      metadata: item.metadata,
    }));
  } catch (err) {
    console.error("Vector query failed:", err);
    return res.status(500).json({ error: "Failed to search snippets" });
  }

  // ---------------------------------------------------------------
  // Step C – ask Claude for a review comment
  // ---------------------------------------------------------------
  const claudeResponse = await fetch("https://api.anthropic.com/v1/chat/completions", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "x-api-key": process.env.CLAUDE_API_KEY ?? "", // injected via App Runner env
    },
    body: JSON.stringify({
      model: "claude-3-5-sonnet-20241022", // example model name
      messages: [
        {
          role: "system",
          content:
            "You are a helpful code reviewer. Suggest improvements and point out bugs.",
        },
        {
          role: "user",
          content: `Here is the new snippet:\n\`\`\`\n${code}\n\`\`\`\n` +
            `Here are three similar snippets from our private store:\n` +
            similarItems.map((s, i) => `Snippet ${i + 1}:\n${s.code}\n`).join("\n"),
        },
      ],
      max_tokens: 500,
    }),
  });

  if (!claudeResponse.ok) {
    console.error("Claude API error:", await claudeResponse.text());
    return res.status(502).json({ error: "Claude service failed" });
  }

  const claudeJson = await claudeResponse.json();
  const reviewComment = claudeJson.choices?.[0]?.message?.content ?? "No comment";

  // ---------------------------------------------------------------
  // Step D – return the review
  // ---------------------------------------------------------------
  res.json({
    review: reviewComment,
    similarSnippets: similarItems,
  });
});

// -------------------------------------------------------------------
// 4️⃣  Start the HTTP server (App Runner will call this)
// -------------------------------------------------------------------
const PORT = process.env.PORT ? parseInt(process.env.PORT) : 8080;
app.listen(PORT, () => {
  console.log(`AI review service listening on port ${PORT}`);
});
Enter fullscreen mode Exit fullscreen mode

What each block does

Lines 1‑7 – Import required libraries. The AWS SDK reads credentials from the App Runner role, so we never hard‑code keys.

Lines 10‑17 – Create a DynamoDBDocumentClient, a higher‑level wrapper that lets us work with plain JavaScript objects instead of low‑level attribute maps.

Lines 20‑30 – fakeEmbedding is a placeholder. In a real system you’d call an embedding model (e.g., @aws-sdk/client-bedrock or a self‑hosted encoder).

Lines 36‑41 – Configure Express to parse JSON bodies.

Lines 43‑99 – The core handler: validate input, produce an embedding, run a vector search, call Claude, and send the result back.

Lines 103‑108 – Start listening on the port defined by the environment (App Runner injects PORT).

Analogy: Think of the vector search as asking a librarian to hand you the three books whose cover colors most closely match a new painting you just brought in. The librarian (DynamoDB) knows the exact RGB values (the vector) for each cover and can instantly find the best matches.


Deploying to App Runner with the AWS SDK

Why use the SDK instead of the console? – The SDK lets you version‑control the whole infrastructure, replay the exact steps on a test account, and avoid manual mis‑clicks that often omit the required service role or security‑group rule.

Step‑by‑step deployment script

import {
  AppRunnerClient,
  CreateServiceCommand,
  DeleteServiceCommand,
} from "@aws-sdk/client-apprunner";
import {
  IAMClient,
  CreateRoleCommand,
  AttachRolePolicyCommand,
} from "@aws-sdk/client-iam";

const REGION = "us-east-1";
const client = new AppRunnerClient({ region: REGION });
const iam = new IAMClient({ region: REGION });

async function ensureServiceRole() {
  const roleName = "AppRunnerCodeReviewRole";

  // 1️⃣ Create an IAM role that App Runner can assume
  const createRole = new CreateRoleCommand({
    RoleName: roleName,
    AssumeRolePolicyDocument: JSON.stringify({
      Version: "2012-10-17",
      Statement: [
        {
          Effect: "Allow",
          Principal: { Service: "build.apprunner.amazonaws.com" },
          Action: "sts:AssumeRole",
        },
      ],
    }),
  });

  const roleResp = await iam.send(createRole);
  console.log("Created role:", roleResp.Role?.Arn);

  // 2️⃣ Attach the managed policy that gives DynamoDB access
  const attachPolicy = new AttachRolePolicyCommand({
    RoleName: roleName,
    PolicyArn:
      "arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess", // scope down later in production
  });
  await iam.send(attachPolicy);
  console.log("Attached DynamoDB policy to role");
  return roleResp.Role?.Arn!;
}

// -------------------------------------------------------------------
// 3️⃣ Create the App Runner service that runs the Express container
// -------------------------------------------------------------------
async function createAppRunnerService() {
  const roleArn = await ensureServiceRole();

  const cmd = new CreateServiceCommand({
    ServiceName: "secure-code-review",
    SourceConfiguration: {
      // We are using a Dockerfile in a public ECR repo; replace with yours
      ImageRepository: {
        ImageIdentifier:
          "123456789012.dkr.ecr.us-east-1.amazonaws.com/code-review:latest",
        ImageRepositoryType: "ECR_PUBLIC",
      },
      // No build step needed – the image already contains the compiled TS
    },
    InstanceConfiguration: {
      Cpu: "1 vCPU",
      Memory: "2 GB",
    },
    // 4️⃣ Attach the VPC connector we created earlier
    NetworkConfiguration: {
      EgressConfiguration: {
        EgressType: "VPC",
        VpcConnectorArn:
          "arn:aws:apprunner:us-east-1:123456789012:vpcconnector/code-review-connector",
      },
    },
    // 5️⃣ Give the service the IAM role we prepared
    ServiceRoleArn: roleArn,
    // 6️⃣ Environment variables – keep secrets out of code
    EnvironmentVariables: [
      { Name: "CLAUDE_API_KEY", Value: process.env.CLAUDE_API_KEY ?? "" },
      { Name: "AWS_REGION", Value: REGION },
    ],
    // 7️⃣ Health check – App Runner expects a 200 response on `/` (we expose it)
    HealthCheckConfiguration: {
      Protocol: "TCP",
      // A short timeout prevents silent failures when the container never starts
      HealthyThreshold: 1,
      Interval: 10,
      Timeout: 5,
      UnhealthyThreshold: 3,
    },
  });

  const resp = await client.send(cmd);
  console.log("App Runner service ARN:", resp.Service?.ServiceArn);
}

// -------------------------------------------------------------------
// Run the deployment
// -------------------------------------------------------------------
await createAppRunnerService().catch((e) => {
  console.error("Deployment failed:", e);
});
Enter fullscreen mode Exit fullscreen mode

Explanation of the crucial pieces

  1. IAM role – App Runner needs a role to call DynamoDB. The AssumeRolePolicyDocument tells AWS that the build service (the part that runs your container) is allowed to assume the role.
  2. Managed policy – We attach AmazonDynamoDBFullAccess for the demo; in production you would craft a custom policy that mirrors the VPC endpoint policy (only the CodeSnippetVectors table).
  3. NetworkConfiguration – This is where we reference the VPC connector ARN created earlier. Without this, the service would try to reach DynamoDB over the public internet and the endpoint policy would block it, producing an AccessDenied error that looks like bad credentials.
  4. Health check – App Runner pings the root path (/) using TCP. If the container never becomes ready, the service will stay in a “deploying” state without obvious logs. Setting a short timeout surfaces the problem quickly.

Helpful tip: After the first deployment, open the service URL in a browser; you should see a “Cannot GET /” message from Express, which means the container is alive and the health check passed.


Observability, Testing, and Debugging

Why observability matters – A secure AI pipeline hides many moving parts (container start‑up, VPC traffic, DynamoDB vector queries, external LLM calls). Without logs and metrics you cannot tell where a latency spike or failure originates.

CloudWatch Logs & Metrics

App Runner automatically streams container stdout/stderr to CloudWatch Logs. Add a few console.log statements in the Express handler (already present) to see:

  • Incoming request payload size
  • Result of the vector query (number of items returned)
  • Claude response status

You can also enable X‑Ray tracing on the service (via the console) to visualize the request flow across the VPC connector and DynamoDB.

Common gotchas and how to spot them

Symptom Likely cause Fix
AccessDenied from DynamoDB VPC endpoint policy missing dynamodb:* for the table Update the endpoint policy to include the table ARN.
502 from Claude Missing or malformed CLAUDE_API_KEY environment variable Verify the key is set in the App Runner “Environment variables” page and redeploy.
504 timeout on /review Cold start > 20 s + vector query taking long Warm the service with a scheduled ping, or increase Cpu/Memory to reduce start time.
No items returned from vector search Partition key mismatch (pk value) Use a sentinel partition that contains all snippets, or add a GSI that partitions by language.

Local testing before deployment

  1. Run DynamoDB Local (or use a dev table) and point the SDK to it via AWS_ENDPOINT_URL.
  2. Start the Express server with npm run dev.
  3. Call the endpoint using curl:
curl -X POST http://localhost:8080/review \
  -H "Content-Type: application/json" \
  -d '{"code":"function add(a,b){return a+b;}"}'
Enter fullscreen mode Exit fullscreen mode

You should see a JSON payload with a review field and an array of similarSnippets.

In plain English: If you can get a review locally, the only remaining variables are network (VPC) and IAM permissions – that’s where the hidden “AccessDenied” bugs usually appear.


The Takeaway

**Key points


Transparency notice

This article was written with the help of an AI system — Groq (GPT OSS 120B).

Published: 2026-11-20 · Primary focus: AppRunner

All code blocks are intended to be correct and runnable, but please verify them
against the official docs for the tools mentioned before using in production.

Find an error? Drop a comment — corrections are always welcome.

Top comments (0)