在NodeJS中配置aws ec2

  1. 获取access key和secret access key
          自己账户下有security credentials的选项
          
          然后点击Access Keys
    
          根据提示create new access key
  1. 安装SDK
     npm install aws-sdk --save 
          在app.js中,声明引用aws-sdk
     var AWS = require('aws-sdk'); 
  1. 配置EC2的服务
          在项目根目录下新建文件config.json,输入以下内容保存
     { "accessKeyId": "Your_Own_Access_Key_Id", "secretAccessKey": "Your_Own_Secret_Access_Key", "region": "Your_Own_Instance_Region" } 
          在app.js中,load之前的配置文件
     AWS.config.loadFromPath('./config.json'); 
          接着配置ec2
     var ec2 = new AWS.EC2({region: 'us-west-1'}); 
  1. 创建Instance
 1 // Create the instance
 2 ec2.runInstances(params, function(err, data) {
 3   if (err) {
 4     console.log("Could not create instance", err);
 5     return;
 6   }
 7 
 8   var instanceId = data.Instances[0].InstanceId;
 9   console.log("Created instance: ", instanceId);
10 
11   // Add tags to the instance
12   params = {
13     Resources: [instanceId],
14     Tags: [{
15       Key: 'Name',
16       Value: 'Test_01'
17     }]
18   };
19 
20   ec2.createTags(params, function(err) {
21     console.log("Tagging instance", err ? "failure" : "success");
22   });
23 });

 

 
posted @ 2016-09-14 03:20  十万片晶矿  阅读(1479)  评论(0编辑  收藏  举报