question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Q: Clarify use of AWSOptions / GetAWSOptions

See original GitHub issue

I followed documentation Configuring the AWS SDK for .NET with .NET Core which describes use of AWSOptions as it would be possible to override cloud environment by placing required values into appsettings.Development.json.

However when I deployed my service to ECS, nothing worked. Long story short, I ended up with this additional code in my Startup.cs:

        private AWSOptions GetAwsOptions()
        {
            var awsOptions = _configuration.GetAWSOptions();

            if (_env.IsDevelopment())
            {
                var credentialProfileStoreChain = new CredentialProfileStoreChain();
                if (credentialProfileStoreChain.TryGetAWSCredentials(awsOptions.Profile, out var credentials))
                {
                    awsOptions.Credentials = credentials;
                }
            }

            awsOptions.Credentials = awsOptions.Credentials ?? new ECSTaskCredentials();
            awsOptions.Region = awsOptions.Region ?? new EnvironmentVariableAWSRegion().Region;

            return awsOptions;
        }

Notice that I had to default to ECSTaskCredentials and EnvironmentVariableAWSRegion.Region - because IConfiguration.GetAWSOptions extension is returning empty AWSOptions if config section is not present (and I don’t want this section on cloud). See implementation of GetAWSOptions.

(I’m using DI to resolve IAmazonSecretsManager, and manually instantiating AmazonSimpleNotificationServiceClient providing access/secret key)

Is there any example how this API should be used correctly? I was expecting GetAWSOptions will take care of detecting on which environment code runs, so I can seamlessly use same code both on local env and cloud.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
3GDXCcommented, Apr 18, 2020

@wdolek Glad you got your issue sorted; and if I helped in anyway to identify your route cause then it’s time well spent as far as I’m concerned, I’m a developer that likes to try and help where I can and time is never wasted it’s always good to bounce around a few suggestions and/or have someone try and reproduce issues. That’s the wonderful would of open source and the open source community

2reactions
wdolekcommented, Apr 18, 2020

@grahamehorner @3GDXC I got confused by Development in example.

Anyway, I feel like total idiot now. I stepped back and started from scratch and it works. However I removed all AWS_ env variables from being set from our CDK, those are already present in ECS by default. I’m suspecting I was passing wrong value to AWS_ env var and thus it was failing for me (we obtained this project from another team some time ago and didn’t check if everything fits together - apparently not). I also refactored Startup and related components so it does not access AWSOptions right away - effectively eliminating another place where NRE could be thrown.

I’m closing this ticket. @3GDXC I’m sorry you wasted so much time with me, patiently explaining me how it should work, thank you! 🙇

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using AWSSDK.Extensions.NETCore.Setup
To use the Configuration object to get the AWS options, first add the AWSSDK.Extensions.NETCore.Setup NuGet package. Then, add your options to the configuration ......
Read more >
Credential Loading and the AWS SDK for .NET (Deep Dive)
In this post, I explore the locations and search order used to load credentials by the AWS SDK for .NET by diving deep...
Read more >
How to set credentials on AWS SDK on NET Core?
Eg: var awsOptions = Configuration.GetAWSOptions(); awsOptions.Credentials = new EnvironmentVariablesAWSCredentials(); services.
Read more >
Untitled
2016 · To use the Configuration object to get the AWS options, ... #58 - GitHub Q: Clarify use of AWSOptions / GetAWSOptions...
Read more >
Configuring the AWS SDK for .NET with .NET Core
In this post, we will step by step understand how to configure and set up a .NET application to use AWS SDK.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found