Integrating MuleSoft with AWS Dynamo DB

To know more on DynamoDB and how to run it locally, check the below post

Running AWS DynamoDB in your local workspace
How to run AWS Dynamo DB on local workspace?

Before we start integrating DynamoDB with Mule, let's understand the use case we are solving.

We want to save and retrieve book details to DynamoDB using Mule. So, let's get started!

  • Create a AWS DynamoDB Table called books

We will use book_id as our primary key.

  • Create an IAM User with DynamoDB Full Access

  • Add DynamoDB Connector
  • Create an AWS DynamoDB Connector Configuration
  • Create Book Flow

This private flow creates data in DynamoDB

  1. The Book Id is set to UUID

2. We then need to transform the input payload to something that Dynamo DB can recognize

%dw 2.0
output application/json
	book_id: {
		S: vars.bookId
	book_name: {
		S: payload.bookName
	book_price: {
		S: payload.bookPrice
	book_author: {
		S: payload.bookAuthor
		l: payload.bookCategories map ((item, index) ->{
			S: item
		bool: payload.isAvaiable

Here S represents String, l represents List and bool represents Boolean.

So, we need to transform incoming payload to the above structure to make it DynamoDB specific payload.

3. We use the Configuration created earlier and use the payload created above to insert data.

4. Finally we can return the UUID to client for reference in the last Transform Message.

%dw 2.0
output application/json
	status: "Success",
	message: "Data inserted successfully",
	bookId: vars.bookId
  • Fetch Book Flow
  1. This private flow takes book_id and return the book details from Table

We use Get item operation to fetch the details.

We pass id from query parameters as a string input and fetch the details.

2. The final transformation is then created to transform DynamoDB specific payload to user readable payload

%dw 2.0
output application/json
	bookName: payload.item.book_name.s,
	bookPrice: payload.item.book_price.s,
	bookAuthor: payload.item.book_author.s,
	bookCategories: payload.item.book_categories.l map ((item, position) -> 
	isAvailbale: payload.item.is_book_available.bool

Let's try this now!

We pass input payload for Book with details and it responds us with a bookId

We then use the given bookId to fetch book details

Hope you liked it. Cheers✔

Tirthankar Kundu

Tirthankar Kundu