Reputation: 1462
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)
Upvotes: 6
Views: 21089
Reputation: 411
I recently face this problem. The solution for me was to change the dynamodb endpoint to: http://docker.for.mac.localhost:8000
new DocumentClient({
endpoint: "http://docker.for.mac.localhost:8000",
sslEnabled: false,
region: 'eu-central-1', // change to your region
accessKeyId: 'keyid123',
secretAccessKey: 'secret123',
})
Upvotes: 0
Reputation: 41
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
Upvotes: 4
Reputation: 6624
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
Upvotes: 7
Reputation: 411
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/
Upvotes: 16
Reputation: 51
Just fought with the same issue - append a slash to the endpoint url solved it for me.
client.setEndpoint( "http://localhost:8000/");
Upvotes: 5