Nothing happens when a CreateThingGroupCommand is sent using aws-sdk-js-v3

0

I have created a JS lambda via Amplify using the NodeJS 20.x runtime. The lambda gets triggered by a DyanmoDb stream when a new item is created in a table. When a new item is created I want the lambda to use the content of the item to create a new ThingGroup in IoT Core. To create the ThingGroup I use the aws-sdk-js-v3 library and the CreateThingGroupCommand.

My lambda code looks like this:

import { unmarshall } from "@aws-sdk/util-dynamodb";
import { IoTClient, CreateThingGroupCommand } from "@aws-sdk/client-iot";

/**
 * @type {import('@types/aws-lambda').APIGatewayProxyHandler}
 */
export const handler = async (event) => {
  try {
    // Setup an IotClient
    const client = new IoTClient({ region: process.env.REGION });

    // Get all records with new and old data from incoming event
    const records = event.Records.map(record => ({
      new: unmarshall(record.dynamodb.NewImage)
    }));

    records.forEach(async record => {
        let thingGroupName = record.new.name;
        let thingGroupDescription = 'Thing group for ' + record.new.name;

        const params = {
          thingGroupName: thingGroupName,
          thingGroupProperties: {
            thingGroupDescription: thingGroupDescription,
          },
        };

        try {
          const command = new CreateThingGroupCommand(params);
          const response = await client.send(command);
          console.log('New IoT Core ThingGroup created!');
          console.log(response);
        } catch (error) {
          console.log('Failed to create new IoT Core ThingGroup!');
          console.log(error);
        }
      }      
    });

    return {
      statusCode: 200,
      body: JSON.stringify({ message: "Successfully create new ThingGroup!" }),
    };

  } catch (error) {
    console.log('Failed to process DynamoDb stream with error: ', error);

    return {
      statusCode: 500,
      body: JSON.stringify({ message: "Failed to process DynamoDb stream" }),
    };
  }
};

I have a policy that I have attached to the role the lambda is using that look like this:

{ "Version": "2012-10-17", "Statement": [ { "Action": [ "iot:CreateThingGroup" ], "Resource": [ "*" ], "Effect": "Allow" } ] }

When the lambda is triggered by the DynamoDb stream it gets the data from the stream, setup the CreateThingGroupCommand but then when it performs the const response = await client.send(command); nothing more happens. The next line that has the console.log('New IoT Core ThingGroup created!'); never executes and there is no exception that is caught, the lambda just terminates.

There is no ThingGroup created with the name from the stream.

I can't find any more data in the LogGroup for the lambda or in the any CloudTrail event history that informs me of what might have gone wrong.

Is there anything else I could do to track down what is going wrong or can anyone see what could potentially be wrong with my code?

1 Antwort
0
Akzeptierte Antwort

Hi, for-each does not work with await as you are expecting it to work. Please change this to for..of loop and try. Review this link for more details - https://gist.github.com/joeytwiddle/37d2085425c049629b80956d3c618971

AWS
beantwortet vor einem Monat
  • Thanks for this easy solution to my problem. It worked right away when switching to a for..of loop.

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen