Closed opringle closed 10 months ago
Hi @opringle ,
The problem is, the get_execution_role() method is only used on AWS SageMaker notebook instances. So if you use it locally, it won't correctly parse your credential (from your stacktrace, I think you are using IAM user credential).
So if you want to use sagemaker locally, you can create an IAM role with enough SageMaker access permission. Then just directly use that role in your code.
Feel free to reopen this if you have more questions.
Thanks
This is really a pretty bad experience. get_execution_role()
sounds like it's going to just figure out all the IAM/role/confusion/whatever to make SageMaker work. And on a notebook instance it does. But if you run that same code on your laptop it fails, sending customers into IAM/role/confusion limbo.
Without this it's basically impossible to write a simple set of code that works both on a SageMaker notebook instance and anywhere else. Which is a real barrier to people who want to build the SageMaker ecosystem.
understood. definitely agree that the SDK can do better here. I'll leave this issue open as a feature request, and hopefully we can prioritize this work in the near future. Thanks @leopd!
Also having issues here, +1 to smoothing it out.
same
A temp solution is re-use the IAM role attached to your notebook (when you create the notebook, you had one there). You can get its arn
from IAM console.
I think local mode should work offline, what need is there to check credentials when running locally?
I have written this super hacky function to resolve the sagemaker execution role. it may fail miserably, and you should probably not use it at all. But, it may work in simple cases:
def resolve_sm_role():
client = boto3.client('iam', region_name=region)
response_roles = client.list_roles(
PathPrefix='/',
# Marker='string',
MaxItems=999
)
for role in response_roles['Roles']:
if role['RoleName'].startswith('AmazonSageMaker-ExecutionRole-'):
print('Resolved SageMaker IAM Role to: ' + str(role))
return role['Arn']
raise Exception('Could not resolve what should be the SageMaker role to be used')
sagemaker.get_execution_role()
could basically get the environment variable AWS_ROLE_SESSION_NAME
as it's documented for credentials setup, and that would fit local processing too. But, sorry, all AWS IAM needs a refactoring
Putting iluoyi's solution in code
try:
role = sagemaker.get_execution_role()
except ValueError:
iam = boto3.client('iam')
role = iam.get_role(RoleName='AmazonSageMaker-ExecutionRole-20191205T100050')['Role']['Arn']
A SageMaker execution role exists if you ever ran a job before, if not:
Then use the name in RoleName=
like above
A potential long term solution would be to create a function that checks for an existing execution service role, if it does not exist, then create the new role.....but service-role creation with managed policies through boto3 IAM requires......patience....
Any plans to fix this? This is very annoying if you want to execute notebooks locally. get_execution_role should create a default role with SM permissions when called out of a notebook.
Nothing yet?
Almost three years later and this is still an issue?
Got today "The current AWS identity is not a role: arn:aws:iam::XXXXXXXXXX:user/xxxxxxxx, therefore it cannot be used as a SageMaker execution role."
The above solution (https://github.com/aws/sagemaker-python-sdk/issues/300#issuecomment-577957428) is in docs now: https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-roles.html
No update there? This issue is 4 years old ...
Just stumbled across this issue. Will this issue ever be solved?
Inside SageMaker we can have multiple notebook instances and each notebook instance can have a different IAM role. When running your code locally get_execution_role will not work since there might be several roles dedicated to different SageMaker notebook instances. Therefore, you have to choose which is the right role to use.
In order to make your code work in both local and remote modes, you could instantiate a variable containing the specific value of IAM role, and implement a try block like here below.
local_variable_for_sm_role = “arn:aws:iam::XXXX:role/service-role/XXXXX”
try:
role = sagemaker.get_execution_role()
except ValueError:
role = local_variable_for_sm_role
It seems that sagemaker-python-sdk team does not care about the community issues.
I got the same error. Tried everything, is it still an issue?
I got the same error. Tried everything, is it still an issue?
I am getting around with: Created Sagemaker All Access Role and define role as the arn of this role, works for me. role = 'arn:aws:iam::ACCTNMRXXXX:role/SageMakerAllAccess'
How is this not fixed and just closed?
Please fill out the form below.
System Information
Describe the problem
sagemaker.get_execution_role()
. Instead, I receive abotocore.errorfactory.NoSuchEntityException
.Minimal repro / logs
To reproduce the problem:
Script:
Credentials:
Error:
pip install sagemaker && python mwe.py