Skip to content

Latest commit

 

History

History
83 lines (59 loc) · 2.12 KB

deploy-api.md

File metadata and controls

83 lines (59 loc) · 2.12 KB

Deploy the inference API

Expose a SageMaker endpoint over HTTPS for a frontend (or curl) via API Gateway → Lambda → SageMaker.

This repo has no frontend. Deploy this stack, then set your app’s NEXT_PUBLIC_API_URL (or equivalent) to the predict URL.

Prerequisites

  • SageMaker endpoint InService (default name: album-classifier)
  • AWS CLI + Node.js 18+
  • CDK bootstrapped once per account/region

Deploy

cd infrastructure
npm install

export CDK_DEFAULT_REGION=us-east-2
npm run cdk bootstrap aws://YOUR_ACCOUNT_ID/us-east-2   # once

npm run deploy -- InferenceStack
# override endpoint: --context endpointName=your-endpoint

Get the URL:

aws cloudformation describe-stacks \
  --stack-name InferenceStack \
  --region us-east-2 \
  --query 'Stacks[0].Outputs[?OutputKey==`ApiGatewayPredictUrl`].OutputValue' \
  --output text

Request format

POST /predict with JSON (base64 image):

curl -X POST "$API_URL" \
  -H "Content-Type: application/json" \
  -d "{\"image\": \"$(base64 -i cover.jpg | tr -d '\n')\"}"

Lambda forwards the body to SageMaker and returns JSON predictions.

Frontend wiring

In your separate frontend app:

# .env.local
NEXT_PUBLIC_API_URL=https://xxxxx.execute-api.us-east-2.amazonaws.com/prod/predict

Send JSON, not multipart, when calling API Gateway:

async function predict(file: File) {
  const buf = await file.arrayBuffer();
  const image = btoa(String.fromCharCode(...new Uint8Array(buf)));
  const res = await fetch(process.env.NEXT_PUBLIC_API_URL!, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ image }),
  });
  return res.json();
}

Config tweaks

Edit infrastructure/lib/inference-stack.ts:

  • CORS — restrict allowOrigins to your Vercel domain before production
  • Timeout — Lambda is 120s; API Gateway REST sync still ~29s (cold starts may need a warm-up/retry)

Related