0

I have created AWS Java Lambda project from ECLIPSE IDE. In the handle request section I want to process the request and insert it into a AWS DynamoDB table. In

I can see this can be easily done using node.js. Lot of code samples are available. Is there a proper JAVA help available. I am new to JAVA and struggling to find this. Any help is appreciated.

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class LambdaHandler implements RequestHandler<Object, Object> {
    @Override
    public Object handleRequest(Object input, Context context) {
        context.getLogger().log("Input: " + input);
        // TODO: implement DYNAMO DB INSERT
        return input;
    }

}

1 Answer 1

1

Use the api in package com.amazonaws.services.dynamodbv2 (maven dependency artifactID= aws-java-sdk-dynamodb )

AWSCredentials creds = new BasicAWSCredentials("myacceskey","mysecretkey");
AmazonDynamoDBClient dyndbclient = new AmazonDynamoDBClient(creds);
String tableName = "myDynamoDbTable"
Map<String, AttributeValue> dbItem = new HashMap<String, AttributeValue>();
//TODO put key/values from request in dbItem
dyndbclient.putItem(tableName, dbItem);

You may also want to redefine your lambda handler function, so that you have a better cast input parameter than your current (Object input )

Sign up to request clarification or add additional context in comments.

6 Comments

Assuming he is using dynamodb in the same account as in lambda function, he can ignore the credentials while creating AmazonDynamoDBClient object. AmazonDynamoDBClient dynamoDBClient = new AmazonDynamoDBClient(); Just providing enough permissions for operations on DynamoDB in Lambda function role will suffice.
As I said I am new to JAVA. I configured the project using ECLIPSE IDE.The "import com.amazonaws.services.dynamodbv2 cannot be resolved" is the error ECLIPSE is showing when I tried to include this.
Try adding the following maven dependency to pom.xml (in the root of your project) : <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-dynamodb</artifactId> <version>LATEST</version> </dependency>
Not able to find any pom.xml file in my root directory. Everything works fine when I create JAVA Project. Issue starts when I create JAVA LAMDA Projecr
you can download the jar file dependency here ( central.maven.org/maven2/com/amazonaws/aws-java-sdk-dynamodb/…) Than, add it to your eclipse project: wiki.eclipse.org/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.