无法在 InstanceProfileCredentialsProvider 上加载 EC2 实例的凭证

Not able to load credentials of an EC2 instance on InstanceProfileCredentialsProvider

我已将 EC2 角色附加到实例,附加到我的 EC2 实例,并且我是 运行 我的 AWS JAVA SDK。当我尝试以这种方式加载凭据时:

InstanceProfileCredentialsProvider instanceCred = new InstanceProfileCredentialsProvider();

我收到以下错误:

Exception in thread "main" com.amazonaws.AmazonClientException: Unable to load credentials.
    at com.amazonaws.auth.InstanceProfileCredentialsProvider.loadCredentials(InstanceProfileCredentialsProvider.java:195)
    at com.amazonaws.auth.InstanceProfileCredentialsProvider.getCredentials(InstanceProfileCredentialsProvider.java:124)

任何人都可以建议我可能遗漏了什么吗?

我能够通过 运行 我的 EC2 实例中的代码并增强我的代码来绕过它,这是代码:

 String arn = "arn:aws:iam::"+accountnumber+":role/My-CrossAccount-CustomRole-ReadOnly";

InstanceProfileCredentialsProvider instanceCred = new InstanceProfileCredentialsProvider();

stscred = new STSAssumeRoleSessionCredentialsProvider(instanceCred.getCredentials(),arn,"123",clientConfiguration);

确保您的角色操作中有 "sts:AssumeRole"