5

I currently have ReactJS + NodeJS/ExpressJS + Webpack onto EC2, Amazon Web Services (AWS) under one project and would like to get it deployed together at once, in one project.

What are some suggestions on how to go about doing so? Done the research, and I've only seen tutorials on deploying one in specific, whether it be just ReactJS or just NodeJS. Any insights or leads would be greatly appreciated.

Will accept/upvote answer. Thank you in advance

1
  • With Docker it's very easy and EC2 support it. Commented Dec 29, 2016 at 21:05

5 Answers 5

4

You don't "deploy" ReactJS, it's just a static file or files like any other JS libraries in your applications. You also don't deploy Webpack. Webpack should run on a developer machine (or in CI/CD stack or build system).

As for the NodeJS part just use Elastic Beanstalk.

Sign up to request clarification or add additional context in comments.

5 Comments

Before I accept the answer/upvote, have a few questions. There is no need for any configurations or hack around to get ReactJS + Webpack working then so long as I just deploy NodeJS? Could you refer me to a guide to deploy it as how I mentioned? Thank you in advance
There is an official guide called "Deploying Node.js Applications to AWS Elastic Beanstalk" here: docs.aws.amazon.com/elasticbeanstalk/latest/dg/…
How can I do so to EC2? And would I just need to deploy NodeJS like so and rest would just work out?
just checking in to see if you've seen my last comment. Please let me know.
I disagree that webpack should be run on dev machine. This pollutes the source control history and storage. I run all my builds during deployment using a git hook.
4
+25

I do not commit builds to source control. I see that a lot and it can make things easier, but you can also forget to rebuild as you have to do it manually, and it adds a lot of bloat to your repo.

I believe builds should be run as part of the deployment process. Assuming you are using git, you can add script hooks/post-receive in a remote repo there. When you push to that remote, the script will run. This is where I do my webpack build.

You may want to look into https://github.com/git-deploy/git-deploy for context, but I do this manually.

In my projects, on the deployment machine I do git --init --bare /var/git/myproject.git then add the script in /var/git/hooks/post-receive. The hook checks out the code into /var/www/myproject, runs the build, which fills in the /var/www/myproject/build. Then it removes the old /var/www/myproject/public and renames build to public. And done.

2 Comments

How can I deploy reactjs/nodejs project together?
You can customize the post-receive script to do whatever you want, including launching the node server. Entirely up to you!
3

I'm coming from more of an operations background and would say that if your goal includes keeping that site up as much as possible then use Packer to generate AMI's and CloudFormation to build an Application Load Balancer (the newer, cheaper brother of ELB) in front of an AutoScalingGroup which keeps the EC2 instances up and running.

24 Comments

No mean to turn down your suggestion, but I've just been hearing words being thrown around, and would rather prefer some actual guidance as to how/where to start at the very least. A full guide would be extremely preferred.
I'll try again then. But first I need to get a clarification of the goal of your setup - is it for production and important to be stable and available or is it for development where stability is less important in favour of rapid updates?
No worries! It's for production but only for like internal usage so I would say more in favour of rapid updates. But would have to deploy them in EC2, preferably all under one project.
OK then. I suspect that using Packer to burn an AMI for every change would take too long. I'd suggest then that you look at using Docker to build Docker images and configure your EC2 instance to pull down and run the new docker image. Have that EC2 instance fired up by an AutoScalingGroup (it can be a constant number of instances). For the CloudFormation/ASG/UserData part (but not Docker) you might get a hint from github.com/amosshapira/thermal
What is use for Docker? I currently have EC2 instance with Amazon Linux running and transferred my project (which contains both React/NodeJS/Express) onto the instance via SFTP. And for EC2's Security Groups, I opened a port for 3000 (protocol: tcp, source: 0.0.0.0/0), which is how my Express is defined as well. So I ran the Express within EC2 instance, but see it listening to port 3000 within the terminal. But once I hit the Public DNS with :3000, it says The site can't be reached - ec2...us-west-1.compute.amazonaws.com took too late to respond. What could be the issue?
|
2

I'm currently working on a large scale project doing exactly what you describe. First off, there are so many different ways to do this, so what you really need is some general guidelines to get started, then we can dig a little deeper into details when some initial decisions are made, if you'd like. If you've already got the app deploying and running in two separate steps, but are just looking to combine those, I can definitely help. I'd just need to know how you're currently building/deploying. If you're just getting started on building your pipeline and need to set up the process from scratch, then read on:

