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); Each line
- Import the App Runner client and the command used to create a connector.
- Build a client that talks to the
us-east-1region. -
CreateVpcConnectorCommandreceives a name, a list of private subnets, and the security groups that will be attached to the ENI the connector creates. - 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", ]); Explanation of key lines
- Table creation includes a
VectorSearchConfigurationthat tells DynamoDB to maintain an index on theembeddingattribute. 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
PolicyDocumentattached to the endpoint is crucial – without it, App Runner will get anAccessDeniederror 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
- Receive a JSON payload
{ code: string }. - Convert the code to an embedding by calling a local helper (in production you’d use an embedding model; here we’ll stub it).
- Query DynamoDB with the VectorSearch operation to retrieve the three most similar stored snippets.
- Send those snippets to Claude (Anthropic’s LLM) via
fetch. - 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}`); }); 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(Source:https://dev.to/dineshgowtham/how-to-build-a-secure-ai-code-review-service-on-app-runner-with-vpc-connected-dynamodb-vector-5232Related posts
A busy lock, and the 5.8 wakes behind it

Flask API รับมือ 50,000 คำขอต่อวินาทีได้อย่างไร
Run a file browser for your whole LAN with one binary (no Docker, no database)
Before I let my agent sign anything, I make it prove the verdict — building MintGuard for zkVerify Agent Games
