iamabhaykmr
iamabhaykmr

Reputation: 2011

get_execution_role() sagemaker: UnknownServiceError: Unknown service: 'sagemaker-metrics'. Valid service names are: accessanalyzer

I am trying this code to get iam role programmatically.

from sagemaker import get_execution_role
get_execution_role()

It's giving the following error.

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

I tried multiple solution from the internet like upgrading sagemaker and boto3 to latest version without success.

I am using conda_tensorflow2_py38 kernel in sagemaker notebook.

Upvotes: 9

Views: 6051

Answers (4)

rach
rach

Reputation: 11

I was deploying my code as Lambda image, so could not restart the kernel as others have suggested. What helped me was installing boto3 directly in my code as below:

import subprocess
import sys

subprocess.call('pip3 install boto3 -t /tmp/ --no-cache-dir'.split(), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
sys.path.insert(1, '/tmp/')


Upvotes: 0

y2kmarkham
y2kmarkham

Reputation: 35

update the boto3 and sagemaker %pip install --upgrade boto3 sagemaker in your notebook

and do remember to RESTART your kernel

Upvotes: 1

awslearning pankaj
awslearning pankaj

Reputation: 1

pip install --upgrade boto3 will help in this as there are some upgrades REF - https://sagemaker.readthedocs.io/en/stable/v2.html for more deatils

Upvotes: 0

durga_sury
durga_sury

Reputation: 1152

Upgrade your boto3 installation in your notebook by running this - %pip install --upgrade boto3. Once that's upgraded, restart your kernel and run the cells above, it should work as expected.

The get_execution_role() function is looking for a SageMaker session and creates one if it doesn't exist, and with the later version of the sagemaker sdk, it is trying to create a client for sagemaker-metrics as well, which isn't supported with the older boto3 version.

Upvotes: 15

Related Questions