Skip to main content

Adobe

Integration of DynamoDB with AEM

Business Team Brainstorming Data Target Financial Cocnept

DynamoDB, offered by Amazon Web Services, is a NoSQL database that provides both key-value and document data models. It supports tables of any size with horizontal scaling (Expanding your infrastructure by incorporating extra nodes or machines to accommodate increased demands.). It empowers developers to create serverless applications that have the flexibility to begin on a small scale and expand globally to handle massive amounts of data, along with tens of millions of requests per second. DynamoDB is specifically designed to serve high-performance, internet-scale applications.

To connect DynamoDB with AEM we need to create an IAM (Identity and Access Management) user with the required permissions. IAM user can be created with the help of a Root user which is an admin account and once the IAM user is created generate access and secret keys which are required to establish a connection with the AEM.           

Steps to Create an IAM user, Generate its Access and Secret Keys and Enable Console Login.

1: Log in to the AWS console using Root user and go to “Security Credentials” available under username. 

2: Under the security credential console locate “Access Management” and under that find “Users” and click on “Add users”.

3: Provide an IAM username, and allow required policies to the user, next step is to review and create an IAM user.4: Once the IAM user is created click the “Security Credentials” tab to enable console access. Under the Console Sign-in section click on “Enable console access”.

Allow console access and generate a custom password for your user and click on “Apply”.

And use the “Console sign-in link” whenever you want to log in with an IAM user.


5: Under the same “Security Credentials” tab scroll down to the “Access Keys” section and click on “Create access key”. Select “Command Line Interface(CLI)”, check the confirmation checkbox at the bottom and click on “Next”. Now click on “Create Access Key”. Download the .csv file which has your access and secret key and click on “Done”.   

Add the following Dependency to the pom.xml of the Core Module.

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

Create an OSGI Configuration to Read the Access and Secret Keys of the IAM User.

Create a POJO and annotate it with @DynamoDBTable annotation, this annotation requires a table name from DynamoDB so that it can identify the target table. To map the property with the partition key which is a unique key, use the @DynamoDBHashKey annotation and provide the name of the partition key from DynamoDB. To access other properties of the DynamoDB table use @DynamoDBAttribute, this requires the name of the attribute to which we want to map our property. Refer to the following code snippet.

@Getter
@Setter
@DynamoDBTable(tableName = "MyTable")
public class MyTable {

    @DynamoDBHashKey(attributeName = "userId")
    private String userId;

    @DynamoDBAttribute(attributeName = "name")
    private String name;

}

The next step is to create a DynamoDB mapper which helps us to perform CRUD operations on tables. To create a mapper object, we need access and the secret key of the IAM user and the code of the selected region. The following screenshot shows where we can set the appropriate region, and view the code associated with it.

Use the Following Code Snippet to Create DynamoDB Mapper. 

/*DynamoDBServiceConfig is the OSGI configuration from
where we are going to read the access and secret keys.*/
@Reference
private transient DynamoDBServiceConfig dynamoDBServiceConfig;

private DynamoDBMapper getDynamoDBMapper() {
        BasicAWSCredentials awsCredentials = new BasicAWSCredentials(dynamoDBServiceConfig.getAccessKey(),
        dynamoDBServiceConfig.getSecretKey());
        AWSCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
        AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
        .withCredentials(credentialsProvider)
        .withRegion(Regions.AP_SOUTH_1.getName())
        .build();
        DynamoDBMapper dynamoDBMapper = new DynamoDBMapper(client);
        return dynamoDBMapper;
}


public MyTable createUserByID(String userID) throws DynamoDBExceptionMessage {
        try {
              DynamoDBMapper dynamoDBMapper = getDynamoDBMapper();
              //MyTable is the POJO created in above step
              MyTable myTable = new MyTable();
              myTable.setUserId(userID);
              //dynamoDBMapper.save() is used to create new entry in the DynamoBD table
              dynamoDBMapper.save(myTable); 
              return myTable;
        } catch (AmazonServiceException e) {
              throw new DynamoDBExceptionMessage(e.getMessage(), e.getStatusCode(), e.getErrorCode(), e.getErrorCode(),
                e.getRequestId());
   }
}

By following all the above steps, you can connect your AEM application with DynamoDB. Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Ashwini Bhombe

Ashwini Bhombe is Senior Technical Consultant at Perficient. Ashwini has 4.7 years of experience in AEM.

More from this Author

Categories
Follow Us
TwitterLinkedinFacebookYoutubeInstagram