DynamoDb SocketException: A socket operation was attempted to an unreachable network

Viewed 4728

I'm trying to connect to DynamoDb locally run using docker: docker run -p 8000:8000 dwmkerr/dynamodb -sharedDb

But I keep getting the error mentioned in the title.

I can validate that DynamoDb server is running and accessible.

I can access the url http://localhost:8000/shell/

I even tried using a nodejs client https://github.com/aaronshaf/dynamodb-admin and managed to create some tables.

However, it is only dotnet code that doesn't run !

class Program
{
    static void Main(string[] args)
    {
        var clientConfig = new AmazonDynamoDBConfig()
        {
            UseHttp = true,
            LogMetrics = true,
            LogResponse = true,
            DisableLogging = false,
            ServiceURL = "http://localhost:8000"
        };

        var dynamoClient = new AmazonDynamoDBClient(clientConfig);

        //AWSSDK
        var request = new CreateTableRequest
        {
            TableName = "Todo",
            KeySchema = new List<KeySchemaElement> { new KeySchemaElement("Id", KeyType.HASH), },
            AttributeDefinitions = new List<AttributeDefinition> { new AttributeDefinition("Id", ScalarAttributeType.N), },
            ProvisionedThroughput = new ProvisionedThroughput
            {
                ReadCapacityUnits = 10,
                WriteCapacityUnits = 5,
            }
        };
        dynamoClient.CreateTableAsync(request).Wait();
    }
}

Here is the full stack trace:

Unhandled Exception: System.AggregateException: One or more errors occurred. (A socket operation was attempted to an unreachable network) ---> System.Net.Http.HttpRequestException: A socket operation was attempted to an unreachable network ---> System.Net.Sockets.SocketException: A socket operation was attempted to an unreachable network
   at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask`1 creationTask)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
   at System.Net.Http.HttpClient.GetStringAsyncCore(Task`1 getTask)
   at Amazon.Runtime.Internal.Util.AsyncHelpers.<>c__DisplayClass1_1`1.<<RunSync>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Amazon.Runtime.Internal.Util.AsyncHelpers.ExclusiveSynchronizationContext.BeginMessageLoop() in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Internal\Util\_mobile\AsyncHelpers.cs:line 142
   at Amazon.Runtime.Internal.Util.AsyncHelpers.RunSync[T](Func`1 task) in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Internal\Util\_mobile\AsyncHelpers.cs:line 87
   at Amazon.Util.AWSSDKUtils.DownloadStringContent(Uri uri, TimeSpan timeout, IWebProxy proxy) in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\sdk\src\Core\Amazon.Util\AWSSDKUtils.cs:line 969
   at Amazon.Util.EC2InstanceMetadata.GetItems(String relativeOrAbsolutePath, Int32 tries, Boolean slurp) in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\sdk\src\Core\Amazon.Util\_bcl+coreclr\EC2InstanceMetadata.cs:line 513
   at Amazon.Util.EC2InstanceMetadata.get_IAMSecurityCredentials() in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\sdk\src\Core\Amazon.Util\_bcl+coreclr\EC2InstanceMetadata.cs:line 311
   at Amazon.Runtime.DefaultInstanceProfileAWSCredentials.FetchCredentials() in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Credentials\_bcl+coreclr\DefaultInstanceProfileAWSCredentials.cs:line 142
   at Amazon.Runtime.DefaultInstanceProfileAWSCredentials.GetCredentials() in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Credentials\_bcl+coreclr\DefaultInstanceProfileAWSCredentials.cs:line 88
   at Amazon.Runtime.DefaultInstanceProfileAWSCredentials.GetCredentialsAsync() in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Credentials\_bcl+coreclr\DefaultInstanceProfileAWSCredentials.cs:line 106
   at Amazon.Runtime.Internal.CredentialsRetriever.InvokeAsync[T](IExecutionContext executionContext) in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Pipeline\Handlers\CredentialsRetriever.cs:line 90
   at Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext) in E:\JenkinsWorkspaces\v3-trebuchet-release\AWSDotNetPublic\sdk\src\Core\Amazon.Runtime\Pipeline\RetryHandler\RetryHandler.cs:line 137
   at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.ErrorCallbackHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.MetricsHandler.InvokeAsync[T](IExecutionContext executionContext)
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at test_dynamodb.Program.Main(String[] args) in G:\Test Projects\test-dynamodb\test-dynamodb\Program.cs:line 39

Surprisingly, it runs well on Mac!

3 Answers

If you trace down the stack a ways, you'll see that this exception isn't due to the SDK's attempt to connect to DynamoDB, but rather to the EC2 Instance Metadata URL (http://169.254.169.254). The DynamoDB client is trying to retrieve an IAM role from EC2, and when run outside of EC2, is unable to do so.

I'm seeing this exception crop up from AWSSDK.Core version 3.3.24.6 on, rather than the much more meaningful exception that used to be thrown:

Amazon.Runtime.AmazonServiceException : Failed to retrieve credentials from EC2 Instance Metadata Service.

The root cause of your issue, however, is that you haven't made AWS credentials available to the SDK. Add a default profile in AWS Explorer for Visual Studio, or create a credentials file, and you should be up and running.

I had this problem and it took me a while to track it down.

It is caused by not having correct credentials in your AWS credentials file.

In my case I had overwritten the [default] credentials and the SDK could not authenticate properly.

Once I had fixed this it started to work.

I am using the AWSSDK.Lambda NuGet package version 3.3.102.46.

Instead of creating an AWS profile, you can simply specify random keys:

var client = new AmazonDynamoDBClient(
    awsAccessKeyId: "test",
    awsSecretAccessKey: "test",
    clientConfig);

This bypassed the profile check, and the access key values are not used.

Related