Dynamo DB Local - Connection Refused

14,195

Solution 1

You need to trigger DynamoDB in command prompt..

Go the location where the Dynamodb cli is installed and run the following command

java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb

verify if it is running by http://localhost:8000/shell/

Solution 2

If you are using SAM-LOCAL or developing aws lambda function using eclipse this answer can be useful.

Short Answer assign an IP alias to loopback to reach out using a different address to 127.0.0.1, and update the endpoint to that address

From the command line run

ifconfig lo0 alias 172.16.123.1

And from lambda code instead of accessing http://localhost:8000/ use http://172.16.123.1:8000/

refer to this link for explanation.

Long Answer

In lambda function pom

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-dynamodb</artifactId>
    <version>1.11.313</version>
</dependency>

started dynomodb using

java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb

do IP alias

ifconfig lo0 alias 172.16.123.1

For accessing DynmoDb from Code do

DynamoDB dynamoDb = new DynamoDB(AmazonDynamoDBClientBuilder.standard().withCredentials(new EnvironmentVariableCredentialsProvider()).withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://172.16.123.1:8000/", "local")).build());

After you are done with testing lambda function do

ifconfig lo0 -alias 172.16.123.1

Solution 3

Just fought with the same issue - append a slash to the endpoint url solved it for me.

client.setEndpoint( "http://localhost:8000/");

Solution 4

If you are using SAM-LOCAL, you can't access localhost:8080 directly.

I've found 2 options:

OPTION 1

Instead of calling http://localhost:8080 , you must use your machine's IP http://192.168.xx.xxx:8000

OPTION 2

Create a network on DynamoDB Docker container. You can find different ways to do that here: connecting AWS SAM Local with dynamodb in docker

My favorite: https://stackoverflow.com/a/57309422/13035616

Share:
14,195

Related videos on Youtube

AgentX
Author by

AgentX

Computer programmer by choice &amp; profession!!

Updated on September 15, 2022

Comments

  • AgentX
    AgentX over 1 year

    I am trying to connect to Local Dynamo DB using the AWS Java SDK. So I installed the Local Dynamo DB and started the javascript shell. All works fine and the shell starts at the usual address http://localhost:8000/shell/

    Now when I try to access the Dynamo DB Instance via the AWS SDK things start to break.

    Here is my code:

    public class MyDynamoDB {
        private AmazonDynamoDBClient client;
    
        public MyDynamoDB() {
            client = new AmazonDynamoDBClient();
            client.setEndpoint("http://localhost:8000");
        }
    
        public void saveAndLoad() {
            DynamoDBMapperConfig config = new DynamoDBMapperConfig(new TableNameOverride("xyz"));
            DynamoDBMapper mapper = new DynamoDBMapper(client, config);
            Data data = new Data();
            ...
            mapper.save(data);
    
    
            //check if persisted
            Data d = mapper.load(Data.class, "Key");
            if (d != null) {
                System.out.println(" Found data: " + d.getStuff());
            } else {
                System.out.println("Data not found");
            }
        }
    }
    

    On running this I am getting the following stack trace

    Nov 19, 2015 4:00:47 PM com.amazonaws.http.AmazonHttpClient executeHelper
    INFO: Unable to execute HTTP request: Connection refused: connect
    java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:117)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:177)
    at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:304)