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?
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With