First off you'll want to set up some kind of build server that will install your npm dependancies and run your webpack build. Most likely you'll want a separate webpack config that's just for your build server, this'll give you a build optimized for production or qa/staging environments. This config should split out vendor files that you won't update all the time, pull out seperate css files with extract text plugin and uglify the files. If you have an isomorphic React app, or are using es6 features not supported in your version of node, then you'll need a webpack build for your server code as well. This is really different from the hot reloading build you'll want to have on your local machine while you're actually coding the app. I'll be happy to show some examples if you'd like of our webpack config files for both local development and our CI build. You may also need a build.sh or makefile to do something with the compiled .js files that your webpack build creates, but that'll depend on your deployment which I'll cover later. You can run your production build locally as your getting your config just right and fire up the app from those files to test it's all working. Additionally, since you'll likely want to be able to automate all of this, you probably want to run your tests and linting right before you build your app, we run eslint and mocha/jdom to run our enzyme/expect specs as part of our build. Once that's all working nicely, you'll most likely want to set up a build server that can run your builds automatically. My team is using Jenkins for this, which is a little more work to set up, but it's free (aside from the ec2 box we run it on). There are also a ton of subscription based build/continuous integration servers, such as Travis and CodeShip. There's plenty of articles on the pros and cons of these different products and how to set them up. The bottom line is you'll want to have a build server that can pull down your code from source control, install npm deps, lint, test and build your app. If anything fails it should fail your build and if your build succeeds you'll have some sort of archive that you'll later deploy to an ec2 instance. In our shop we use a build.sh file to tarball up our build archive (basically a folder with our node server files as well as our minified client files, css files and any fonts or images needed to run the app) and upload it to an S3 bucket that we deploy from. We like this fairly old school method because the tarball will never change, so we have ultra reliable roll backs.

What you do with your build archive will depend on how you want to do deployments. We have a custom deployment system using puppet, but there are plenty of products that do this such as elastic beanstalk, that would be much easier to set up. You'll want some kind of process supervisor to actually run your node app, so unless you have a dev ops team that wants to build custom pipelines, using AWS built in features will probably be the easiest way to get started. As usual, there are so many ways to do this, but the basic principal is that you need something to download your build archive and run/supervise your node process. You also may want to be able to create and configure ec2 boxes on the fly (Puppet, chef, etc.), or even use containers (Docker) which allow you to move complete stacks around as single units. Using automation to create and configure servers is crucial if you need to scale your app, but it is complicated and may not be necessary for smaller projects. This is definitely an area where you can start simply and add complexity later on, as long as you have good long term goals and make sure to take the necessary step to prepare for future complexity.

All of this can get you pretty far in the weeds, so it's best to find the simplest thing that will serve your needs as you get started and then add complexity as real life situations demand so. I'll be happy to elaborate on any of these details if you provide a little more context about how big and well funded of a project your working on. If it's a little side project to learn the tech, I'd have very different advice then if you're trying to build an app that'll have a lot of traffic and/or complex features.

2 Comments

Hi Ben. Thank you so much for the insight. Would you be able to chat sometime?
Hi Jo Ko, I'd be happy to chat sometime. I'm actually not sure if there's a way to exchange info without putting my email address up on SO. Any ideas?
1

This could get 100 different answers and they could all end up being good ideas. First, you mention react + nodejs - keep in mind that these solve different tasks. React is going to be frontend and served out via static files. Nodejs is focused more around the server-side and would be the code that serves data. They can easily work together. You might use Express for the webserver (nodejs) to serve the HTML/React pages.

Unfortunately, I saw that you mentioned webpack, so you are going to have to 'build' your application with something - either via webpack, gulp, grunt, etc. This is where source control and build servers are great - but if you're new to it, it might be more complex than you need.

If you have just basic EC2 images as webservers and only 1-2, then the biggest hurdle is just pushing up your code. Something like https://deploybot.com/ could work as it can push your git repo down to multiple hosts via ftp, etc. If you wanted to get a bit fancier, you could look at something like Jenkins or some of the other items.

Docker is a great choice and if you are going to be dealing with multiple developers, server environments, deployments - it's worth the time. Otherwise, keep it simple and just get your code on the EC2 instance ;).

3 Comments

I currently have EC2 instance with Amazon Linux running and transferred my project (which contains both React/NodeJS/Express) onto the instance via SFTP. And for EC2's Security Groups, I opened a port for 3000 (protocol: tcp, source: 0.0.0.0/0), which is how my Express is defined as well. So I ran the Express within EC2 instance, but see it listening to port 3000 within the terminal. But once I hit the Public DNS with :3000, it says The site can't be reached - ec2...us-west-1.compute.amazonaws.com took too late to respond. What could be the issue?
Try adding something like this to your express app: app.get('/status', (req, res) => res.status(200).send('Service running')); then going to your public dns at url:3000/status. This should at least tell you if it's a code issue or server issue. Replace app which whatever variable you used for the express app.
I'm supposed to see the issue on the console, correct? But I don't see anything and the page times out again, saying it took too late to response. I currently have app.get('/', function(req, res) { res.sendFile(path.resolve('...'))}); as well. Is it supposed to go before or after? Tried it after.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.