Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to retrieve AWS Credentials information from AWS SDK?

I'm trying to setup healthchecks for some required services to my dotnet core 3.1 API and I'm struggling on Amazon DynamoDB check.

We're using the Xabaril healthcheck packages and the DynamoDb one ask for a DynamoDBOptions that requires the AccessKey, SecretKey and RegionEndpoint.

I know the AWS SDK get this information from the environment Credentials profile configuration:

using Amazon.DynamoDBv2;
//... other usings

public void ConfigureServices(IServiceCollection services)
{
    // ... other stufs
    services.AddAWSService<IAmazonDynamoDB>();
    // ...
}

...But I need get it too in order to set up my dependency healthcheck like this:

    services.AddHealthChecks()
        .AddDynamoDb(dynamoDbOptions => 
            { 
                dynamoDbOptions .AccessKey = "<???>";
                dynamoDbOptions .RegionEndpoint = Amazon.RegionEndpoint.EUWest2; // <???>
                dynamoDbOptions .SecretKey = "<???>";
            }, "DynamoDB");

How can I get this <???> infos from AWS SDK packages?

like image 247
Diego Rafael Oliveira Souza Avatar asked Sep 13 '25 22:09

Diego Rafael Oliveira Souza


1 Answers

After spend some more time in the official docs, I found the topic that covers exactly this need: Accessing credentials and profiles in an application.

To actively retrieve profiles and credentials, use classes from the Amazon.Runtime.CredentialManagement namespace.

In fact, we can use the SharedCredentialsFile class to find a profile in a file that uses the AWS credentials file format, the NetSDKCredentialsFile class to find a profile in the SDK Store or even CredentialProfileStoreChain to search in both.

You can see the examples in the link above. I got this in my case:

private static AWSCredentials GetAccountCredentials(string profileName = "default")
{
    var chain = new CredentialProfileStoreChain();
    if (chain.TryGetAWSCredentials(profileName, out AWSCredentials awsCredentials))
        return awsCredentials;

    // ToDo: Error Handler
    return null;
}
like image 154
Diego Rafael Oliveira Souza Avatar answered Sep 16 '25 13:09

Diego Rafael Oliveira Souza