Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to create and run a new Instance #6091

Closed
3 tasks done
rrajj opened this issue May 13, 2024 · 6 comments
Closed
3 tasks done

Not able to create and run a new Instance #6091

rrajj opened this issue May 13, 2024 · 6 comments
Assignees
Labels
bug This issue is a bug.

Comments

@rrajj
Copy link

rrajj commented May 13, 2024

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);
  }
};```
  1. 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.

@rrajj rrajj added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels May 13, 2024
@aBurmeseDev aBurmeseDev self-assigned this May 13, 2024
@aBurmeseDev aBurmeseDev added investigating Issue is being investigated and/or work is in progress to resolve the issue. and removed needs-triage This issue or PR still needs to be triaged. labels May 13, 2024
@aBurmeseDev
Copy link
Member

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

@aBurmeseDev aBurmeseDev added response-requested Waiting on additional info and feedback. Will move to \"closing-soon\" in 7 days. and removed investigating Issue is being investigated and/or work is in progress to resolve the issue. labels May 13, 2024
@rrajj
Copy link
Author

rrajj commented May 13, 2024

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
Copy link
Author

rrajj commented May 13, 2024

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
}```

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to \"closing-soon\" in 7 days. label May 14, 2024
@rrajj
Copy link
Author

rrajj commented May 20, 2024

@aBurmeseDev Hi, any further help?

@rrajj
Copy link
Author

rrajj commented May 21, 2024

Solved

@rrajj rrajj closed this as completed May 21, 2024
Copy link

github-actions bot commented Jun 6, 2024

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants