How to deploy a React + NodeJS Express application to AWS?

前端 未结 2 1103
孤独总比滥情好
孤独总比滥情好 2020-12-04 05:07

I have a React + Webpack/Babel + Node/Express application and I want to deploy it on AWS.

Would I have to deploy React and Node/Express separately? Or could they be

2条回答
  •  再見小時候
    2020-12-04 05:36

    Basic Concepts

    To deploy your app hassle free, you need to learn about three concepts: Microservices, containers, and process managers. I will discuss them with a bit more details and few links to get you started:

    Microservices

    Microservices is an architecture that allows you to divide your app into smaller services. This has multiple benefits: 1- The services are easily testable. 2- The services are replaceable. 3- The services can scale separately.

    Containerization

    Almost every useful app has at least dozens of dependencies. You can install dependencies on the target machines, but most certainly you'll face few challenges. Programs like Docker allow you to create a container for your app and deploy that container on the cloud. (Regardless of the cloud provider) Learn more...

    Process Managers

    Process managers ensure that your app is running smoothly and all parts are healthy. If your app crashes, it can easily restart the app.

    1. Deploying a serverless NodeJS / React Application

    Note: This approach does not work if you are doing server-rendering with ReactJS. Go to the next option.

    You can simply build your app and deploy it to a static S3 website. This option works if you use microservices architecture to separate your API from your react app.

    Creating a static website in S3 is really simple:

    1. Create a bucket in S3 with the exact name of the website. Example: blog.stackoverflow.com.
    2. Enable static hosting
    3. Create an A record in Route 53 and connect it to the bucket you created.

    For more information check AWS handy documentation.

    2. Deploying a NodeJS application into EC2

    You can launch different EC2 instances for every microservice. (API, React app, etc.) You need to use a process manager such as PM2 to ensure your app is running smoothly.

    Continuous Delivery (Automating deployment)

    To create an automatic deployment, I prefer to use Terraform in combination with Ansible. Terraform is very declarative. You describe how the cloud infrastructure should look like and Terraform build it for you.

    Ansible, on the other hand, is very procedural and is perfect for provisioning a new server.

    Error handling and reporting: Sentry

    Ideally, you should have unit tests to prevent shipping buggy code to the production. (Use Jest with supertest, Enzyme for shallow rendering). But the world is imperfect, and it is good to receive any potential bugs that happen on the client. Enter Sentry

提交回复
热议问题