- Get access key and secret access key
The option to have security credentials under your account and click Access Keys to create new access key as prompted
- Installing the SDK
npm Install AWS-SDK--saveIn App.js, the declaration references Aws-sdk var AWS = require (' aws-sdk ');
- Configuring the EC2 Service
Under Project Root, create a new file Config.json, and enter the following to save { "Accesskeyid": "your_own_access_key_id", "Secretaccesskey": "Your_own_secret_access_key" , "region": "Your_own_instance_region" }
In App.js, the configuration file before loadAWS.config.loadFromPath ('./config.json ');Then configure EC2 var ec2 = new AWS. EC2 ({region: ' us-west-1 '});
- Create Instance
1 //Create The instance2Ec2.runinstances (params,function(err, data) {3 if(err) {4Console.log ("Could not create instance", err);5 return;6 }7 8 varInstanceId = data. Instances[0]. InstanceId;9Console.log ("Created instance:", instanceId);Ten One //ADD Tags to the instance Aparams = { - Resources: [InstanceId], - Tags: [{ theKey: ' Name ', -Value: ' test_01 ' - }] - }; + -Ec2.createtags (params,function(err) { +Console.log ("Tagging instance", err?) "Failure": "Success"); A }); at});
Reference: Http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/node-configuring.html Http://docs.aws.amazon.com/AWSJav Ascriptsdk/guide/node-examples.html http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/node-services.html Http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/EC2.html
Configuring AWS EC2 in Nodejs