AWS Lambda LocalStack – Cannot assign requested address: HttpRequestException

I found myself hitting the following exception whilst playing around with AWS Lambda and LocalStack when invoking a Lambda function from another Lambda function:

Cannot assign requested address: HttpRequestException
localstack_main  |    at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
localstack_main  |    at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken)
localstack_main  |    at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
localstack_main  |    at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
localstack_main  |    at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
localstack_main  |    at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
localstack_main  |    at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
localstack_main  |    at Amazon.Runtime.HttpWebRequestMessage.GetResponseAsync(CancellationToken cancellationToken)
localstack_main  |    at Amazon.Runtime.Internal.HttpHandler`1.InvokeAsync[T](IExecutionContext executionContext)
localstack_main  |    at Amazon.Runtime.Internal.Unmarshaller.InvokeAsync[T](IExecutionContext executionContext)
localstack_main  |    at Amazon.Runtime.Internal.ErrorHandler.InvokeAsync[T](IExecutionContext executionContext)
localstack_main  |    at Amazon.Runtime.Internal.ErrorHandler.InvokeAsync[T](IExecutionContext executionContext)
localstack_main  |    at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
localstack_main  |    at Amazon.Runtime.Internal.EndpointDiscoveryHandler.InvokeAsync[T](IExecutionContext executionContext)
localstack_main  |    at Amazon.Runtime.Internal.EndpointDiscoveryHandler.InvokeAsync[T](IExecutionContext executionContext)
localstack_main  |    at Amazon.Runtime.Internal.CredentialsRetriever.InvokeAsync[T](IExecutionContext executionContext)localstack_main  |    at Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext)
localstack_main  |    at Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext)
localstack_main  |    at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
localstack_main  |    at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
localstack_main  |    at Amazon.Runtime.Internal.ErrorCallbackHandler.InvokeAsync[T](IExecutionContext executionContext)localstack_main  |    at Amazon.Runtime.Internal.MetricsHandler.InvokeAsync[T](IExecutionContext executionContext)
localstack_main  |    at FanOutFunction.Function.FunctionHandler(Stream stream, ILambdaContext context) in C:\Git\MyApp\src\FanOutFunction\Function.cs:line 86

Lambdas in LocalStack run within Docker so the AWS endpoint should to be set to the LocalStack container which wasn’t the case for me. As the LocalStack edge port is typically exposed you can just target the docker host by using host.docker.internal

I am using the .NET LocalStack client so this looks like the following:

// appsettings.json
{
  "LocalStack": {
    "UseLocalStack": true,
    "Session": {
      ...
    },
    "Config": {
      "LocalStackHost": "host.docker.internal",
      "UseSsl": false,
      "UseLegacyPorts": false,
      "EdgePort": 4566
    }
  }
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.