Amazon AWS Node.js WebSocket - node.js

With the Amazon AWS Free Usage Tier, how should I deploy a simple Node.js WebSocket chat server that uses the ws & pg modules?
If you can: How to deploy a Node.js WebSocket server to Amazon Elastic Beanstalk?
Otherwise, what are all the steps I should take to set it up using EC2 directly?
Or, should I do it with OpsCloud?

You CAN use Elastic Beanstalk with the free usage tier. However if you want to run both Postgres and Node.js, you might find you need to roll your own EC2 instance to remain within the free usage constraints - check here: http://aws.amazon.com/free/
EB does make it relatively simple to deploy, and if you do your development setup correctly, EB will install the extra node modules you need.
In any case, when developing you will find you need to install new modules - I'm sure you're familiar with the npm install mechanism. What you need to add to that is the --save option, so
npm install pg
becomes
npm install --save pg
This then adds that package to the dependencies section of the package.json file. When you deploy to EB or any other dev ops system, these dependencies are automatically installed (the system runs npm install). If you are cloning from a repository onto your server, then running npm install yourself will install those modules.
Hopefully this helps you get the modules you need installed on the server. It's worth noting that your node_modules directory should not be shipped with your code - let the system build it when deploying.

Related

How can I deploy a Serverless Framework application the macOS that uses the sharp library to AWS?

I'm developing a Serverless Framework application that is using the Node runtime and is deployed to AWS. One of my AWS Lambda functions uses the sharp library.
When I run the AWS Lambda function, the following error occurs:
'darwin-x64' binaries cannot be used on the 'linux-x64' platform. Please remove the 'node_modules/sharp/vendor' directory and run 'npm install'.
I believe this error is occurring because when I run the sls deploy command on my local computer, the application is packaged on macOS and then moved to AWS. I think the application needs to be packaged on an operating system using linux-x64.
How can I deploy my Serverless Framework from my computer and still be able to use the sharp library?
You can install the Linux compatible package using the following:
rm -rf node_modules/sharp
npm install --arch=x64 --platform=linux --target=10.15.0 sharp
Note that this also specifies a target NodeJS version, ensure its the same version of node you're using in your Lambda. This is straight out of the docs (see here.)
However that didn't solve my problems. My serverless configuration (using serverless-bundle plugin) meant that my modules were being installed again in a separate folder, wiping out the platform-specific modules I just manually installed.
Two choices here:
use serverless-plugin-scripts to hook into the deploy events to run the above patch; or
run serverless in docker using a Linux container with a matching node version.
For my specific edge case I had to go with Docker. The build scripts will effect every function you're deploying -- adding ~30mb of Sharp code -- and Lambda#Edge has limitations on source code size.
The description of https://www.npmjs.com/package/sharp suggests it is linux compatible.
I am unfamiliar with how to (or if you can) force nodes native package resolution to a separate OS. Try building your lambda zip inside a docker image https://hub.docker.com/_/alpine/
If you have not already, I would suggest following the Installing the AWS SAM CLI on macOS guide to insure you have the correct local environment for developing Serverless on macOS.
This process is designed for the macOS, and includes built-in support for Docker, so that you can build and deploy packages that are compatible with Lambda directly from your local computer.
For AWS lambda deployment with Sharp module, the following worked for me when using serverless, esbuild and serverless-esbuild. Changed the serverless.yml file with the below configuration. It is basically telling esbuild to download sharp again with the following --arch=x64 --platform=linux considering your lambda uses x64 arch. Check serverless-esbuild packager and packagerOptions options for more understanding.
esbuild:
# keep existing configurations
external:
- sharp
packagerOptions:
scripts:
- npm install --arch=x64 --platform=linux sharp

Angular2 development Without node and npm

I am new angular2 development, and what i came to know is before starin the angular2 development, I must install the nodejs for server and npm to download dependencies from official documentation.
I succesfully deployed the source code in tomcat sever[by build]
So my Question is after installation and creating the new project, i got node_modules. By using these node_modules(can i start development of angular2 on another i.e a new machine where node & npm is not installed)
Basically my question is.. I want to start development of angular2 by using the project structure on new machine. Without the installation on node & npm
From Angular docs
Node.js and npm are essential to Angular development.
If you built/compiled the app and have all the modules installed (you have your node_modules) folder then its just javascript and html which you can run on any server you want.
For npm, if you need any modules/packages, I think you can manually download the package and add it to your development environment. But what if the process needs to be automated? You can't just sit there and download all the packages from github. So npm is really helpful when it come to this. You only need a file containing all dependencies, and run it at build time (package.json)
About nodejs, nodejs allows you to run javascript on the server when you need any interaction with the database. So why don't we just go with the easy way?

AWS Elastic Beanstalk: Do I have to deploy my app with full "/node_modules" and "/bower_components" and compiled sass and other dependencies?

I don't understand the way AWS Elastic Beanstalk work in terms of Node.js app. Best practices of Node.js development say that one don't have to push /node_modules or /bower_components or other third party packages to version control systems (GitHub, for example), only package.json. All those /node_modules have to be in .gitignore. Then any person can do npm install or bower install and install all the dependencies. And the same is for sass - compile them for production env.
Ok, but what about AWS Elastic Beanstalk? How do I have to create a source bundle of my Node.js app? Do I have to commit FULL /node_modules and /bower_components and then make a .zip bundle with them? But my HEAD commit is without /node_modules, because /node_modules is in .gitignore. So no dependencies will be in .zip bundle:
git archive --format=zip HEAD > app.zip
Or do I have an option to install all the dependencies AFTER I have deployed my app.zip to AWS Elastic Beanstalk?
Or AWS Elastic Beanstalk does npm install by itself somewhere under the hood immediately after I have uploaded my app.zip bundle?
Elastic Beanstalk runs a few npm commands when deploying your app using got. I did a little experiment a few months back.
In short, you can use npm install to deploy your dependencies, then use npm prestart to build and compile things like sass.

meteor deploy npm packages

I use multiple npm packages in my meteor application, for instance the 'knox' package for amazon s3 access.
On my local system I don't have any problems, because I have the 'knox'- npm package installed on my system. But on the server this is obviously not the case.
I have read different suggestions what I could do:
1)
Install the npm module into the /public folder of my application
- unfortunately I don't know how to do that.
2)
I followed this tutorial:
NPM Deploy Tutorial
I created packages/knox/package.js packages/knox/knox.js and I am pretty sure I did everything as described in the tutorial but this is not even working locally
Use npm package from Atmosphere. See details on how to use it.
Did you remember to add the knox package to the .meteor/packages file?
The link you shared is pre Meteor 0.6.5, which loaded all packages in packages/ automatically. Now, you need to specify them individually.

Amazon EC2 NodeJS npm does not install modules

I just configured a new micro intsance on amazon ec2.
I checked out node source code and build it.
Now i want to setup express and other node modules but npm does not work.
When i give command e.g. npm install express, it waits there without any further process.
Any help appreciated.

Resources