Aws-sdk-net: Support setting region with Environment variables

Created on 19 Nov 2016  路  3Comments  路  Source: aws/aws-sdk-net

Problem:
To use .NET Core AWS SDK on Linux and Docker, I want to set the Region using environement variable instead of relying on credential file.

Expected:
When Region is not set in AWSOptions, it will fall back to use 'AWS_REGION' environment variable to set the region.

Actual:
Amazon.Extensions.NETCore.Setup.ClientFactory.CreateClient would throw an exception "Amazon.Runtime.AmazonClientException: No RegionEndpoint or ServiceURL configured.".

Steps to reproduce:

  1. Remove Region property from appsettings.json.
  2. Initiate any of the service client, e.g. AwsEcsService using default constructor.

Source of problem:

  1. ClientFactory.cs Line 158
    config.RegionEndpoint = options.Region;
    This effectively sets RegionEndpoint to null, at the same time, ClientConfig.cs's probeForRegionEndpoint is set to false, which disables probing from environment variable.

Workaround:

  1. Create AWSOptions object manually (do not use the one in appsettings). Read the Region environment variable and set it to RegionEndpoint.
var region = Environment.GetEnvironmentVariable(EnvironmentVariableAWSRegion.ENVIRONMENT_VARIABLE_REGION);
if (string.IsNullOrEmpty(region))
    throw new ArgumentNullException($"Cannot read '{EnvironmentVariableAWSRegion.ENVIRONMENT_VARIABLE_REGION}' from environment variable.", nameof(region));
services.AddDefaultAWSOptions(new AWSOptions
{
    Region = RegionEndpoint.GetBySystemName(region)
});

Suggestion:
It will be nice to simply set new AWSOptions() without having to read it manually from environment variable like

services.AddDefaultAWSOptions(new AWSOptions());
bug

Most helpful comment

Also ran into this issue recently. I was able to fix it by setting the AWS_REGION along with AWS_DEFAULT_REGION environment variable as well.

All 3 comments

Thanks for letting us know about the issue. I just pushed version 3.3.0.2 of AWSSDK.Extensions.NETCore.Setup that fixes the issue so that clients can pick up the region by the environment variable.

Hi,

I'm still getting the exception below when my class tries to resolve the service IAmazonRekognition via DI.

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Amazon.Runtime.AmazonClientException: No RegionEndpoint or ServiceURL configured
   at Amazon.Runtime.ClientConfig.Validate() in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\ClientConfig.cs:line 472
   at Amazon.Runtime.AmazonServiceClient..ctor(AWSCredentials credentials, ClientConfig config) in D:\JenkinsWorkspaces\trebuchet-stage-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\AmazonServiceClient.cs:line 154
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Amazon.Extensions.NETCore.Setup.ClientFactory.CreateClient(Type serviceInterfaceType, AWSCredentials credentials, ClientConfig config) in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\extensions\src\AWSSDK.Extensions.NETCore.Setup\ClientFactory.cs:line 103
   at Amazon.Extensions.NETCore.Setup.ClientFactory.CreateServiceClient(ILogger logger, Type serviceInterfaceType, AWSOptions options) in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\extensions\src\AWSSDK.Extensions.NETCore.Setup\ClientFactory.cs:line 90

I'm using the following nuget packages:

  • AWSSDK.Rekognition 3.3.15.6
  • AWSSDK.Extensions.NETCore.Setup 3.3.6

I also have set the environment variables as stated in the docs:

  • AWS_DEFAULT_REGION
  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY

The @imkheong 's workaround is working ;)

Also ran into this issue recently. I was able to fix it by setting the AWS_REGION along with AWS_DEFAULT_REGION environment variable as well.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

genifycom picture genifycom  路  4Comments

Technolink picture Technolink  路  4Comments

ShahriatHossain picture ShahriatHossain  路  4Comments

mihafreenode picture mihafreenode  路  4Comments

radleta picture radleta  路  3Comments