list_ec2_instances
Retrieve and display EC2 instance details including ID, type, state, and public IP for AWS resource management.
Instructions
Lists EC2 instances in the current region, showing ID, type, state, and public IP.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| region | No | Optional AWS region to list instances from (overrides default) |
Implementation Reference
- src/index.ts:846-890 (handler)The main handler function for the 'list_ec2_instances' tool. It uses the EC2Client (region-specific if provided) to send a DescribeInstancesCommand, extracts instance details including ID, name (from tags), type, state, IPs, launch time, and tags, formats as JSON, and notes the default region if not overridden.if (name === "list_ec2_instances") { // Create a region-specific client if provided, otherwise use default // Note: Re-instantiating client for every request isn't ideal for perf but fine for this scale // To strictly support region override we'd need to recreate the client or default to the global one // For simplicity here using the global one unless specific tool logic is needed. // Actually, if args.region is passed, we should use a new client. const region = (args as { region?: string })?.region; const client = region ? new EC2Client({ region }) : ec2Client; const command = new DescribeInstancesCommand({}); const response = await client.send(command); const instances = response.Reservations?.flatMap( (r) => r.Instances?.map((i) => ({ InstanceId: i.InstanceId, Name: i.Tags?.find((t) => t.Key === "Name")?.Value, InstanceType: i.InstanceType, State: i.State?.Name, PublicIpAddress: i.PublicIpAddress, PrivateIpAddress: i.PrivateIpAddress, LaunchTime: i.LaunchTime, Tags: i.Tags, })) || [] ) || []; const content: any[] = [ { type: "text", text: JSON.stringify(instances, null, 2), } ]; if (!region) { content.push({ type: "text", text: "\n(Checked default region 'us-east-1'. Use the 'region' argument to check other regions like 'ap-south-1', 'us-west-2', etc.)" }); } return { content: content, }; }
- src/index.ts:118-130 (registration)Registration of the 'list_ec2_instances' tool in the ListTools response, including its name, description, and input schema for optional region parameter.{ name: "list_ec2_instances", description: "Lists EC2 instances in the current region, showing ID, type, state, and public IP.", inputSchema: { type: "object", properties: { region: { type: "string", description: "Optional AWS region to list instances from (overrides default)", }, }, }, },
- src/index.ts:121-129 (schema)The input schema definition for the 'list_ec2_instances' tool, specifying an optional 'region' string parameter.inputSchema: { type: "object", properties: { region: { type: "string", description: "Optional AWS region to list instances from (overrides default)", }, }, },
- src/index.ts:53-53 (helper)Initialization of the shared EC2Client used by the list_ec2_instances handler (and others).const ec2Client = new EC2Client({});