Mubbashir Mustafa
Posted on April 4, 2020
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
Posted on April 4, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.