'aws cli fails to return a role policy

I am copying the name of a policy a created (and attached to a role) and running the following command:

▶ aws iam get-role-policy --role-name MyRole  --policy-name MyPolicy

however I am getting:

An error occurred (NoSuchEntity) when calling the GetRolePolicy operation: The role policy with name MyPolicy cannot be found.

The policy is right there, I am copying the name from the AWS console.

What is the issue here?

I have also tried the following, that does list the policy

$ aws iam list-attached-role-policies --role-name MyRole

{
    "AttachedPolicies": [
        {
            "PolicyName": "MyPolicy",
            "PolicyArn": "arn:aws:iam::123456789:policy/MyPolicy"
        }
    ]
}
(END)


Solution 1:[1]

list-attached-role-policies lists all managed policies attached to a role and get-role-policy retrieves an inline policy. In order to retrieve a managed policy you'll want to use get-policy, get the policy version from there and retrieve it using get-policy-version.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 alex