AWS - ec2Client.send never resolves in AWS SDK V3 JS

53 Views Asked by At

I'm setting up a lambda for monitoring purposes using AWS SDK for JS V3. I want to retrieve the health status of an instance, this is the code

import { EC2Client, DescribeInstanceStatusCommand } from "@aws-sdk/client-ec2";

const ec2Client = new EC2Client();

export const handler = async (event) => {
  let instanceIds = ["i-..."];

  for (let instanceId of instanceIds) {
    try {
      const params = {
        InstanceIds: [instanceId],
      };
      console.log('Sending', params);
      const result = await ec2Client.send(
        new DescribeInstanceStatusCommand(params),
      );
      console.log('Received', result);
      const healthStatus = result.InstanceStatuses[0]?.InstanceStatus?.Status;

      if (healthStatus !== "ok") {
        console.log(`EC2 instance ${instanceId} is not healthy:`, healthStatus);
      }
    } catch (error) {
      console.error(`Error getting EC2 health for ${instanceId}:`, error);
    }
  }
}

At the beginning it would run fine, then it started timing out seemingly at random. Now most of the times it times out, even though when it works it runs for roughly 800ms and I tried increasing the timeout to 10 seconds. It always manages to log 'Sending...' but most of the times fails to pass through await ec2Client.send. Yet sometimes it executes fine again and when it does I can retest it as many times as I want without incurring in timeouts. I doubt this has anything to do with cold starts, given the high timeout I've tested with.
I'm pretty sure I'm not going over rate limits as I'm still testing manually at this stage, and if that was the case I would expect some sort of exceptions. Also, based on rate limits docs I could find on AWS official docs, I'm confident I'm well below the limits. My Lambda is running the default configuration on Node 18 under an execution role that allows full access to EC2.

0

There are 0 best solutions below