Sk Shahnawaz-ul Haque
Sk Shahnawaz-ul Haque

Reputation: 588

AmazonClientException: No RegionEndpoint or ServiceURL configured

I have a .NET 5 worker/background service which connects with AWS SQS and does its work. In local environment, because AWS CLI is installed, it runs without any issues. While if I dockerize the service and try to run the container, it throws the error: No RegionEndpoint or ServiceURL configured. I do know that the reason of this error is that in the docker image itself, the AWS CLI is not available and also the AWS CLI specific directory is missing. My question is how can I achieve that?

Following is my current docker file definition:

FROM mcr.microsoft.com/dotnet/runtime:5.0

WORKDIR BackendRouterApp/

COPY deploy/Release/BackendRouter .

ENTRYPOINT ["dotnet","BackendRouterService.dll"]

Upvotes: 2

Views: 1824

Answers (2)

Sk Shahnawaz-ul Haque
Sk Shahnawaz-ul Haque

Reputation: 588

Solved the problem with the following code snippet in .NET Core service registration:

var awsOptions = configuration.GetAWSOptions();
if (awsOptions?.DefaultClientConfig?.RegionEndpoint == null)
{
    /* When running as dockerized container, AWS configurations will be missing, so setting AWS region to US-EAST-1 (N. Virginia) */
    services.AddAWSService<IAmazonSQS>(new Amazon.Extensions.NETCore.Setup.AWSOptions
    {
        Region = Amazon.RegionEndpoint.USEast1
    });
}
else
{
    /* When run through Visual Studio / VS Code.. AWS profile supplied by environment variable AWS_PROFILE
     , else when run through AWS Vault, profile is supplied as command line argument. e.g.: aws-vault exec <PROFILE> -- dotnet run
    */
    services.AddAWSService<IAmazonSQS>();
}

Upvotes: 0

Nick
Nick

Reputation: 1273

I have asked where do you run the container, but based on the available information and the fact that you are mentioning local environment I'm assuming that you do it on a dev machine.

That being said, one option is to pass the region as an env variable when you start the container:

docker run -e AWS_DEFAULT_REGION="eu-west-2"

or directly set it in your Dockerfile during build with ARG or ENV (ARG is not persistent and won't be present if you start a container from the resulting image)

FROM mcr.microsoft.com/dotnet/runtime:5.0

ARG AWS_DEFAULT_REGION

WORKDIR BackendRouterApp/

COPY deploy/Release/BackendRouter .

ENTRYPOINT ["dotnet","BackendRouterService.dll"]

and then: $ docker build --build-arg AWS_DEFAULT_REGION="eu-west-2"

with ENV:

FROM mcr.microsoft.com/dotnet/runtime:5.0

ARG AWS_DEFAULT_REGION
# use the value to set the ENV var default
ENV AWS_DEFAULT_REGION=$AWS_DEFAULT_REGION

WORKDIR BackendRouterApp/

COPY deploy/Release/BackendRouter .

ENTRYPOINT ["dotnet","BackendRouterService.dll"]

In addition, you can pass an env_var file with more than one environment variable if needed.

I would assume that you'll need to pass the AWS credentials from your profile too in order to access SQS where you can use the same approach or better yet pass the local environment variables to the container on runtime in order to not hard ode them in your code. The answers in this SO question have some great tips in that regard: What is the best way to pass AWS credentials to a Docker container?

Upvotes: 2

Related Questions