Hey guys!, that’s my experience creating a node.js app inside AWS (Amazon Web Services), that access a database MySql, the first that you have to do is to create a account to AWS (1 year free for most of the services), then, go to Elastic Beanstalk and create one node.js web server:
you can folow the instructions at the page is really simple, I was created a startup node.js app, that includes this files:
this sample files instruct us how to adapt your app.js file, as you can see in this standard file:
var port = process.env.PORT || 3000, http = require('http'), fs = require('fs'), html = fs.readFileSync('index.html');</code> var log = function(entry) { fs.appendFileSync('/tmp/sample-app.log', new Date().toISOString() + ' - ' + entry + '\n'); }; var server = http.createServer(function (req, res) { if (req.method === 'POST') { var body = ''; req.on('data', function(chunk) { body += chunk; }); req.on('end', function() { if (req.url === '/') { log('Received message: ' + body); } else if (req.url = '/scheduled') { log('Received task ' + req.headers['x-aws-sqsd-taskname'] + ' scheduled at ' + req.headers['x-aws-sqsd-scheduled-at']); } res.writeHead(200, 'OK', {'Content-Type': 'text/plain'}); res.end(); }); } else { res.writeHead(200); res.write(html); res.end(); } }); // Listen on port 3000, IP defaults to 127.0.0.1 server.listen(port); // Put a friendly message on the terminal console.log('Server running at http://127.0.0.1:' + port + '/');
Is impportant to link your node.js service to your RDS database:
You can access from outside AWS to your instance of Mysql,to know the port and the name to connect:
And have to enable outside access too:
To access the database from de app, you can follow the manual:
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_nodejs.rds.html
Finaly I was modified my code to add the database to in the create connection:
var client = mysql.createConnection({ host : process.env.RDS_HOSTNAME, user : process.env.RDS_USERNAME, password : process.env.RDS_PASSWORD, port : process.env.RDS_PORT, database : "ebdb" });