I found two methods to deploy the app using eb(elastic beanstalk):
In this method, we use eb cli to deploy the app
-
First, you need to install the eb cli. Hit this link and install the cli based on your operating system.
-
Second, we need to configure this cli. Before configure it, we need to create a new user and create access keys for that user.
- Hit this link and follow the instructions to create Administrator IAM User and Group.
- Then we want to give this new user an AWSElasticBeanstalkFullAcces. To do so, follow the instruction in this link.
- After creating the user and give him access to eb, we need to configure the cli using the access keys for that user. To do so, follow the instruction in this link and go to
Access key ID and secret access key
section and follow the instructions to get the access keys. - So far, we have created the user, giving him access to eb, and getting access keys for him. Now to configure the eb cli, write this command
aws configure
, This will ask you to add the access keys that you've just created and saved, just copy and paste the value of these keys.
-
Create your client folder and then use create-react-app to create your react project. So type
create-react-app client
in your terminal. -
Create your express server.
-
Build the React app by entering the client folder
cd client
, and then execute this commandnpm run build
-
Edit your server file and add useStatic middleware to use the build folder by adding this line in server.js
app.use(express.static(path.join(\_\_dirname, 'client', 'build')));
-
Don't forget to add
proxy
in your client side package.json file to send request to the server and make the client side and server side work in the same origin. -
Now, write this command
eb init
(make sure you are in the root folder of your project). This command will ask you some questions about your IAM access information, location and application preference. After finishing, you will find a new folder created named.elasticbeanstalk
, usually we don't change anything in this file. -
Eb usually starts the app by executing app.js file, then server.js file, then execute
npm i
. Whenever you have something different(like different command to execute), you need to tell eb about that. How? by adding a new config file that telling the eb what to execute.- Create new folder in the root of your project and name it
.ebextensions
. This folder contains file to configure your eb application. For example, to configure the command that the eb will execute, add a new file inside.ebextensions
folder and name itnodecommand.config
. Add the following code inside this file. You can replaceNodeCommand
with the another command in case you have to do so.
- Create new folder in the root of your project and name it
option_settings:
aws:elasticbeanstalk:container:nodejs:
NodeCommand: "npm start"
Note: this step is not necessary in this project because I have a file named server.js
witch will be executed by default by the eb as I said before
- Add, commit, and push your code.
- Back to step 8, when we wrote
eb init
and answer the questions, this will create new eb application for you. To open your dashboard and see all your applications and environments, writeeb console
in the cli. The eb also will create environment for your application. - Write
eb deploy [your Environment name]
, this will deploy your app to eb. In case you don't know which environment you have to use, writeeb console
to see your eb dashboard where you can see all your applications and all environments. - Now write
eb open
to open your app and see the magic 🎉🎉 - Surprise!!
502 Bad Gateway
error! Why? This happened because I deploy my app on port 9000, but The default nginx configuration forwards traffic to an upstream server named nodejs at 127.0.0.1:8081. To solve this problem, you have two solutions:- Just change your server port to 8081.
- Add new config file that tell eb to change the default port to the port you used in your server(9000 in my case).
- Under
.ebextensions
folder, create new file and name itproxy.config
- Inside this fil, add the following lines:
files:
/etc/nginx/conf.d/proxy.conf:
mode: "000644"
owner: root
group: root
content: |
upstream nodejs {
server 127.0.0.1:9000;
keepalive 256;
}
Note: you need to change 9000 and put your server port
- Add, commit, push, and deploy your app again, then navigate to the website again and refresh to see the magic
- Surprise again 😜 react files are not served! Why? This is because we need to tell the eb from where he can get the static files. Now after deploying, static files are not in client>build folder. They have a new path. We need to add a new config file to tell the eb about the new path which is
/var/app/current/client/build/static;
- Under
.ebextensions
folder, create new file and name itstaticfiles.config
. - Add the following code in this file
- Under
option_settings:
aws:elasticbeanstalk:container:nodejs:staticfiles:
/static: /var/app/current/client/build/static;
- Then add, commit, and push you code.
- If you deploy the app, and navigate to the website you will not get react files served!!! WHY? this is because we used
create-react-app
to create the react app which add ignore thebuild
folder when we push the code to gitHub. So, go the.gitignore
file in the react app and remove the line that ignore thebuild
folder. - Add, commit, push, and deploy your app to see your app deployed 💥💥💥