Integrating MuleSoft with AWS Dynamo DB

Integrating MuleSoft with AWS Dynamo DB

To know more about 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!


THE AWS HOUSE
  • Create an AWS DynamoDB Table called books

We will use book_id as our primary key.

  • Create an IAM User with DynamoDB Full Access

THE MULE HOUSE
  • 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
	},
	book_categories:{
		l: payload.bookCategories map ((item, index) ->{
			S: item
		})
	},
	is_book_available:{
		bool: payload.isAvaiable
	}
}

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

So, we need to transform the incoming payload to the above structure to make it a 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 the 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 returns the book details from Table

We use the Get-Item operation to fetch the details.

We pass the 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) -> 
		(item.s)
	),
	isAvailbale: payload.item.is_book_available.bool
}

Let's try this now!

We pass input payload for Book with details and it responds to us with a book ID

We then use the given bookId to fetch book details


Hope you like it. Cheers✔