Search code examples
c#amazon-web-servicesaws-sdkaws-sdk-net

AWS SDK: Value cannot be null. Parameter name: Options property cannot be empty: ClientName


When trying to assume an IAM role for use with AWS SDK using the example code from the documentation, an error is thrown when calling GetCallerIdentityAsync saying that Value cannot be null. Parameter name: Options property cannot be empty: ClientName. Code sample below:

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}");
    }
}
} 

I have created an IAM user via the online console and delegated the approriate permissions to use one specific service

How can i specify a ClientName? there doesnt seem to be any information online about this


Solution

  • That error is originating from the credentials load, and usually unrelated to the actual SDK call you are making. ClientName should be provided by the credentials, but for some reason it is not. I would troubleshoot your authentication first, also take a look at this thread for some possible issues.