aws / aws-sdk-js-v3

Modularized AWS SDK for JavaScript.
Apache License 2.0
2.96k stars 557 forks source link

Not able to create and run a new Instance #6091

Closed rrajj closed 1 month ago

rrajj commented 1 month ago

Checkboxes for prior research

Describe the bug

Nothing happens at the following line of code:

try {
    console.log(in try block);
    const response = await client.send(command);
    console.log(response);
  } catch (err) {
    console.error(err);
  }

Code just stops after printing in try block. Checked the console, no errors there.

SDK version number

2.1618.0

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

v20.5.1

Reproduction Steps

// Create a new EC2 instance.
export const main = async () => {
  const command = new RunInstancesCommand({
    ImageId: 'ami-07caf09b362be10b8',
    InstanceType: 't2.micro',
    // Ensure only 1 instance launches.
    MinCount: 1,
    MaxCount: 1,
  });

  try {
    const response = await client.send(command);
    console.log(response);
  } catch (err) {
    console.error(err);
  }
};```

2) Running the code should create and run an instance, BUT nothing happens at all.

Observed Behavior

Checked the AWS Console, for the particular deployment. It prints the line "in try block" and stops. Below is the log stack 2024-05-12T18:29:43.843Z 5f9b7776-03f7-4a6b-9e6a-246ea0ef1faa INFO in try block END RequestId: 5f9b7776-03f7-4a6b-9e6a-246ea0ef1faa REPORT RequestId: 5f9b7776-03f7-4a6b-9e6a-246ea0ef1faa Duration: 597.59 ms Billed Duration: 598 ms Memory Size: 128 MB Max Memory Used: 128 MB Init Duration: 647.20 ms

Expected Behavior

Create a new instance and run a provided script.

Possible Solution

No response

Additional Information/Context

I have Administrator Access.

aBurmeseDev commented 1 month ago

Hi @rrajj - thanks for reaching out.

I'm not able to reproduce it on my end with the code below and I would suggest modifying your code inside async function.

const { EC2Client, RunInstancesCommand } = require("@aws-sdk/client-ec2");
// Set up AWS credentials and region
const AWS_REGION = "your-aws-region";
const AWS_ACCESS_KEY_ID = "your-access-key-id";
const AWS_SECRET_ACCESS_KEY = "your-secret-access-key";
// Set up EC2 client
const ec2Client = new EC2Client({
    region: AWS_REGION,
    credentials: {
        accessKeyId: AWS_ACCESS_KEY_ID,
        secretAccessKey: AWS_SECRET_ACCESS_KEY
    }
});
// Set up parameters for the runInstances command
const params = {
    ImageId: "your-ami-image-id",
    InstanceType: "t2.micro",
    MaxCount: 1,
    MinCount: 1
    // Add more parameters as needed
};
// Create a function to run the EC2 instance
const runEC2Instance = async () => {
    try {
        // Call the runInstances command
        const data = await ec2Client.send(new RunInstancesCommand(params));
        console.log("Instance ID:", data.Instances[0].InstanceId);
    } catch (err) {
        console.error("Error running EC2 instance:", err);
    }
};
// Call the function to run the EC2 instance
runEC2Instance();

If it doesn't get resolved, please add this middewareStack which would output raw request and share so that I can further investigate.

client.middlewareStack.add(
  (next, context) => async (args) => {
    console.log("AWS SDK context", context.clientName, context.commandName);
    console.log("AWS SDK request input", args.input);
    const result = await next(args);
    console.log("AWS SDK request output:", result.output);
    return result;
  },
  {
    name: "MyMiddleware",
    step: "build",
    override: true,
  }
);

Best, John

rrajj commented 1 month ago

Hey @aBurmeseDev , I am using it as a Lambda function, just to make sure, for this task what should be an ideal timeout duration ?

rrajj commented 1 month ago

Below is the code I ran right now. Let me know if that looks any good. (new to this)

const runEC2Instance = async () => {
            try {
                // Call the runInstances command
                const data = await ec2.send(new RunInstancesCommand(params));
                console.log("Instance ID:", data.Instances[0].InstanceId);
            } catch (err) {
                console.error("Error running EC2 instance:", err);
            }
        };
        // Call the function to run the EC2 instance
        console.log("Reached here to run the function")
        runEC2Instance();

        ec2.middlewareStack.add(
            (next, context) => async (args) => {
              console.log("AWS SDK context", context.clientName, context.commandName);
              console.log("AWS SDK request input", args.input);
              const result = await next(args);
              console.log("AWS SDK request output:", result.output);
              return result;
            },
            {
              name: "MyMiddleware",
              step: "build",
              override: true,
            }
          );

          console.log(ec2.middlewareStack)

The console log prints:


2024-05-13T20:04:09.064Z    3ce65c16-8dc8-4261-92f5-3cbc02f0c120    INFO    {
  add: [Function: add],
  addRelativeTo: [Function: addRelativeTo],
  clone: [Function: clone],
  use: [Function: use],
  remove: [Function: remove],
  removeByTag: [Function: removeByTag],
  concat: [Function: concat],
  applyToStack: [Function: cloneTo],
  identify: [Function: identify],
  identifyOnResolve: [Function: identifyOnResolve],
  resolve: [Function: resolve]

END RequestId: 3ce65c16-8dc8-4261-92f5-3cbc02f0c120
}```
rrajj commented 1 month ago

@aBurmeseDev Hi, any further help?

rrajj commented 1 month ago

Solved

github-actions[bot] commented 3 weeks ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.