При попытке взять на себя роль IAM для использования с AWS SDK с использованием примера кода из документации, при вызове GetCallerIdentityAsync
выдается ошибка, сообщающая об этом Value cannot be null. Parameter name: Options property cannot be empty: ClientName
. Пример кода ниже:
using System;
using System.Threading.Tasks;
using Amazon;
using Amazon.SecurityToken;
using Amazon.SecurityToken.Model;
namespace AssumeRoleExample
{
class AssumeRole
{
/// <summary>
/// This example shows how to use the AWS Security Token
/// Service (AWS STS) to assume an IAM role.
///
/// NOTE: It is important that the role that will be assumed has a
/// trust relationship with the account that will assume the role.
///
/// Before you run the example, you need to create the role you want to
/// assume and have it trust the IAM account that will assume that role.
///
/// See https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create.html
/// for help in working with roles.
/// </summary>
private static readonly RegionEndpoint REGION = RegionEndpoint.USWest2;
static async Task Main()
{
// Create the SecurityToken client and then display the identity of the
// default user.
var roleArnToAssume = "arn:aws:iam::123456789012:role/testAssumeRole";
var client = new Amazon.SecurityToken.AmazonSecurityTokenServiceClient(REGION);
// Get and display the information about the identity of the default user.
var callerIdRequest = new GetCallerIdentityRequest();
var caller = await client.GetCallerIdentityAsync(callerIdRequest);
Console.WriteLine($"Original Caller: {caller.Arn}");
// Create the request to use with the AssumeRoleAsync call.
var assumeRoleReq = new AssumeRoleRequest()
{
DurationSeconds = 1600,
RoleSessionName = "Session1",
RoleArn = roleArnToAssume
};
var assumeRoleRes = await client.AssumeRoleAsync(assumeRoleReq);
// Now create a new client based on the credentials of the caller assuming the role.
var client2 = new AmazonSecurityTokenServiceClient(credentials: assumeRoleRes.Credentials);
// Get and display information about the caller that has assumed the defined role.
var caller2 = await client2.GetCallerIdentityAsync(callerIdRequest);
Console.WriteLine($"AssumedRole Caller: {caller2.Arn}");
}
}
}
Я создал пользователя IAM через онлайн-консоль и делегировал соответствующие разрешения на использование одной конкретной службы.
Как я могу указать ClientName
? кажется, в Интернете нет никакой информации об этом
Эта ошибка возникает из-за загрузки учетных данных и обычно не связана с фактическим вызовом SDK, который вы делаете. ClientName должно быть указано в учетных данных, но по какой-то причине это не так. Сначала я хотел бы устранить неполадки с вашей аутентификацией, а также просмотреть эту ветку на предмет возможных проблем.
Это произошло потому, что я работал на локальном компьютере без запущенного CLI.
Я пометил статью как нуждающуюся в улучшении со ссылкой на этот пост. Судя по моему опыту обучения, когда эти статьи AWS не работают, иногда на YouTube можно найти видео, в котором объясняются необходимые трюки или предварительные условия.