最新资讯
stackoverflow.blog 欢迎来到芯片时代:半导体制造、AI 数据中心与芯片设计的未来 www.youtube.com AI Agent 总犯同样的错:CentralAgent AI 将记忆重构为经验系统 pub.towardsai.net Netflix 科学家建议重采样 LLM 评判:5 次仅将误差棒缩小 13% pub.towardsai.net Jev 作为 LLM 前的静默层:构建提示感知的模型路由器 reddit.com OpenAI 称 Astra 仅满足 10% 安全任务标准 dev.to FreeToken:在自有 GPU 上运行前沿 MoE 模型及其内存需求 dev.to 当 AI Agent 说“完成”时,究竟完成了什么? dev.to 部署 Underdog Bark 3.0 Pro:逐字转写、自定义后端与分块设置 dev.to 为艰难对话构建 AI:五个 Agent、一条安全正则与一位盲评监督者 techcrunch.com Palo Alto Networks 洽谈以 5 亿美元收购 AI Agent 安全初创公司 Clutch theinformation.com 黄仁勋被要求就涉华间谍指控向国会作简报 www.infoq.com Edera 推出 Object Capability Bounded Authority,试图将凭证与 AI Agent 工作负载隔离
多元动态
登录 注册
AI编码 dev.to · @xbill

在 Amazon SageMaker 端点上部署 Gemma 4:AWS CLI、NVIDIA L4 与 MCP Server

原文 我的收藏

该文给出将 Gemma 4 E2B 部署到 Amazon SageMaker 实时端点的分步指南:使用 AWS 发布的 vLLM 容器,在单张 NVIDIA L4 GPU 上托管推理服务,所有 AWS 操作均以 aws CLI 命令形式给出,便于手动执行或由 MCP 服务器调用。作者还构建了一套 Python MCP 工具,用于配合 Claude Code 管理该 vLLM 部署,并提供了 GitHub 仓库。文中说明 SageMaker 实时端点负责容器调度、健康检查、请求路由与 CloudWatch 日志,无需自行维护实例、安全组或负载均衡。

AI推荐理由 这是一篇面向开发者的实操部署指南,具体信息包括:在单张 NVIDIA L4 GPU 上通过 SageMaker 实时端点、使用 AWS 官方 vLLM 容器部署 Gemma 4 E2B,全部 AWS 调用以 aws CLI 命令呈现,并配套一套 Python MCP 工具用于通过 Claude Code 管理该 vLLM 部署,附有 GitHub 仓库链接。价值在于把模型部署与 MCP 工具链结合,对做 Agent/模型运维的开发者有参考意义。局限是正文片段仅到环境搭建部分,未提供性能数据、成本或验证结果,且来源为社区博客,非官方文档,故质量与兴趣分不宜过高。

62

正文

