It's assumed that you have already containerized your React app. If you haven't then read this first
Usually, you should deploy your React app using AWS S3 (with Cloudfront) or AWS Amplify. But if your use case is advanced and you want to deploy containerized React app then you could use AWS ECS.
Amazon Elastic Container Service (Amazon ECS) is a service provided by AWS to orchestrate containers and deploy your containerized applications.
Step1: Upload your image to ECR
Amazon Elastic Container Registry (ECR) is a service where you store your container images (like DockerHub). Consider it as npmjs of containers instead of JS packages.
Sign in to your AWS console and head over to AWS ECR.
Click on "Create Repository" and add a new repository. Add whatever name you would like, for the sake of demo I am using "my-app".
Once the repository is created, it will take you back to the repositories list. Select the newly created repository and then click on the "View push commands" button.
Now we need to use these commands one by one to build, tag and push our container image. But before proceeding make sure you have the latest version of AWS CLI installed.
Use the following command to check that AWS CLI is correctly installed on your system.
aws --version
It would output something like this. Your version could be different, it depends when you are reading this article.
Next, we need to create 'Access Keys'. For that, go to "My Security Credentials" from the dropdown with your username.
Click on "Create New Access Key", it will create a new key for you.
Copy "Access Key ID" and "Secret Access Key". We will need it later on.
Now go back to your terminal and enter the following command
aws configure
It will ask for "Access Key ID" and "Secret Access Key", provide them one by one (copied in the previous step). You can skip the next two questions (default region and default output format).
Once aws cli has been configured, now we can run the push commands. Open up the terminal and make sure you are inside your React app's directory. Run each command shown in the popup opened up by clicking the "View push commands" button. Please note that the commands in the screenshot could be different than what you see in your popup and it's totally fine.
*If your 'Dockerfile' is named something other than 'Dockerfile', you will have to specify it using -f
flag (see below).
Step 2: Deploy on ECS
Once our container image has been pushed, we are now ready to use ECS and all the awesomeness it has to offer.
Head over to Amazon Elastic Container Service (Amazon ECS) and create a cluster.
Select "Network Only" and click "Next Step".
Give whatever name you would like (but remember it), select "create vpc", click "create".
*Note: You can also choose an existing VPC.
Once the Cluster has been created, select 'View Cluster'.
Select "Task Definitions" from the left sidebar.
Click "Create new Task Definition"
Select "Fargate", click "Next Step"
Enter Task Definition Name, select "ecsTaskExecutionRule" in the "Task Role" and "Task Execution Role" fields. Select "0.5GB RAM" and "0.25 vCPU". Click "Add container".
Now we need to add a container of the image we pushed to ECR earlier. Give a name to container and copy-paste the image URI (can be copied from ECR repositories list). Enter "80" in the port mapping field. Click "Add".
Once the container is added, click "Create" to finish Task creation.
Once the Task is created, you can click on "View task definition"
Now we need to create a service to run the task within our container. But before that, we need to create an Elastic Load Balancer.
ELB
We need to create an Elastic Load Balancer (ELB) so that we can access the react app form the browser (and later on attach to a domain). To do that, head over to EC2.
Select "Load Balancer" from the bottom left.
Click on "Create Load Balancer"
Select "HTTP/HTTPS" and click "Create"
Give a name to your Load balancer. Select VPC from the dropdown (it should be the one that was created by your ECS cluster earlier). You also have to select both of the subnets within the VPC. Click "Next: Configure Security Settings" and ignore the warning (we will add HTTPS later on) and click "Next: Configure Security Groups".
Click on "Create a new security group" and hit "Next: Configure Routing".
Give a name to your target group (a target group is a group of resources where ELB should send requests to). Select "IP" and click "Next: Register Targets".
In "Register Targets" leave everything as it is, click "Next: Review". Review the changes and click "Create". Your ELB is created now. Copy the "DNS Name", as this is going to be the URL to access your container from the browser.
Once the ELB has been created, we can now create "Service" to run the "task" created within our "cluster".
Back to ECS
Go inside the cluster we have created. In the "Services" tab, click "Create".
Select "Fargate" as the launch type. Select the task definition we created above (my-app-task in my case), select revision as 1 (latest). Select "LATEST" in platform version, choose the cluster we created in "Cluster". Give a name to your service. Enter "1" in the "Number of tasks" field. Click "Next step".
In "Cluster VPC", select the VPC that was created by our cluster. Select both of the subnets within the VPC. In the Load balancer type, select "Application Load Balancer". Select the ELB we created earlier in the "Load balancer name" field. Select the container we created in "Container to load balance" section and click "Add to load balance"
Once you click on "Add to load balance", the section will be expanded.
From "target group name", select the "target group" we created while creating the ELB earlier and it will fill the rest of the fields for this section.
Once the "Container to load balance" section's configuration is completed. Click "Next step".
In "Auto scaling" section leave everything as it is and click "Next step". Review the settings and click "Create service". Once the service is created, you will be taken back to the services list page. Once the service we just created has an "Active" status, you can go to the browser and use the DNS Name of ELB to access the container.
To get DNS Name of ELB, select the load balancer we created earlier and the DNS Name would be shown in the description below.
Use the address in the browser to access your react app.
Next: Attach domain to ELB
Let's connect:
Linkedin: https://www.linkedin.com/in/mubbashir10/
Twitter: https://twitter.com/mubbashir100
Top comments (13)
Great post, thanks for sharing....
I dockerized my react app, and can ONLY run the dockerized image with interactive mode (-it) specified in the docker run. Unfortunately, I don't see a way to do the same to add the "-it" to the docker image in the ECS Fargate's task definition.
Is there a way to aviod to have to run in the interactive mode? or a way to specify an interactive mode in the Fargat task definition?
Appreciate if you can advise...
Hi, thanks for stopping by. Let's take a step back, could u please mention why you need to run your production app in the interactive mode? (it will help me propose a possible solution) - thanks.
Thanks for pointing out "why must I run the container in interactive mode?"... instead of looking at how to run container with -it in AWS, I seems managed to get around the interactive mode requirement by adding ENV CI=true to the dockerfile... i am not sure if that fixed the -it requirement, but i can run it without interactive mode now.
Hi Mubbashir,
If I run without the interactive mode with below cli
docker run --rm --name=my-react-app --network=mynetwork -p 3000:3000 my-react-app-image
I am getting the server stopped at "Starting the development server..." as shown below:
[HPM] Proxy created: /auth/google -> my-node-server:5000
[HPM] Proxy created: /api/* -> my-node-server:5000
ℹ 「wds」: Project is running at 172.18.0.2/
ℹ 「wds」: webpack output is served from
ℹ 「wds」: Content not from webpack is served from /usr/src/app/public
ℹ 「wds」: 404s will fallback to /
Starting the development server...
my dockerfile is simple:
pull official base image
FROM node:12-slim
set working directory
WORKDIR /usr/src/app
install app dependencies
COPY package*.json ./
RUN npm install
RUN npm install react-scripts@3.4.1 -g
add app
COPY . ./
start app
CMD ["npm", "start"]
But if I run it in my node js console using "npm start" i can start my dev without issue...
Thanks,
Ah, so you are trying to use it for development purposes. When we deploy on ECS, we do it for production. For production, you need to use a multistage docker (first build the react app, second copy the built files and serve using Nginx - no nodejs server involved).
For development, you don't need to use ECS. That's something that you will be doing on your local machine, and it's totally fine to use
-it
for it. You may also want to attach volume to your code directory if you are developing via docker :)Can't seem to get this process working anymore. Basically, once I get to the end and I open up the load balancer url in the browser it shows a 503 Error. I believe AWS has changed the "Create a security group" process and you now have to set up the inbound rules. I'm thinking my error could be somewhere at that part of the process. Have you setup a pipeline recently and encountered a similar issue?
Hi! Thank you for your article!
Images after text - "Next: Configure Security Groups" and text - "Next: Configure Routing" are the same. It's not clear what settings you used in step - 3. Configure Security Groups.
Oh, you are right. I will update the images. The image for "Security Group Configuration" is missing.
Whats the benefit of hosting a react app on fargate as compared to S3? React files are built into static files which don't require a run-time. So, why would you put it on fargate?
Both are two different models of deployments. Instead of S3 vs ECS, you probably want to know about the benefits of containerized applications. Once you have the idea of why should we containerize our application then ECS comes into play. And then your question would be something like why should we use ECS instead of beanstalk (or other container services)?
So at this point, you need to understand the benefits of containerization in general.
Could you elaborate your answer a bit more? It would really be helpful if you could point out some of the reasons why we want to containerize a React app, or any other SPA. Thanks in adavance.
Hi, thank you first for your time you spent helping us with this article, it's really helpfull.
I did the first step fine, BUT after finishing the second step, the react app not accessible from the browser using DNS Name as explained in last thing in the step 2.
thanks in advance to anyone can help.
Hi Mubbashir Mustafa, very good post. Why would not split front end of Backend using s3 for static files and ecs for react backend? In that way you can have separate development cycles.