aws(学习笔记第二课)
- 使用
AWS SDK(node js)
学习内容:
- 使用
AWS SDK(node js)
1. AWS SDK(node js)
AWS
支持多种SDK
开发(除了AWS CLI
,还支持其他的SDK
)Android
Python
Node.js(Javascript)
Java
Browsers(Javascript)
Ruby
PHP
.NET
IOS
Go
- 使用
Node.js
来控制AWS
-
安装
Node.js
这是前提条件,这里采用windows
+git bash
的方式练习,首先安装Node.js
-
Node.js
的程序结构 -
package.json
这里定义了nodecc
所需要的依赖包。{ "dependencies": { "aws-sdk": "2.1.18", "blessed": "0.0.51", "jmespath": "0.10.0" }, "private": true }
-
index.js
这个是main
程序入口,定义了以下的功能listServer
(list servers
展示所有的ec2 server
列表)listAMIs.js
(create server
创建ec2 server
)listServers.js
(terminate server
终止ec2 server
)
-
\lib
下的各个*.js
createServer.js
创建ec2 server
注意,这里的KeyName
一定要和前面设定的密钥对名字一致。listAMIs.js
列出全部可用ami
listServers.js
列出全部可用ec2 server
listSubnets.js
列出全部可用subnet
showServer.js
列出所有创建的ec2 server
terminateServer.js
终止列出的ec2 server
-
index.js以及各个lib/*.js
- index.js
var blessed = require('blessed'); var screen = blessed.screen({ autoPadding: true, smartCSR: true, log: "./nodecc.log" }); screen.title = 'Node Control Center for AWS'; var content = blessed.box({ parent: screen, width: '70%', height: '90%', top: '10%', left: '30%', border: { type: 'none', fg: '#ffffff' }, fg: 'white', bg: 'blue', content: '{bold}Node Control Center for AWS{/bold}\n\nPlease select one of the actions from the left and press return.\n\nYou can always go back with the left arrow key.\n\nYou can terminate the application by pressing ESC or q.', tags: true }); var progress = blessed.progressbar({ parent: screen, width: '70%', height: '10%', top: '0%', left: '30%', orientation: 'horizontal', border: { type: 'line', fg: '#ffffff' }, fg: 'white', bg: 'blue', barFg: 'green', barBg: 'green', filled: 0 }); var list = blessed.list({ parent: screen, width: '30%', height: '100%', top: '0%', left: '0%', border: { type: 'line', fg: '#ffffff' }, fg: 'white', bg: 'blue', selectedBg: 'green', mouse: true, keys: true, vi: true, label: 'actions', items: ['list servers', 'create server', 'terminate server'] }); list.on('select', function(ev, i) { content.border.type = 'line'; content.focus(); list.border.type = 'none'; open(i); screen.render(); }); list.focus(); function open(i) { screen.log('open(' + i + ')'); if (i === 0) { loading(); require('./lib/listServers.js')(function(err, instanceIds) { loaded(); if (err) { log('error', 'listServers cb err: ' + err); } else { var instanceList =<
- index.js
-