This article provides a step by step deployment guide for Gemma 4 E2B to an Amazon SageMaker hosted GPU enabled system. A suite of Python MCP tools is built to simplify management of the vLLM hosted deployment with Claude Code. https://github.com/xbill9/sagemaker-gemma What is this project trying to Do? This project serves Gemma 4 E2B from a SageMaker real-time endpoint on one NVIDIA L4 GPU, using the vLLM container AWS publishes for SageMaker. Every AWS call is a plain aws CLI command, so each step can be run by hand or by the MCP server. A SageMaker real-time endpoint is a managed HTTPS inference server. SageMaker places the container on a GPU instance, health-checks it, routes requests to it and writes its logs to CloudWatch. There is no instance to patch, no security group to open and no load balancer to build. Where do I start? The strategy for starting MCP development for model management is a incremental step by step approach. First, the basic development environment is setup with the required system variables and a working Claude Code configuration. Then, the Python MCP server is brought up over stdio and validated with Claude Code in the local environment. The deployment follows as eight steps, each shown as the raw aws command and the MCP tool that runs it. At This Point You Should Have… An AWS account and the AWS CLI v2, signed in with aws login A SageMaker endpoint quota of at least 1 for a single-L4 instance type (ml.g6.xlarge, ml.g6.2xlarge or ml.g6.4xlarge) Python 3.11 or newer with mcp 2.x Claude Code or Gemini CLI installed and working jq for reading JSON replies Setup the Basic Environment Clone the repository and install the one requirement into the system Python: git clone https://github.com/xbill9/sagemaker-gemma cd sagemaker-gemma python3 -m pip install -r requirements.txt cp .env.example .env Enter fullscreen mode Exit fullscreen mode .env is gitignored. It holds the settings every tool reads: cat .env Enter fullscreen mode Exit fullscreen mode # Copy to .env (gitignored) and edit. sm.py reads it, so the MCP server, CLI and Makefile all see it. AWS_REGION=us-east-2 MODEL_ID=google/gemma-4-E2B-it INSTANCE_TYPE=ml.g6.xlarge ENDPOINT_NAME=gemma-4-e2b ROLE_NAME=sagemaker-gemma-execution-role MAX_MODEL_LEN=8192 # Leave empty to use the newest SageMaker vLLM image in the region. IMAGE_URI= # Fallback instance types, highest priority first (same GPU keeps runs comparable). INSTANCE_POOLS=ml.g6.xlarge,ml.g6.2xlarge,ml.g6.4xlarge Enter fullscreen mode Exit fullscreen mode Gemma 4 is Apache-2.0 and ungated on Hugging Face, so no Hugging Face token is needed. Model Management Tool with MCP Stdio Transport The simplest MCP transport is stdio: the client launches the server as a local process and talks to it over stdin and stdout. In this project Claude Code is the MCP client. The server is one file, server.py, on the MCP Python SDK 2.x: mcp = MCPServer(RIG_NAME) READ_ONLY = ToolAnnotations(readOnlyHint=True, idempotentHint=True) WRITE = ToolAnnotations(destructiveHint=False) DESTRUCTIVE = ToolAnnotations(destructiveHint=True) Enter fullscreen mode Exit fullscreen mode Every tool carries one of the three annotations, so a client can tell a status check from a deploy from a delete. The tools do no AWS work themselves. They call sm.py, which runs each request as an aws CLI subprocess: def aws(*args: str, region: str | None = REGION, parse: bool = True) -> Any: """Run `aws --output json` and return the parsed result.""" cmd = ["aws", *args, "--output", "json"] Enter fullscreen mode Exit fullscreen mode The CLI renews an aws login session on its own, so a server that stays up for hours keeps working credentials. sm.py also drops any AWS_SESSION_TOKEN the server inherits from its parent process, because a static token expires inside a long-running server and outranks the login session. Running the Python Code The project can be linted: make lint Enter fullscreen mode Exit fullscreen mode All checks passed! 16 files already formatted Enter fullscreen mode Exit fullscreen mode and tested: make test Enter fullscreen mode Exit fullscreen mode ---------------------------------------------------------------------- Ran 16 tests in 0.010s OK Enter fullscreen mode Exit fullscreen mode The tests replace the aws subprocess with a fake, so they run offline with no credentials. One of them compares the registered tool set and annotations against a fixed list: a tool that failed to register, or a delete that lost its destructive flag, fails the suite. ✅ Test the Protocol by Hand A client speaks JSON-RPC over stdio. Hold stdin open with sleep, or the server sees end-of-input and exits before it answers: { printf '%s\n' \ '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"probe","version":"0"}}}' \ '{"jsonrpc":"2.0","method":"notifications/initialized"}' \ '{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}'; sleep 3; } \ | python3 server.py 2>/dev/null Enter fullscreen mode Exit fullscreen mode Summarised: 1 sagemaker-gemma 2 ['check_quotas', 'delete_endpoint', 'deploy_endpoint', 'find_vllm_image', 'get_deployment_config', 'get_endpoint_logs', 'get_endpoint_status', 'get_help', 'list_endpoints', 'query_model', 'verify_model_health'] Enter fullscreen mode Exit fullscreen mode 🟢 The server answers the handshake and lists 11 tools. Claude Code .mcp.json Claude Code reads .mcp.json in the project directory and launches the server with the system python3: { "mcpServers": { "sagemaker-gemma": { "command": "python3", "args": ["/home/xbill/sagemaker-gemma/server.py"] } } } Enter fullscreen mode Exit fullscreen mode Gemini CLI reads the same entry from .gemini/settings.json. Validation with Claude Code claude mcp get sagemaker-gemma Enter fullscreen mode Exit fullscreen mode sagemaker-gemma: Scope: Project config (shared via .mcp.json) Status: ✔ Connected Enter fullscreen mode Exit fullscreen mode From inside Claude Code, get_help returns the resolved settings and the order of work: ### sagemaker-gemma Serving `google/gemma-4-E2B-it` with vLLM on a **SageMaker real-time endpoint**, managed entirely through the aws CLI. | Setting | Value | | --- | --- | | Region | `us-east-2` | | Endpoint | `gemma-4-e2b` | | Instance types (priority order) | `ml.g6.xlarge`, `ml.g6.2xlarge`, `ml.g6.4xlarge` | | Max model length | `8192` | | Image | `newest SageMaker vLLM image (find_vllm_image)` | | Execution role | `sagemaker-gemma-execution-role` | **Order of work:** check_quotas → deploy_endpoint → get_endpoint_status until `InService` (about 10 minutes once an instance is placed) → verify_model_health → query_model → delete_endpoint. Enter fullscreen mode Exit fullscreen mode The steps below follow that order. Each one shows the raw CLI command; deploy_endpoint runs Steps 2 to 6 in one call. export AWS_REGION=us-east-2 AWS_PAGER= export NAME=gemma-4-e2b export MODEL_ID=google/gemma-4-E2B-it Enter fullscreen mode Exit fullscreen mode Step 1 — Check the Endpoint Quota SageMaker endpoint quotas are per instance type, per region, and count instances. aws service-quotas list-service-quotas --service-code sagemaker \ --query "Quotas[?QuotaName=='ml.g6.xlarge for endpoint usage'].Value" Enter fullscreen mode Exit fullscreen mode [ 1.0 ] Enter fullscreen mode Exit fullscreen mode The check_quotas tool reads the same quota for each fallback type across the US regions: | Instance | us-east-1 | us-east-2 | us-west-1 | us-west-2 | | --- | ---: | ---: | ---: | ---: | | `ml.g6.xlarge` | 1 | 1 | - | 1 | | `ml.g6.2xlarge` | 1 | 1 | - | 1 | | `ml.g6.4xlarge` | 1 | 1 | - | 1 | Regions with quota for at least one of these types: us-east-1, us-east-2, us-west-2. Enter fullscreen mode Exit fullscreen mode A - means the type is not offered in that region. A 0 means a quota increase request first. 🔎 Tip: One Quota Call per Region list-service-quotas pages through every SageMaker quota in the region, and the Service Quotas API is rate limited per account. Twelve calls at once, one per type per region, came back as: An error occurred (TooManyRequestsException) when calling the ListServiceQuotas operation (reached max retries: 2) Enter fullscreen mode Exit fullscreen mode check_quotas makes one call per region, one region at a time, and filters the types from that one result. sm.py also sets AWS_RETRY_MODE=adaptive so the CLI backs off and retries. Step 2 — Find the vLLM Container The AWS vLLM repository holds pinned release tags such as 0.30.0-gpu-py312-cu130-ubuntu24.04-sagemaker-v1.1, floating aliases such as 0.30-gpu-py312, and -soci index tags. Older release lines receive patch rebuilds, so the most recent push can carry an older vLLM. Sort the pinned tags by version: TAG=$(aws ecr describe-images --registry-id 763104351884 --repository-name vllm \ --query "imageDetails[].imageTags[]" --output text | tr '\t' '\n' \ | grep -E -- '^[0-9]+\.[0-9]+\.[0-9]+-.*-sagemaker-v[0-9]+\.[0-9]+$' | sort -V | tail -1) export IMAGE=763104351884.dkr.ecr.$AWS_REGION.amazonaws.com/vllm:$TAG echo $IMAGE Enter fullscreen mode Exit fullscreen mode 763104351884.dkr.ecr.us-east-2.amazonaws.com/vllm:0.30.0-gpu-py312-cu130-ubuntu24.04-sagemaker-v1.1 Enter fullscreen mode Exit fullscreen mode The find_vllm_image tool applies the same rule. The image runs vLLM 0.30.0. Step 3 — Create the Execution Role SageMaker assumes this role to pull the image and write logs. It is created once per account. aws iam create-role --role-name sagemaker-gemma-execution-role \ --assume-role-policy-document '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"sagemaker.amazonaws.com"},"Action":"sts:AssumeRole"}]}' aws iam attach-role-policy --role-name sagemaker-gemma-execution-role \ --policy-arn arn:aws:iam::aws:policy/AmazonSageMakerFullAccess export ROLE=$(aws iam get-role --role-name sagemaker-gemma-execution-role --query Role.Arn --output text) Enter fullscreen mode Exit fullscreen mode On a second run create-role reports EntityAlreadyExists; get-role still sets ROLE. deploy_endpoint creates the role only when it is missing. Step 4 — Create the Model A SageMaker model pairs an image with its settings. The container turns each SM_VLLM_ variable into the matching vLLM flag: SM_VLLM_MAX_MODEL_LEN becomes --max-model-len. aws sagemaker create-model --model-name $NAME --execution-role-arn $ROLE \ --primary-container "{\"Image\":\"$IMAGE\",\"Environment\":{ \"SM_VLLM_MODEL\":\"$MODEL_ID\", \"SM_VLLM_MAX_MODEL_LEN\":\"8192\", \"SM_VLLM_GPU_MEMORY_UTILIZATION\":\"0.9\"}}" Enter fullscreen mode Exit fullscreen mode { "ModelArn": "arn:aws:sagemaker:us-east-2::model/gemma-4-e2b" } Enter fullscreen mode Exit fullscreen mode Step 5 — Create the Endpoint Config With Fallback Instance Types The endpoint config says where the model runs. InstancePools lists up to five instance types in priority order, and SageMaker places the first one with a free instance. All three types below carry one L4, so the model sees the same GPU whichever is placed. aws sagemaker create-endpoint-config --endpoint-config-name $NAME \ --production-variants "[{\"VariantName\":\"AllTraffic\",\"ModelName\":\"$NAME\", \"InitialInstanceCount\":1, \"InstancePools\":[{\"InstanceType\":\"ml.g6.xlarge\",\"Priority\":1}, {\"InstanceType\":\"ml.g6.2xlarge\",\"Priority\":2}, {\"InstanceType\":\"ml.g6.4xlarge\",\"Priority\":3}], \"ContainerStartupHealthCheckTimeoutInSeconds\":1800, \"ModelDataDownloadTimeoutInSeconds\":1800}]" Enter fullscreen mode Exit fullscreen mode { "EndpointConfigArn": "arn:aws:sagemaker:us-east-2::endpoint-config/gemma-4-e2b" } Enter fullscreen mode Exit fullscreen mode The two 1800-second timeouts give the container time to download the weights and compile before SageMaker's health check gives up. 🔎 Tip: A Fallback List Holds Quota for Every Type in It With one Gemma endpoint running on ml.g6.xlarge from the three-type list above, a second endpoint asking for ml.g6.2xlarge in the same region was refused: ResourceLimitExceeded: The account-level service limit 'ml.g6.2xlarge for endpoint usage' is 1 Instances, with current utilization of 1 Instances and a request delta of 1 Instances. Enter fullscreen mode Exit fullscreen mode With a quota of 1 per type, a second endpoint goes in another region, or uses types outside the first endpoint's list. Step 6 — Create the Endpoint aws sagemaker create-endpoint --endpoint-name $NAME --endpoint-config-name $NAME Enter fullscreen mode Exit fullscreen mode { "EndpointArn": "arn:aws:sagemaker:us-east-2::endpoint/gemma-4-e2b" } Enter fullscreen mode Exit fullscreen mode Billing starts when an instance is placed. aws sagemaker wait endpoint-in-service --endpoint-name $NAME blocks until it is ready; get_endpoint_status reports the same state and the instance type that was placed: ✅ `gemma-4-e2b` in `us-east-2`: **InService** - Instance: `ml.g6.xlarge` Enter fullscreen mode Exit fullscreen mode The container log is in CloudWatch, and get_endpoint_logs tails it: aws logs tail /aws/sagemaker/Endpoints/$NAME --follow Enter fullscreen mode Exit fullscreen mode Start-up on the L4, in minutes after create-endpoint: Phase Minutes Weights loaded, 9.75 GiB in 82.75 s 7.4 KV cache sized, 723,484 tokens 9.2 InService 9.9 🔎 Tip: A Creating Endpoint Cannot Be Deleted create-endpoint cannot be undone until the endpoint settles. A delete-endpoint sent while it is Creating is refused: aws: [ERROR]: An error occurred (ValidationException) when calling the DeleteEndpoint operation: Cannot update in-progress endpoint "arn:aws:sagemaker:us-east-2::endpoint/gemma-4-e2b". Enter fullscreen mode Exit fullscreen mode The endpoint finishes starting, bills from the moment its instance is placed, and can be deleted once it reaches InService or Failed. Check the model ID and instance types before Step 6. 🔎 Tip: Capacity Is Separate From Quota A quota of 1 lets you request one instance; the region still has to have one free. In us-east-1, two requests for L4 instances each stayed in Creating for about 30 minutes and then failed: Unable to provision requested ML compute capacity due to InsufficientInstanceCapacity error. Please retry using a different ML instance type or after some time. Enter fullscreen mode Exit fullscreen mode The same request in us-east-2 placed an instance at once. While SageMaker waits for capacity, no container starts and the CloudWatch log group never appears, which is how get_endpoint_logs tells a capacity wait from a slow model load. No charge accrues during the wait. When it fails, repeat Steps 4 to 6 in another region where Step 1 shows the same quota. Step 7 — Cross Check the Deployed Model The vLLM container accepts an OpenAI chat body on invoke-endpoint: echo '{"messages":[{"role":"user","content":"Why is the sky blue?"}],"max_tokens":256}' > req.json aws sagemaker-runtime invoke-endpoint --endpoint-name $NAME \ --content-type application/json --body fileb://req.json out.json jq -r '.choices[0].message.content' out.json | head -1 jq -c '{model,usage}' out.json Enter fullscreen mode Exit fullscreen mode { "ContentType": "application/json", "InvokedProductionVariant": "AllTraffic" } The sky is blue due to a phenomenon called **Rayleigh scattering**. This process is caused by how sunlight interacts with the Earth's atmosphere. {"model":"google/gemma-4-E2B-it","usage":{"prompt_tokens":15,"total_tokens":271,"completion_tokens":256,"prompt_tokens_details":null,"completion_tokens_details":null}} Enter fullscreen mode Exit fullscreen mode verify_model_health sends one short request and checks for a reply: ✅ model=`google/gemma-4-E2B-it` tokens=2 wall=0.749s reply='ok' Enter fullscreen mode Exit fullscreen mode Step 8 — Teardown The endpoint bills by the hour until it is deleted. Deleting the config and the model as well leaves nothing behind. delete_endpoint runs all three: aws sagemaker delete-endpoint --endpoint-name $NAME aws sagemaker delete-endpoint-config --endpoint-config-name $NAME aws sagemaker delete-model --model-name $NAME Enter fullscreen mode Exit fullscreen mode Each command prints nothing and exits 0. list_endpoints confirms the account is clear: ### 0 endpoint(s) matching `gemma` Enter fullscreen mode Exit fullscreen mode Summary The goal of this article was to deploy Gemma 4 E2B to an Amazon SageMaker real-time endpoint with the AWS CLI and manage it from an MCP server. The key to the solution was the AWS vLLM SageMaker container, which turns the deployment into three create- calls and makes the endpoint answer OpenAI-style chat requests. The deployment results were: 🟢 Eight CLI steps take an account from quota check to a serving endpoint and back to nothing 🟢 The endpoint reached InService 9.9 minutes after create-endpoint, with the weights using 9.75 GiB of the L4's 24 GB 🟢 The MCP server runs every step from Claude Code, marks each tool read-only, write or destructive, and passes its tests offline 🟢 InstancePools gives one endpoint config several fallback instance types ⚠️ L4 capacity varied by region: us-east-1 refused twice, about 30 minutes each, while us-east-2 placed an instance at once ⚠️ A fallback list holds quota for every type in it Scope: one account, ml.g6.xlarge with one NVIDIA L4, vLLM 0.30.0 from the AWS container, google/gemma-4-E2B-it at full precision, deployed in us-east-2 on 2026-09-25. Start-up times are from a single deployment. The strategy for using MCP for SageMaker deployment was validated with an incremental step by step approach. References Repository: https://github.com/xbill9/sagemaker-gemma Gemma 4 E2B on Hugging Face: https://huggingface.co/google/gemma-4-E2B-it AWS Deep Learning Containers: https://github.com/aws/deep-learning-containers SageMaker real-time inference: https://docs.aws.amazon.com/sagemaker/latest/dg/realtime-endpoints.html CreateEndpointConfig API: https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateEndpointConfig.html MCP Python SDK: https://github.com/modelcontextprotocol/python-sdk vLLM: https://docs.vllm.ai/

来源报道 · 1 篇 · 以下链接均前往原始媒体

← 返回精选动态 原文

更多内容