📱 AWS CDK 101 - 👯 Fetching JSON from S3 through stepfunction
Tags: typescript, aws, serverless, cdk
Published: 2022-05-04 04:46:28

: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-using-layers-in-lambda-functions-and-saving-json-to-s3)

:repeat: Reposted previous post at :link: [dev to @aravindvcyber](https://dev.to/aravindvcyber/aws-cdk-101-using-layers-in-lambda-functions-and-saving-json-to-s3-46fg)

In this article, let us refactor one of our previous step functions which invoked lambda which puts records to dynamo using full message from the event data, into one which can read message data from s3 with keys during the event data. Also, this is a direct follow-up to my previous article mentioned above.

## Benefits of using S3 data :hourglass:

These are not the only benefits, it is just only what I just observed in my deployments.

* Using S3 data directly will make the event detail payload always of the same payload size and very light

* While a huge message is pumped into the api gateway S3 will act as the place to capture the full message payload and only the s3 object identifier is pushed into the event detail so that we are not pushing a lot of into the actual moving parts.

* This also helps to simply preprocess the actual message received by checking for the format, data integrity, transformation, and even antivirus scanning.

* S3 in itself can trigger various event-driven actions asynchronously by its event notifications.

* Pumping the event details into the step functions will be now much more efficient as we are no longer worried about the size of the data.

* Also here we have using S3 as staging, if we need high performance and high throughput we can change it to dynamodb for storing this staging data.

## Construction Plan :building_construction:

As we mentioned earlier we are trying to read a JSON from s3, from our message recorder lambda function which we have refined in our previous articles for writing to dynamodb.

```ts
import { S3 } from "aws-sdk";

const s3 = new S3();
```

## Refactoring put operation inside the lambda into a helper function  :confetti_ball:

We have refactored the previously used message recorder function for better reuse of the components as shown below.

```ts

const dbPut: any = async (Record: any, msg: any) => {
  const dynamo = new DynamoDB();
  const crt_time: number = new Date(msg.createdAt).getTime();
  const putData: PutItemInput = {
    TableName: process.env.MESSAGES_TABLE_NAME || "",
    Item: {
      messageId: { S: msg.messageId },
      createdAt: { N: `${crt_time}` },
      event: { S: msg.event },
    },
    ReturnConsumedCapacity: "TOTAL",
  };

  console.log("putData", JSON.stringify(putData, undefined, 2));

  await dynamo.putItem(putData).promise();
};

```

## Refactoring the sendSuccess Helper :crystal_ball:

We will also make use of this opportunity to refactor the existing SendTaskSuccess statement into a dedicated function that we could efficiently.

```ts

const funcSuccess: any = (res: any, mid: string, token: string) => {
  console.log("sending success ", { res });
  const sendSuccess: StepFunctions.SendTaskSuccessInput = {
    output: JSON.stringify({
      statusCode: 200,
      headers: { "Content-Type": "text/json" },
      putStatus: {
        messageId: mid,
        ProcessorResult: res,
      },
    }),
    taskToken: token,
  };
  const resultStatus = sfn.sendTaskSuccess(
    sendSuccess,
    (err: any, data: any) => {
      if (err) console.log(err, err.stack);
      else console.log(data);
    }
  );
  console.log("sent success: ", { resultStatus, sendSuccess });
};
```

## Refractoring the sendFailure Helper  :surfer:

Similarly, we will also refactor and create sendTaskFailure module into a new helper function.

```ts
const funcFailure: any = (err: any, mid: string, token: string) => {
  console.log("sending failure ", { err });
  const sendFailure: StepFunctions.SendTaskFailureInput = {
    error: JSON.stringify(err),
    cause: JSON.stringify({
      statusCode: 500,
      headers: { "Content-Type": "text/json" },
      putStatus: {
        messageId: mid,
        ProcessorResult: err,
      },
    }),
    taskToken: token,
  };
  const resultStatus = sfn.sendTaskFailure(
    sendFailure,
    (err: any, data: any) => {
      if (err) console.log(err, err.stack);
      else console.log(data);
    }
  );
  console.log("sent failure: ", { resultStatus, sendFailure });
};
```
## Granting read Object access to lambda :leaves:

In our previous article we have granted access to the entry handler to putObject, here we will grant access to the recorder function to read data as follows.

```ts

stgMsgBucket.grantWrite(eventCounterBus.handler);
stgMsgBucket.grantRead(messageRecorder);

```


![S3 read access](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i9lnlklwovw1p5xpqtx5.png)

## Changes inside the lambda :banana:

The below changes will be used inside the lambda handler to read every message using the `event.detail`
content which now has the bucket name and the key from the event.

Here can find that the bucket name and object key is extracted from the message and used to retrieve an object from S3, then it is put into dynamodb for recording using the various helper functions created above.

```ts
await Promise.all(
    event.Records.map(async (Record: any) => {
      console.log("Received message:", JSON.stringify(event, undefined, 2));
      const msg = JSON.parse(Record.body).Record;
      const s3Get = await s3
        .getObject({
          Bucket: msg.bucket,
          Key: msg.key,
        })
        .promise();
      const data = s3Get.Body?.toString("utf-8");
      if (data) {
        msg.event = data;
        const token = JSON.parse(Record.body).MyTaskToken;
        await dbPut(Record, msg)
          .then(async (data: any) => {
            await funcSuccess(data, msg.messageId, token);
          })
          .catch(async (err: any) => {
            await funcFailure(err, msg.messageId, token);
          });
      }
    })
  );
```

Once we get the message, we are updating the message object with the message content and inserting it into dynamodb with the `dbPut` we have refracted earlier.

## Removing the event content from statemachine invocation payload :lollipop:

Now we can remove the usage of the actual message body across the entire pipeline, which makes it reduce the storage used in transmission call during various invocations call.

```ts
const sfnTaskPayload = sfn.TaskInput.fromObject({
      MyTaskToken: sfn.JsonPath.taskToken,
      Record: {
        "messageId.$": "$.id",
        "createdAt.$": "$.time",
        // "event.$": "States.StringToJson($.detail.message)",
       // "event.$": "$.detail.message",
        "bucket.$": "$.detail.message.bucket",
        "key.$": "$.detail.message.key"
      },
    });

```

Also in the entry handler function discussed in the last article as shown below

```ts
   const message = JSON.parse(event.body);
  message.uuid = getUuid();
  message.handler = context.awsRequestId;
  message.key = `uploads/${message.uuid}.json`;
  message.bucket = process.env.BucketName || "";
  console.log("Initial request:", JSON.stringify(message, undefined, 2));
  delete message.message;  //new line added, since s3 will have the data
```

## Integration testing :maple_leaf:

![New message](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hbkeouaz2pvgj79copoa.png)
 
![sfn instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pfpllatp83spq1ns8h14.png)
 
![s3 data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pfp0gyddim9k4mvhxo5s.png)

![Final destination](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i6cjecmumoeuof23v40k.png)

## Keypoint async-await :bell:

One important thing you may have to learn here would be how we have handled the async-await, to fetch from S3 first and then write to dynamodb, among other async operations.

Beginners most likely may go wrong in using async-await and they may mesh up by getting execution leaks when they implemented callbacks poorly.

By then the trace below can help you understand where the problem is present.

![trace](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4mojrzuhmc4ubqt7lg80.png)

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-fetching-json-from-dynamodb-vs-s3-through-stepfunction-1mb7

🎉 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/kofi3.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-s3-through-stepfunction)

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

