Q: Clarify use of AWSOptions / GetAWSOptions
See original GitHub issueI 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:
- Created 3 years ago
- Comments:17 (9 by maintainers)
@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
@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 toAWS_
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 refactoredStartup
and related components so it does not accessAWSOptions
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! 🙇