🚁 AWS CDK 101 - 🐬 Fetching JSON from dynamodb vs S3 through stepfunction
Tags: serverless, cdk, dynamodb, s3
Published: 2022-05-08 15:44:06

:beginner: Beginners new to AWS CDK, please do look at my previous articles one by one in this series.

If in case missed my previous article, do find it with the below links.

:repeat: Original previous post at :link: [Dev Post](https://devpost.hashnode.dev/aws-cdk-101-fetching-json-from-s3-through-stepfunction)

:repeat: Reposted previous post at :link: [dev to @aravindvcyber](https://dev.to/aravindvcyber/aws-cdk-101-fetching-json-from-s3-through-stepfunction-15ob)

In this article, let us refactor our previous step functions which were using S3-based storage to retrieve the JSON data for processing into a more performant dynamodb-based one. Both have their advantages compared to another one, so we have to choose them wisely based on the expected turnaround.

## Visualizing this scenario with S3 :sailboat:

![s3 bucket](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lx1y1g41oeyhr9qxpbn3.png)

## Visualizing this scenario with dynamodb  :rocket:

![dynamodb](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8nypba6gb81rjjdttghr.png)


## Benefits of fetching JSON from dynamodb vs S3 :basketball:

* In simple words, the first benefit of using dynamodb in place of S3 is a reduction in time taken for the I/O operation.
* S3 takes more time than the similar dynamodb operation.
* Though dynamodb is costlier compared to the s3 storage, when we use have a good archival setup this can be taken to our advantage.
* Further S3 operation is an external request which has overhead like DNS lookup and httpAgent creation.
* While dynamodb is treated as a database transaction and is faster.
* Moreover you can move your S3 metadata into fields in dynamodb for easier manipulation.
* This will also help with an opportunity to query and extract only the required field in each step of processing without downloading the full object contents as in S3.
* Also S3 will have better throughput when we have more prefixes, which can change the key path. Whereas in dynamodb we have good leverage over the throughput by making use of the provisioned and pay per request model.
* We can also fine-tune our read and write capacity based on the requests which could be throttled for efficient cost management and reliability with full control of the reservation.
* And by the way why we wanted to reduce the I/O time, this is because we always trigger this from a compute resource which is capacity and runtime metered, which will be waiting for longer idle.
* Data is dynamodb, can we efficiently archive like S3 with various service integration, besides the special TTL property we have with dynamodb, which can be used to delete the temporary objects like in our case.

## Final results from analysis :factory:

I tried to trace this using x-ray and newrelic traces and find the below observations.

And do understand there could be some difference since both the traces from x-ray and newrelic track the timings differently.

Yet the point to note here would be that both convey that S3 could be a bit slower compared to dynamodb when we have high throughput transactions without any fine-tuning in the resource specifications.

### Some trace results from xray for putObject  :fountain:

![dynamodb put](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/76vzf20lnp6dalyb39e5.png)

#### In-depth trace from newrelic for putItem :carousel_horse:

![newrelic put](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tuffbgr12y6y98pko8wf.png)

#### Some trace results from xray for getObject :boat:

![dynamodb get](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7bjlznwlr5l8yhcuj830.png)

#### In-depth trace from newrelic for getItem :tent:

![newrelic get](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/shdgvrewdlf1al9x31wy.png)

## Construction :ship:

Here let us start by creating a new table as shown below in our main stack.

```ts
const stgMessages = new dynamodb.Table(this, "stgMessagesTable", {
      tableName: process.env.stgMessagesTable,
      sortKey: { name: "createdAt", type: dynamodb.AttributeType.NUMBER },
      partitionKey: { name: "messageId", type: dynamodb.AttributeType.STRING },
      encryption: dynamodb.TableEncryption.AWS_MANAGED,
      readCapacity: 5,
      writeCapacity: 5,
});
```

And grant access to both the entry handler and the backend step-function indirectly invoked handler.

```ts

stgMessages.grantWriteData(eventCounterBus.handler);
stgMessages.grantReadData(messageRecorder);
```

![table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n6vwo5djc1gyjywu1par.png)
 
## Refactoring the message entry handler :circus_tent:

The existing message entry handler previously used to save to S3 will be now enhanced as shown below.
## Helper function putItem

```ts
const dbPut: any = async (msg: any) => {
  const dynamo = new DynamoDB();
  const crt_time: number = new Date(msg.Metadata?.timestamp).getTime();
  const putData: PutItemInput = {
    TableName: process.env.STAGING_MESSAGES_TABLE_NAME || "",
    Item: {
      messageId: { S: msg.Metadata?.UUID },
      createdAt: { N: `${crt_time}` },
      content: { S: JSON.stringify(msg) },
    },
    ReturnConsumedCapacity: "TOTAL",
  };
  await dynamo.putItem(putData).promise();
};
```

## Appending putItem once after putObject :trident:

Adding this as an additional set in the save helper function, so that we can use this as our successive step. This will help us understand the I/O timing information as we have demonstrated above.

```ts

const save = async (uploadParams: PutObjectRequest) => {
  let putResult: PromiseResult<PutObjectOutput, AWSError> | undefined =
    undefined;
  let putDBResult: PromiseResult<PutObjectOutput, AWSError> | undefined =
    undefined;
  try {
    putResult = await s3.putObject(uploadParams).promise();
    putDBResult = await dbPut(uploadParams);
  } catch (e) {
    console.log(e);
  } finally {
    console.log(putResult);
    console.log(putDBResult);
  }
  return putResult;
};
```
## Creating additional properties in the JSON :traffic_light:

In this step, we will add additional data into the JSON which will can help us in retrieving from the dynamodb.

```ts
message.uuid = getUuid();
message.handler = context.awsRequestId;
message.key = `uploads/${message.uuid}.json`;
message.bucket = process.env.BucketName || "";
message.table = process.env.STAGING_MESSAGES_TABLE_NAME || "";
message.timestamp = new Date().toUTCString();
```
## Changes to step-function payload :rocket:

The existing payload for the step function will now be enhanced with more property data as follows to extract the additional message coming from the event bridge detail post JSON parsing.

```ts
const sfnTaskPayload = sfn.TaskInput.fromObject({
   MyTaskToken: sfn.JsonPath.taskToken,
   Record: {
     "messageId.$": "$.detail.message.uuid",
     "createdAt.$": "$.detail.message.timestamp",
     "bucket.$": "$.detail.message.bucket",
     "key.$": "$.detail.message.key",
     "table.$": "$.detail.message.table",
     "uuid.$": "$.detail.message.uuid",
     "timestamp.$": "$.detail.message.timestamp"
   },
});

```
 ![sqs payload](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ocni6ucszoc0h79uqc28.png)
## Refactoring the message recorder handler :gift:

Once retrieving the data from S3 from the previous article from inside the message recorder function, let us now add the new step to fetch directly from dynamo ignoring the getObject as follows.

Once the `s3Get` call is completed, we have used the `dbGet` which will is mentioned in the trace shown above.

```ts
try {
  const crt_time: number = new Date(msg.timestamp).getTime();
  const getData: GetItemInput = {
    TableName: msg.table,
    Key: {
      messageId: { S: msg.uuid },
      createdAt: { N: `${crt_time}` },
    },
    ProjectionExpression: "messageId, createdAt, content",
    ConsistentRead: true,
    ReturnConsumedCapacity: "TOTAL",
  };
  const dbGet = await dynamo.getItem(getData).promise();
  data = JSON.parse(dbGet.Item.content.S).Body;

  //OLD code with S3
  if (data) {
    msg.event = data;
    const token = JSON.parse(Record.body).MyTaskToken;
    await dbPut(msg)
      .then(async (data: any) => {
        await funcSuccess(data, msg.messageId, token);
      })
      .catch(async (err: any) => {
        await funcFailure(err, msg.messageId, token);
      });
  }
} catch (err) {
  console.log(err);
}
```

![parti-ql](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jgpv60whiwv96o48aex7.png)

Thus we can add a new step to record to dynamodb and fetch from dynamo and performance our timing checks.

In the next article, we will completely ignore the direct S3 operations which are not used here and do that indirectly.

We will be adding more connections to our stack and making it more usable in the upcoming articles by creating new constructs, so do consider following and subscribing to my newsletter.

⏭ We have our next article in serverless, do check out

https://dev.to/aravindvcyber/aws-cdk-101-using-batched-dynamodb-stream-to-delete-item-on-another-dynamodb-table-4oag

🎉 Thanks for supporting! 🙏

Would be great if you like to [☕ Buy Me a Coffee](https://www.buymeacoffee.com/AravindVCyber), to help boost my efforts.

<a href="https://www.buymeacoffee.com/AravindVCyber"><img src="https://img.buymeacoffee.com/button-api/?text=Buy me a coffee&emoji=&slug=AravindVCyber&button_colour=BD5FFF&font_colour=ffffff&font_family=Cookie&outline_colour=000000&coffee_colour=FFDD00" /></a>

<a href='https://ko-fi.com/X8X0CITDJ' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://cdn.ko-fi.com/cdn/kofi4.png?v=3' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>

:repeat: Original post at :link: [Dev Post](https://devpost.hashnode.dev/aws-cdk-101-fetching-json-from-dynamodb-vs-s3-through-stepfunction)

:repeat: Reposted at :link: [dev to @aravindvcyber](https://dev.to/aravindvcyber/aws-cdk-101-fetching-json-from-s3-through-stepfunction-15ob)