AWS .NET SDK How to specify region when using Localstack
Asked Answered
E

2

5

How do I instantiate a .NET AWSSDK SQS client that has both a custom Region and ServiceURL?


More information:

I have a queue set up in localstack that I can verify is there through an sqs list-queues query using the CLI:

> aws --endpoint-url=http://localhost:4566 --region=ap-southeast-2 sqs list-queues

{
    "QueueUrls": [
        "http://localhost:4566/000000000000/question-renderer-requests-errors",
        "http://localhost:4566/000000000000/question-renderer-requests"
    ]
}

The queues are in region ap-southeast-2, however when I try to access the queues through the SDK, it can't find anything:

var cred = new BasicAWSCredentials("test", "test");

var sqsClientConfig = new AmazonSQSConfig()
{
    RegionEndpoint = RegionEndpoint.APSoutheast2,
    ServiceURL = "http://localhost:4566"
};

var sqsClient = new AmazonSQSClient(cred, sqsClientConfig);

var queues = await sqsClient.ListQueuesAsync(new ListQueuesRequest());

I discovered that RegionEndpoint and ServiceURL are mutually exclusive:

RegionEndpoint and ServiceURL are mutually exclusive properties. Whichever property is set last will cause the other to automatically be reset to null.

I need the service endpoint set to http://localhost:4566 to point to localstack, how do I also set the region endpoint? Am I going the right way about this?

Update: I googled for the default region which is us-east-1, and when I put the queue in there the SDK managed to find it - so it must be a region configuring issue.

Ent answered 9/3, 2022 at 2:6 Comment(0)
E
14

Use AuthenticationRegion to specify region (part of IClientConfig)


var sqsClientConfig = new AmazonSQSConfig()
{
    RegionEndpoint = RegionEndpoint.APSoutheast2,
    ServiceURL = "http://localhost:4566"
};

Becomes:

var sqsClientConfig = new AmazonSQSConfig()
{
    AuthenticationRegion = "ap-southeast-2",
    ServiceURL = "http://localhost:4566"
};
Ent answered 18/4, 2022 at 1:15 Comment(1)
Tks a lot. You save my day!!Ulbricht
F
0

For future readers who stumble upon this question and are looking for a simplified way to integrate .NET applications with LocalStack:

Check out LocalStack.NET. It's a thin wrapper around the AWS SDK.NET designed to automatically configure the target endpoints to use LocalStack. This means you don't have to set the ServiceURL or worry about the region manually.

With LocalStack.NET, your configuration would look something like this:

public void ConfigureServices(IServiceCollection services)
{
    services.AddLocalStack(Configuration);
    services.AddDefaultAWSOptions(Configuration.GetAWSOptions());
    services.AddAwsService<IAmazonSQS>();
}

For more details on how to use LocalStack.NET, you can refer to the Getting Started documentation.

I hope this helps future readers looking to integrate .NET with LocalStack!

Forgetful answered 27/9, 2023 at 11:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.