<blockquote class="twitter-tweet"><p lang="en" dir="ltr">🚁 AWS CDK 101 - 🐬 Fetching JSON from dynamodb vs S3 through stepfunction <a href="https://twitter.com/hashnode?ref_src=twsrc%5Etfw">@hashnode</a><br><br>Checkout the full collection<a href="https://t.co/CuYxnKr0Ig">https://t.co/CuYxnKr0Ig</a><br><br>%[<a href="https://t.co/wBjjaPapaN">https://t.co/wBjjaPapaN</a>]<a href="https://twitter.com/hashtag/TheHashnodeWriteathon?src=hash&amp;ref_src=twsrc%5Etfw">#TheHashnodeWriteathon</a><a href="https://twitter.com/hashtag/serverless?src=hash&amp;ref_src=twsrc%5Etfw">#serverless</a> <a href="https://twitter.com/hashtag/awscdk?src=hash&amp;ref_src=twsrc%5Etfw">#awscdk</a> <a href="https://twitter.com/hashtag/dynamodb?src=hash&amp;ref_src=twsrc%5Etfw">#dynamodb</a> <a href="https://twitter.com/hashtag/amazons3?src=hash&amp;ref_src=twsrc%5Etfw">#amazons3</a> <a href="https://twitter.com/hashtag/typescript?src=hash&amp;ref_src=twsrc%5Etfw">#typescript</a></p>&mdash; Aravind V (@Aravind_V7) <a href="https://twitter.com/Aravind_V7/status/1523332976494661632?ref_src=twsrc%5Etfw">May 8, 2022</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>