Back to all articles
  • Flask
  • AWS ECS
  • Fargate
  • NGINX
  • Docker

How to Deploy a Flask Application Using NGINX on AWS ECS — Part 3

Create the ECS cluster, task definition, service, security group, and load-balanced deployment that completes the Flask and NGINX series.

Filipe Galo6 min read

AWS ECS cluster configuration

In the previous part, we successfully created our VPC Architecture. Now it is time to start building our AWS ECS Cluster.

Steps:

  1. Create AWS ECS Cluster

  2. Create AWS ECS Task Definition

  3. Configure AWS ECS Service Security Group

  4. Create AWS ECS Service

  5. Confirm if our application is running

  6. Create AWS ECS Cluster


Navigate to the Elastic Container Service page in AWS Management Console, and disable the New ECS Experience:

Click on Clusters in the left sidebar and then click on Create Cluster and select as cluster template Networking only and click on Next step:

How to Deploy a Flask Application Using NGINX on AWS ECS — Part 3 tutorial screenshot 2

Name cluster as project-cluster and click on Create:

How to Deploy a Flask Application Using NGINX on AWS ECS — Part 3 tutorial screenshot 3

  1. Create Task Definition

In the left sidebar, click on Task Definitions and then click on Create new Task Definition and select FARGATE as Launch type compatibility and click Next step:

How to Deploy a Flask Application Using NGINX on AWS ECS — Part 3 tutorial screenshot 4

Give the name project-td to your Task Definition and select Linux as the Operating system family:

How to Deploy a Flask Application Using NGINX on AWS ECS — Part 3 tutorial screenshot 5

As Task size, select 0.5GB as memory and 0.25 as CPU:

How to Deploy a Flask Application Using NGINX on AWS ECS — Part 3 tutorial screenshot 6

Open another tab on your AWS ECR service page and get the URI for each image created, in this case, nginx and project.

Back to the Add container page, name the first container name nginx, andpost the URI for your nginx image in the image section. In Port Mappings, add the 80 port:

How to Deploy a Flask Application Using NGINX on AWS ECS — Part 3 tutorial screenshot 7

Click on Add.

Repeat the process but now for our Flask Application (project):

How to Deploy a Flask Application Using NGINX on AWS ECS — Part 3 tutorial screenshot 8

Click on Add.

To finish the creation of our Task Definition click on Create.

3. Configure AWS ECS Service Security Group

Navigate to the VPC page in AWS Management Console, and click on Security Groups. Click on Create security group.

Give it the name of project-service-sg, and on the Inbound rules section, select All TCP as the type, and in the Source, select the project-alb-sg

How to Deploy a Flask Application Using NGINX on AWS ECS — Part 3 tutorial screenshot 9

  1. Create AWS ECS Service

Go back to the Clusters page and click on your cluster:

How to Deploy a Flask Application Using NGINX on AWS ECS — Part 3 tutorial screenshot 10

In the Service tab click on Create.

Select FARGATE as Launch type, and as Task Definition, select the one we created in the previous step, name your service project-service, and the Number of tasks set it to 2.

How to Deploy a Flask Application Using NGINX on AWS ECS — Part 3 tutorial screenshot 11

Click on Next Step.

Choose our VPC as the Cluster VPC, and as subnets, select the project-private-rt-a and project-private-rt-b

How to Deploy a Flask Application Using NGINX on AWS ECS — Part 3 tutorial screenshot 12

As Security Group, click on Edit, choose the one created in Step 3, and then Click on Save.

How to Deploy a Flask Application Using NGINX on AWS ECS — Part 3 tutorial screenshot 13

In the Load balancing section, select Application Load Balancer as the type. The Load balancer name should be the one we created in the last part:

How to Deploy a Flask Application Using NGINX on AWS ECS — Part 3 tutorial screenshot 14

In the Container to load balance section, click on Add to load balancer:

How to Deploy a Flask Application Using NGINX on AWS ECS — Part 3 tutorial screenshot 15

As Target group name, select project-alb-tg:

How to Deploy a Flask Application Using NGINX on AWS ECS — Part 3 tutorial screenshot 16

Click on Next step and Next step, and you should see a review of what we just did:

How to Deploy a Flask Application Using NGINX on AWS ECS — Part 3 tutorial screenshot 17How to Deploy a Flask Application Using NGINX on AWS ECS — Part 3 tutorial screenshot 18

To finalize, click on Create Service.

Go back to the Cluster page, and you should see two tasks running:

How to Deploy a Flask Application Using NGINX on AWS ECS — Part 3 tutorial screenshot 19

  1. Confirm if our application is running

Please navigate to the EC2 page in AWS Management Console, click on Load Balancers, andcopy the DNS name of our load balancer.

How to Deploy a Flask Application Using NGINX on AWS ECS — Part 3 tutorial screenshot 20

Open a new tab in your process and paste the URL you copied.

How to Deploy a Flask Application Using NGINX on AWS ECS — Part 3 tutorial screenshot 21

And that’s it; as you can see, you are accessing your Flask application directly from the Internet while NGINX is redirecting the requests internally inside the AWS ECS Service.

And since we deployed our Service with two replicas, we have each one in different subnets.

Now go back to your Cluster page, open the two Tasks in different browser tabs**, click** on the Logs tab, and select the nginx container.

Refresh your application browser tab many times, and you will see that our Load Balancer is working correctly and redirecting the traffic to our Tasks.

How to Deploy a Flask Application Using NGINX on AWS ECS — Part 3 tutorial screenshot 22

That is it from me; thanks a lot for reading!

If you enjoyed my content and my three-part series on “How to deploy a Flask application using NGINX on AWS ECS,” or if you have any questions, feel free to connect with me on Twitter https://twitter.com/AutomationForge

Part 1: /blog/deploy-flask-nginx-aws-ecs-part-1

Part 2: /blog/deploy-flask-nginx-aws-ecs-part-2