Skip to content

sagemaker.get_execution_role() returns incorrect ARN #2089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hughack opened this issue Jan 14, 2021 · 3 comments · Fixed by #2191
Closed

sagemaker.get_execution_role() returns incorrect ARN #2089

hughack opened this issue Jan 14, 2021 · 3 comments · Fixed by #2191

Comments

@hughack
Copy link

hughack commented Jan 14, 2021

Describe the bug
I manually created an execution role for SageMaker Studio called AmazonSageMaker-ExecutionRole-ap-southeast-2

Calling sagemaker.get_execution_role() within a SageMaker Studio Notebook returns the incorrect role ARN:

arn:aws:iam::************:role/service-role/AmazonSageMaker-ExecutionRole-ap-southeast-2

Actual ARN:

arn:aws:iam::************:role/AmazonSageMaker-ExecutionRole-ap-southeast-2

To reproduce
Create role in CloudFormation:

  SageMakerExecutionRole:
      Type: "AWS::IAM::Role"
      Properties:
        RoleName: !Sub AmazonSageMaker-ExecutionRole-${AWS::Region}
        AssumeRolePolicyDocument:
          Version: 2012-10-17
          Statement:
            - Effect: Allow
              Principal:
                Service:
                  - sagemaker.amazonaws.com
                  - ecs-tasks.amazonaws.com
              Action:
                - "sts:AssumeRole"
        Path: /
        ManagedPolicyArns:
          - arn:aws:iam::aws:policy/AmazonSageMakerFullAccess
          - !Ref SageMakerExecutionPolicy

Setup SageMaker Studio with this role as execution role.

Open a notebook and run:

from sagemaker import get_execution_role
print(get_execution_role())

Expected behavior
Output role ARN:

arn:aws:iam::************:role/AmazonSageMaker-ExecutionRole-ap-southeast-2

Actual output:

arn:aws:iam::************:role/service-role/AmazonSageMaker-ExecutionRole-ap-southeast-2

Note addition of service-role/.

Additional context
Looks like this line is the culprit:

if "AmazonSageMaker-ExecutionRole" in assumed_role:

@hughack
Copy link
Author

hughack commented Jan 15, 2021

Do the auto-generated roles always have the same format? AmazonSageMaker-ExecutionRole-YYYYMMDDTHHMMSS

Could replace the if with:

if re.match(r'/.*AmazonSageMaker-ExecutionRole-\d{8}T\d{6}.*/',assumed_role):
    ...

But if any other pattern has been used in the past then this could cause issues for anyone using the alternative role name.

I'm guessing i should just change the role name for my account?

@cfregly
Copy link

cfregly commented Jan 15, 2021

I have similar issues when using get_execution_role() within a SageMaker Processing Job, for example. I just ended up doing the hack that you suggested above @hughack by converting assumed-role => role or role/service-role specific to each variant that I’m struggling with. My code is up to 3-4 if statements. Hoping there is a cleaner way.

Sagemaker Engineering: any suggestions?

@ChoiByungWook
Copy link
Contributor

ChoiByungWook commented Mar 6, 2021

Hey @hughack,

Apologies on the late response.

Thank you for bringing this issue to our attention.

I'm not entirely sure why we have a conditional to essentially hardcode the path "service-role" if the role name contains "AmazonSageMaker-ExecutionRole".

This code seems to have been part of the Python SDK since its inception, so I am bit worried of removing this logic, as many of the original writers... don't work on SageMaker anymore :(, but clearly it is causing issues.

Based on the code that comes after that specific conditional, it looks like it was looking to avoid making calls to IAM, since I am guessing due to missing permissions?

I believe at the time the code was introduced and even at the time of writing this response, it looks like the default SageMaker IAM policy doesn't account for IAM read permissions.

I don't think it is actually possible to get the proper IAM Role ARN without having IAM permissions, at least from the Python SDK perspective.

I think the best course of action is to move line of code into the except block of the try/except clause, as a safe compromise, to maintain supporting users of this conditional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants