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.
- SageMaker endpoint
InService(default name:album-classifier) - AWS CLI + Node.js 18+
- CDK bootstrapped once per account/region
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-endpointGet the URL:
aws cloudformation describe-stacks \
--stack-name InferenceStack \
--region us-east-2 \
--query 'Stacks[0].Outputs[?OutputKey==`ApiGatewayPredictUrl`].OutputValue' \
--output textPOST /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.
In your separate frontend app:
# .env.local
NEXT_PUBLIC_API_URL=https://xxxxx.execute-api.us-east-2.amazonaws.com/prod/predictSend 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();
}Edit infrastructure/lib/inference-stack.ts:
- CORS — restrict
allowOriginsto your Vercel domain before production - Timeout — Lambda is 120s; API Gateway REST sync still ~29s (cold starts may need a warm-up/retry)
- Stack code:
infrastructure/lib/inference-stack.ts - Lambda:
infrastructure/lambda/lambda_function.py - sagemaker.md · troubleshooting.md