0

Running this code in the console application is working but not in webAPI project .netCore6 with the docker container. I'm getting Error:-

"Unable to get IAM security credentials from EC2 Instance Metadata Service."

var region = RegionEndpoint.EUWest1;
var client = new AmazonCognitoIdentityProviderClient(region);
var request = new ListGroupsRequest
{
    UserPoolId = "MyUserPoolID" 
};
var response = await client.ListGroupsAsync(request);

Any help? Thanks

Arpit Jain
  • 1,599
  • 9
  • 23
Yaron
  • 29
  • 5

2 Answers2

1

Issue

When you run your application without docker, .NET SDK for AWS loads the credentials from the credential file located at C:\Users\.aws\credentials location.

When running the application in docker, credential file is not found in the container. Hence, SDK fallbacks to other ways to locate the AWS credentials.

Here, I have explained how does AWS SDK for .NET loads credentials.

Solution

Now, answering you question:

  1. If you are running the docker containers inside ECS, then you can use ECS Task Role, this way you don't need to maintain environment variables.
  2. If you are not running the docker containers inside ECS, then maintain the following environment variables in your container.
    AWS_ACCESS_KEY_ID
    AWS_SECRET_ACCESS_KEY
    

This should fix your issue.

Add Environment Variables when running from Visual Studio

In launchSettings.json, the settings in the Docker section are related to how Visual Studio handles containerized apps.

You can add the above environment variables in the environmentVariables section as shown below in the picture.

enter image description here

Refer Container Tools launch settings for more detail.

Ankush Jain
  • 5,654
  • 4
  • 32
  • 57
  • Thanks, for now I'm running locally using VS2022 How do I set the environment variables in my container? – Yaron May 30 '23 at 11:28
  • Thank you, that worked But only after I added the session token. I there a chance to set only the profile name as environment variable? – Yaron May 30 '23 at 13:11
0

The issue is your credentials are not being located within the container. You need to make sure that your credentials are available. One way is you can use an environment variable crendential provider and set both the key and secret key when you start your docker container.

To start docker container and specify keys:

docker run -e AWS_ACCESS_KEY_ID=AKIxxxxxxxxxxxxxx -e AWS_SECRET_ACCESS_KEY=K3Oxxxxxxxxxxxxxxxxxxxx -it getting-started /bin/bash

Then use the .NET environment variable cred provider. See:

https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/creds-assign.html

smac2020
  • 9,637
  • 4
  • 24
  • 38
  • Thanks, I'm starting the docker from my Visual Studio, Do I need to do It differently? and can I run with profileName only? how do I add the env vars? – Yaron May 30 '23 at 11:19