aws-sdk-net

AWS SDK Configuration with Environmental Variables & Dependency Injection .NET Core


I am having trouble getting the AWS Setup Extension to process environment variables.

Following instructions on the AWS Docs + Unofficial Docs.

In my StartUp file, I add the following config and services.

 services.AddDefaultAWSOptions(root.GetAWSOptions());
 services.AddAWSService<IAmazonDynamoDB>();

My configuration includes a JSON file with AWS settings and also environmental variables.

    public static IConfigurationRoot Configuration  => new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .AddEnvironmentVariables()
    .Build();

According to the following diagram and the Unofficial Docs. AWS Setup should attempt to configure credentials from environmental variables failing higher priority options.

enter image description here

However, I have found it is able to import an AWS profile but not the environmental variables.

I've been investigating in the following repo under the console app if anyone cared to reproduce. https://github.com/aws/aws-sdk-net/issues/1717


Solution

  • I had the same issue, and I worked it out to the following:

    The AWS SDK uses the [default] profile credentials before it uses the session credentials set by environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN).

    The SDK does not validate the [default] profile credentials, it only cares that they exist.

    After deleting the [default] profile from the credentials file, the SDK used the environment variables as I originally expected.

    The credentials file is usually located at "~/.aws/credentials".

    AWS SDK for .NET credential and profile resolution:
    https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/creds-assign.html