You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用get_execution_role获取IAM角色时遇sagemaker-metrics未知服务错误

SageMaker获取IAM角色时遭遇sagemaker-metrics服务不存在错误

我用这段代码尝试编程获取IAM角色:

from sagemaker import get_execution_role
get_execution_role()

但触发了如下错误:

UnknownServiceError                       Traceback (most recent call last)
/tmp/ipykernel_8241/4227035378.py in <cell line: 1>()
----> 1 get_execution_role()
      2 role="arn:aws:iam::984132841759:role/service-role/AmazonSageMaker-ExecutionRole-20221129T111507",

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/sagemaker/session.py in get_execution_role(sagemaker_session)
   5039     """
   5040     if not sagemaker_session:
-> 5041         sagemaker_session = Session()
   5042     arn = sagemaker_session.get_caller_identity_arn()
   5043 

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/sagemaker/session.py in __init__(self, boto_session, sagemaker_client, sagemaker_runtime_client, sagemaker_featurestore_runtime_client, default_bucket, settings, sagemaker_metrics_client)
    131         self.settings = settings
    132 
-> 133         self._initialize(
    134             boto_session=boto_session,
    135             sagemaker_client=sagemaker_client,

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/sagemaker/session.py in _initialize(self, boto_session, sagemaker_client, sagemaker_runtime_client, sagemaker_featurestore_runtime_client, sagemaker_metrics_client)
    183             self.sagemaker_metrics_client = sagemaker_metrics_client
    184         else:
-> 185             self.sagemaker_metrics_client = self.boto_session.client("sagemaker-metrics")
    186         prepend_user_agent(self.sagemaker_metrics_client)
    187 

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/boto3/session.py in client(self, service_name, region_name, api_version, use_ssl, verify, endpoint_url, aws_access_key_id, aws_secret_access_key, aws_session_token, config)
    297 
    298         """
-> 299         return self._session.create_client(
    300             service_name,
    301             region_name=region_name,

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/botocore/session.py in create_client(self, service_name, region_name, api_version, use_ssl, verify, endpoint_url, aws_access_key_id, aws_secret_access_key, aws_session_token, config)
    868             * path/to/cert/bundle.pem - A filename of the CA cert bundle to
    869               uses.  You can specify this argument if you want to use a
-> 870               different CA cert bundle than the one used by botocore.
    871 
    872         :type endpoint_url: string

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/botocore/client.py in create_client(self, service_name, region_name, is_secure, endpoint_url, verify, credentials, scoped_config, api_version, client_config)
     85         loader,
     86         endpoint_resolver,
-> 87         user_agent,
     88         event_emitter,
     89         retry_handler_factory,

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/botocore/client.py in _load_service_model(self, service_name, api_version)
    152                 'signatureVersion'
    153             ),
-> 154         )
    155         client_args = self._get_client_args(
    156             service_model,

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/botocore/loaders.py in _wrapper(self, *args, **kwargs)
    130     for this to be used, it must be used on methods on an
    131     instance, and that instance *must* provide a
-> 132     ``self._cache`` dictionary.
    133 
    134     """

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/botocore/loaders.py in load_service_model(self, service_name, type_name, api_version)
    375     def load_service_model(self, service_name, type_name, api_version=None):
    376         """Load a botocore service model
-> 377 
    378         This is the main method for loading botocore models (e.g. a service
    379         model, pagination configs, waiter configs, etc.).

UnknownServiceError: Unknown service: 'sagemaker-metrics'. Valid service names are: accessanalyzer, account, acm, acm-pca, alexaforbusiness, amp, amplify, amplifybackend, amplifyuibuilder, apigateway, apigatewaymanagementapi, apigatewayv2, appconfig, appconfigdata, appflow, appintegrations, application-autoscaling, application-insights, applicationcostprofiler, appmesh, apprunner, appstream, appsync, athena, auditmanager, autoscaling, autoscaling-plans, backup, backup-gateway, batch, braket, budgets, ce, chime, chime-sdk-identity, chime-sdk-meetings, chime-sdk-messaging, cloud9, cloudcontrol, clouddirectory, cloudformation, cloudfront, cloudhsm, cloudhsmv2, cloudsearch, cloudsearchdomain, cloudtrail, cloudwatch, codeartifact, codebuild, codecommit, codedeploy, codeguru-reviewer, codeguruprofiler, codepipeline, codestar, codestar-connections, codestar-notifications, cognito-identity, cognito-idp, cognito-sync, comprehend, comprehendmedical, compute-optimizer, config, connect, connect-contact-lens, connectparticipant, cur, customer-profiles, databrew, dataexchange, datapipeline, datasync, dax, detective, devicefarm, devops-guru, directconnect, discovery, dlm, dms, docdb, drs, ds, dynamodb, dynamodbstreams, ebs, ec2, ec2-instance-connect, ecr, ecr-public, ecs, efs, eks, elastic-inference, elasticache, elasticbeanstalk, elastictranscoder, elb, elbv2, emr, emr-containers, es, events, evidently, finspace, finspace-data, firehose, fis

我已经尝试升级sagemaker和boto3到最新版本,但问题仍然存在,当前使用的是SageMaker Notebook的conda_tensorflow2_p38内核。


可行解决方案

  • 直接使用已知角色ARN:既然你已经明确角色ARN(arn:aws:iam::984132841759:role/service-role/AmazonSageMaker-ExecutionRole-20221129T111507),直接赋值给变量即可,无需调用get_execution_role():
role = "arn:aws:iam::984132841759:role/service-role/AmazonSageMaker-ExecutionRole-20221129T111507"
  • 手动初始化Session并禁用metrics客户端:如果必须使用Session,初始化时传入sagemaker_metrics_client=None来跳过该客户端的创建:
from sagemaker import Session, get_execution_role

session = Session(sagemaker_metrics_client=None)
role = session.get_execution_role()
  • 清理botocore缓存并重装:升级后缓存未更新可能导致服务模型缺失,执行以下命令清理缓存并重装botocore:
rm -rf ~/.aws/models/botocore
pip install --upgrade botocore
  • 切换到更新的内核:conda_tensorflow2_p38属于较旧的预置内核,尝试切换到conda_python3或官方提供的最新TensorFlow内核,这类内核预装了兼容版本的依赖库。

内容的提问来源于stack exchange,提问作者iamabhaykmr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 00:00:08