Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamo DB Local - Connection Refused

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)
like image 915
AgentX Avatar asked Nov 19 '15 10:11

AgentX


2 Answers

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/

like image 167
CodeUrLife Avatar answered Dec 18 '22 14:12

CodeUrLife


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
like image 32
best wishes Avatar answered Dec 18 '22 12:12

best wishes