Tutorial AWS Lambda + SageMaker: Serverless ML Inference

# Tutorial Lengkap AWS Lambda + SageMaker: Serverless ML Inference Menggabungkan AWS Lambda dengan SageMaker memungkinkan inference ML yang serverless, scalable, dan hemat biaya. Tutorial ini mencaku...

By Ruby Abdullah · · tutorial
AWSLambdaSageMakerServerlessML InferenceAPI

Tutorial Lengkap AWS Lambda + SageMaker: Serverless ML Inference

Menggabungkan AWS Lambda dengan SageMaker memungkinkan inference ML yang serverless, scalable, dan hemat biaya. Tutorial ini mencakup pattern untuk mengintegrasikan fungsi Lambda dengan endpoint SageMaker untuk aplikasi ML production.

Mengapa Lambda + SageMaker?

Manfaat Utama:
  • Serverless: Tidak perlu mengelola infrastruktur
  • Hemat biaya: Bayar per invocation
  • Scalable: Scaling otomatis sesuai demand
  • Fleksibel: Multiple pattern integrasi
  • Event-driven: Trigger inference dari event AWS apapun

Use Cases:
  • Real-time API inference
  • Trigger batch processing
  • Event-driven ML pipelines
  • Inference hemat biaya
  • Multi-model serving

Prerequisites

pip install boto3 sagemaker

AWS CLI sudah dikonfigurasi

aws configure

Pattern 1: Lambda Memanggil SageMaker Endpoint

1. Deploy SageMaker Endpoint

import sagemaker

from sagemaker.sklearn import SKLearnModel

session = sagemaker.Session()

role = sagemaker.getexecutionrole()

Deploy model

model = SKLearnModel(

modeldata="s3://bucket/model/model.tar.gz",

role=role,

frameworkversion="1.0-1",

pyversion="py3"

)

predictor = model.deploy(

initialinstancecount=1,

instancetype="ml.m5.large",

endpointname="sklearn-endpoint"

)

2. Kode Fungsi Lambda

# lambdafunction.py

import json

import boto3

import os

Initialize SageMaker runtime client

sagemakerruntime = boto3.client("sagemaker-runtime")

ENDPOINTNAME = os.environ.get("SAGEMAKERENDPOINT", "sklearn-endpoint")

def lambdahandler(event, context):

"""Panggil SageMaker endpoint untuk inference."""

try:

# Parse input

if "body" in event:

body = json.loads(event["body"])

else:

body = event

features = body.get("features", [])

# Siapkan payload

payload = json.dumps({"features": features})

# Panggil endpoint

response = sagemakerruntime.invokeendpoint(

EndpointName=ENDPOINTNAME,

ContentType="application/json",

Body=payload

)

# Parse response

result = json.loads(response["Body"].read().decode())

return {

"statusCode": 200,

"headers": {"Content-Type": "application/json"},

"body": json.dumps({

"prediction": result,

"endpoint": ENDPOINTNAME

})

}

except Exception as e:

return {

"statusCode": 500,

"body": json.dumps({"error": str(e)})

}

3. Deploy Lambda dengan SAM

# template.yaml

AWSTemplateFormatVersion: '2010-09-09'

Transform: AWS::Serverless-2016-10-31

Resources:

InferenceFunction:

Type: AWS::Serverless::Function

Properties:

Handler: lambdafunction.lambdahandler

Runtime: python3.9

Timeout: 30

MemorySize: 256

Environment:

Variables:

SAGEMAKERENDPOINT: sklearn-endpoint

Policies:

  • Version: '2012-10-17'
Statement:

  • Effect: Allow
Action:

  • sagemaker:InvokeEndpoint
Resource: '*'

Events:

Api:

Type: Api

Properties:

Path: /predict

Method: post

# Deploy

sam build

sam deploy --guided

Pattern 2: Serverless Inference dengan Lambda Container

1. Lambda Container dengan Model

# Dockerfile

FROM public.ecr.aws/lambda/python:3.9

Install dependencies

COPY requirements.txt .

RUN pip install -r requirements.txt

Artikel Terkait

Tutorial AWS SageMaker Model Monitor: Monitoring Model Produksi

Tutorial Lengkap AWS SageMaker Model Monitor: Monitoring Model ML di Production Amazon SageMaker Model Monitor secara ot...

Tutorial AWS SageMaker Feature Store: Manajemen Feature untuk ML

Tutorial Lengkap AWS SageMaker Feature Store: Manajemen Fitur untuk ML Amazon SageMaker Feature Store adalah repositori ...

Tutorial AWS SageMaker Pipelines: ML Pipeline Automation

Tutorial Lengkap AWS SageMaker Pipelines: Automasi ML Workflows SageMaker Pipelines adalah layanan CI/CD yang dibuat khu...

Tutorial Lengkap AWS SageMaker: Machine Learning di Cloud

Tutorial Lengkap AWS SageMaker: End-to-End ML Pipeline Amazon SageMaker adalah layanan machine learning terkelola penuh ...