1

This might be a very silly question when it comes to dynamo DB.

Well,to summarize the issue I have a dynamoDB set up in my project.
(FYI, I have not setup a local dynamodb, I'm trying to connect to my remote db in lab environment)
The configuration for which is as follows:

AmazonDynamoDBClientBuilder amazonDynamoDB = AmazonDynamoDBClientBuilder.standard();
    if (DYNAMO_DB_MODE_REMOTE.equals("remote")) {
        amazonDynamoDB.setRegion(amazonAWSRegion);
        amazonDynamoDB.setCredentials(new InstanceProfileCredentialsProvider(false));
    } else {
        amazonDynamoDB.setEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(amazonDynamoDBEndpoint, amazonAWSRegion));
        amazonDynamoDB.setCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(amazonAWSAccessKey, amazonAWSSecretKey)));
    }

The issue that I encountered with the above piece of code is as follows:

    com.amazonaws.SdkClientException: Failed to connect to service endpoint:
        at com.amazonaws.internal.EC2ResourceFetcher.doReadResource(EC2ResourceFetcher.java:100)
        at com.amazonaws.internal.InstanceMetadataServiceResourceFetcher.getToken(InstanceMetadataServiceResourceFetcher.java:91)
        at com.amazonaws.internal.InstanceMetadataServiceResourceFetcher.readResource(InstanceMetadataServiceResourceFetcher.java:69)
        at com.amazonaws.internal.EC2ResourceFetcher.readResource(EC2ResourceFetcher.java:66)
        at com.amazonaws.auth.InstanceMetadataServiceCredentialsFetcher.getCredentialsEndpoint(InstanceMetadataServiceCredentialsFetcher.java:58)
        at com.amazonaws.auth.InstanceMetadataServiceCredentialsFetcher.getCredentialsResponse(InstanceMetadataServiceCredentialsFetcher.java:46)
        at com.amazonaws.auth.BaseCredentialsFetcher.fetchCredentials(BaseCredentialsFetcher.java:112)
        at com.amazonaws.auth.BaseCredentialsFetcher.getCredentials(BaseCredentialsFetcher.java:68)
        at com.amazonaws.auth.InstanceProfileCredentialsProvider.getCredentials(InstanceProfileCredentialsProvider.java:166)
        at com.amazonaws.http.AmazonHttpClient$RequestExecutor.getCredentialsFromContext(AmazonHttpClient.java:1251)
        at com.amazonaws.http.AmazonHttpClient$RequestExecutor.runBeforeRequestHandlers(AmazonHttpClient.java:827)
        at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:777)
        at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:764)
        at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:738)
        at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:698)
        at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:680)
        at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:544)
        at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:524)
        at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.doInvoke(AmazonDynamoDBClient.java:5110)
        at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:5077)
        at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.executeUpdateItem(AmazonDynamoDBClient.java:4696)
        at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.updateItem(AmazonDynamoDBClient.java:4662)
        at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper$SaveObjectHandler.doUpdateItem(DynamoDBMapper.java:870)
        at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper$2.executeLowLevelRequest(DynamoDBMapper.java:610)
        at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper$SaveObjectHandler.execute(DynamoDBMapper.java:749)
        at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.save(DynamoDBMapper.java:639)
        at com.amazonaws.services.dynamodbv2.datamodeling.AbstractDynamoDBMapper.save(AbstractDynamoDBMapper.java:123)
        .
        .
        .
        .
        .
        .
Caused by: java.net.SocketException: Network is unreachable: connect
        at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
        at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:589)
        at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
        at sun.net.www.http.HttpClient.<init>(HttpClient.java:242)
        at sun.net.www.http.HttpClient.New(HttpClient.java:339)
        at sun.net.www.http.HttpClient.New(HttpClient.java:357)
        at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1202)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1181)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1032)
        at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:966)
        at com.amazonaws.internal.ConnectionUtils.connectToEndpoint(ConnectionUtils.java:52)
        at com.amazonaws.internal.EC2ResourceFetcher.doReadResource(EC2ResourceFetcher.java:80)
        ... 66 common frames omitted

By trial and error, I made one minor change i.e. remove the line

amazonDynamoDB.setCredentials(new InstanceProfileCredentialsProvider(false));

from the DynamoDB config and that, somehow, solved the issue!

However I don't understand what was wrong in the first place.
Could someone help me out? Thanks in dvance!

Alexander
  • 1,232
  • 1
  • 15
  • 24
th2797
  • 13
  • 5

1 Answers1

2

The problem is that your local machine doesn't have an instance profile.

If you're looking to consume AWS services from your local machine, you will have to use a credentials provider that retrieves the information locally. I recommend using DefaultAWSCredentialsProviderChain, which looks for local configuration (either in $HOME/.aws or via environment variables or system properties), and also supports instance profiles (in case you're running on an EC2 instance). That's why removing amazonDynamoDB.setCredentials(new InstanceProfileCredentialsProvider(false)); worked

SAIJAL
  • 162
  • 9
  • Thanks for the answer! I'd like to add that I do have my .credentials file setup locally (with aws_access_key_id, aws_security_token etc). Also, I did not explicitly set a credentials provider(after removing InstanceProfileCredentialsProvider) and I was still able to push data into my db. Is this because if we do not explicitly specify a CredentialsProvider, DefaultAWSCredentialsProvider(which read the .credentials file that I mentioned earlier) is automatically considered which is why I was able to connect to remote db from my local machine? – th2797 Jun 09 '21 at 12:37
  • Yes. To access DynamoDB programmatically or through the AWS Command Line Interface (AWS CLI), all you need is to configure your credentials (access_key and secret_key) in the .credentials file. If no specific profile is mentioned, AWS authorizes applications from credentials mentioned in the .credentials file by default. Hence you were able to push the data. – SAIJAL Jun 10 '21 at 06:27
  • If this answer helped you, please mark it as accepted. Cheers! – SAIJAL Jun 10 '21 at 06:28