Amazon EC2 インスタンスの管理 - AWS SDK for JavaScript

AWS SDK for JavaScript v2 の近日発表 end-of-support しました。AWS SDK for JavaScript v3 に移行することをお勧めします。日付、その他の詳細、移行方法については、リンク先の発表内容を参照してください。

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

Amazon EC2 インスタンスの管理

JavaScript code example that applies to Node.js execution

この Node.js コード例は以下を示しています。

  • Amazon EC2 インスタンスに関する基本情報を取得する方法。

  • Amazon EC2 インスタンスの詳細モニタリングを開始および停止する方法。

  • Amazon EC2 インスタンスを開始および停止する方法。

  • Amazon EC2 インスタンスを再起動する方法。

シナリオ

この例では、一連の Node.js モジュールを使用して、いくつかの基本インスタンス管理オペレーションを実行します。Node.js モジュールは、Amazon EC2 クライアントクラスの次のメソッドを使用してインスタンスを管理するために SDK for JavaScript を使用します。

Amazon EC2 インスタンスのライフサイクルの詳細については、Linux インスタンス用の Amazon EC2 ユーザーガイドインスタンスのライフサイクルを参照してください。

前提条件タスク

この例をセットアップして実行するには、まず次のタスクを完了します。

インスタンスの説明

ec2_describeinstances.js というファイル名で Node.js モジュールを作成します。前に示したように SDK を必ず設定します。Amazon EC2 にアクセスするには、AWS.EC2 サービスオブジェクトを作成します。Amazon EC2 サービスオブジェクトの describeInstances メソッドを呼び出して、インスタンスの詳細な説明を取得します。

// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set the region AWS.config.update({ region: "REGION" }); // Create EC2 service object var ec2 = new AWS.EC2({ apiVersion: "2016-11-15" }); var params = { DryRun: false, }; // Call EC2 to retrieve policy for selected bucket ec2.describeInstances(params, function (err, data) { if (err) { console.log("Error", err.stack); } else { console.log("Success", JSON.stringify(data)); } });

この例を実行するには、コマンドラインに次のように入力します。

node ec2_describeinstances.js

このサンプルコードは、このGitHubにあります。

インスタンス監視の管理

ec2_monitorinstances.js というファイル名で Node.js モジュールを作成します。前に示したように SDK を必ず設定します。Amazon EC2 にアクセスするには、AWS.EC2 サービスオブジェクトを作成します。監視を制御するインスタンスのインスタンス ID を追加します。

コマンドライン引数 (ON または OFF) の値に基づいて、Amazon EC2 サービスオブジェクトの monitorInstances メソッドを呼び出して、指定したインスタンスの詳細モニタリングを開始するか、unmonitorInstances メソッドを呼び出します。DryRun パラメータを使用して、これらのインスタンスのモニタリングを変更する前に、インスタンスのモニタリングを変更する権限があるかどうかをテストします。

// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set the region AWS.config.update({ region: "REGION" }); // Create EC2 service object var ec2 = new AWS.EC2({ apiVersion: "2016-11-15" }); var params = { InstanceIds: ["INSTANCE_ID"], DryRun: true, }; if (process.argv[2].toUpperCase() === "ON") { // Call EC2 to start monitoring the selected instances ec2.monitorInstances(params, function (err, data) { if (err && err.code === "DryRunOperation") { params.DryRun = false; ec2.monitorInstances(params, function (err, data) { if (err) { console.log("Error", err); } else if (data) { console.log("Success", data.InstanceMonitorings); } }); } else { console.log("You don't have permission to change instance monitoring."); } }); } else if (process.argv[2].toUpperCase() === "OFF") { // Call EC2 to stop monitoring the selected instances ec2.unmonitorInstances(params, function (err, data) { if (err && err.code === "DryRunOperation") { params.DryRun = false; ec2.unmonitorInstances(params, function (err, data) { if (err) { console.log("Error", err); } else if (data) { console.log("Success", data.InstanceMonitorings); } }); } else { console.log("You don't have permission to change instance monitoring."); } }); }

この例を実行するには、コマンドラインで次のように入力します。詳細モニタリングを開始するには、ON を、モニタリングを停止するには、OFF を指定します。

node ec2_monitorinstances.js ON

このサンプルコードは、このGitHubにあります。

インスタンスの開始と停止

ec2_startstopinstances.js というファイル名で Node.js モジュールを作成します。前に示したように SDK を必ず設定します。Amazon EC2 にアクセスするには、AWS.EC2 サービスオブジェクトを作成します。開始または停止するインスタンスのインスタンス ID を追加します。

コマンドライン引数 (START または STOP) の値に基づいて、Amazon EC2 サービスオブジェクトの startInstances メソッドを呼び出して、指定されたインスタンスを開始するか、stopInstances メソッドを呼び出してそれらを停止します。DryRun パラメータを使用して、選択したインスタンスを実際に開始または停止しようとする前に、権限があるかどうかをテストします。

// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set the region AWS.config.update({ region: "REGION" }); // Create EC2 service object var ec2 = new AWS.EC2({ apiVersion: "2016-11-15" }); var params = { InstanceIds: [process.argv[3]], DryRun: true, }; if (process.argv[2].toUpperCase() === "START") { // Call EC2 to start the selected instances ec2.startInstances(params, function (err, data) { if (err && err.code === "DryRunOperation") { params.DryRun = false; ec2.startInstances(params, function (err, data) { if (err) { console.log("Error", err); } else if (data) { console.log("Success", data.StartingInstances); } }); } else { console.log("You don't have permission to start instances."); } }); } else if (process.argv[2].toUpperCase() === "STOP") { // Call EC2 to stop the selected instances ec2.stopInstances(params, function (err, data) { if (err && err.code === "DryRunOperation") { params.DryRun = false; ec2.stopInstances(params, function (err, data) { if (err) { console.log("Error", err); } else if (data) { console.log("Success", data.StoppingInstances); } }); } else { console.log("You don't have permission to stop instances"); } }); }

この例を実行するには、コマンドラインで次のように入力します。インスタンスを開始するには、START を、停止するには、STOP を指定します。

node ec2_startstopinstances.js START INSTANCE_ID

このサンプルコードは、このGitHubにあります。

インスタンスの再起動

ec2_rebootinstances.js というファイル名で Node.js モジュールを作成します。前に示したように SDK を必ず設定します。Amazon EC2 にアクセスするには、Amazon EC2 サービスオブジェクトを作成します。再起動するインスタンスのインスタンス ID を追加します。AWS.EC2 サービスオブジェクトの rebootInstances メソッドを呼び出して、指定されたインスタンスを再起動します。DryRun パラメータを使用して、実際にインスタンスを再起動する前に、これらのインスタンスを再起動する権限があるかどうかをテストします。

// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set the region AWS.config.update({ region: "REGION" }); // Create EC2 service object var ec2 = new AWS.EC2({ apiVersion: "2016-11-15" }); var params = { InstanceIds: ["INSTANCE_ID"], DryRun: true, }; // Call EC2 to reboot instances ec2.rebootInstances(params, function (err, data) { if (err && err.code === "DryRunOperation") { params.DryRun = false; ec2.rebootInstances(params, function (err, data) { if (err) { console.log("Error", err); } else if (data) { console.log("Success", data); } }); } else { console.log("You don't have permission to reboot instances."); } });

この例を実行するには、コマンドラインに次のように入力します。

node ec2_rebootinstances.js

このサンプルコードは、このGitHubにあります。