/aws-deployment-tutorial

Tutorial for deploying simple flask application to ECS

Primary LanguagePython

Step-by-Step Guide: Dockerizing and Deploying Flask Application on AWS ECS with Load Balancer

Prerequisites:

  1. You should have an AWS account.
  2. You should have docker installed.

Download and Configure AWS CLI

  1. You may download your required AWS CLI from this link

If you do not have AWS Access Key and Secret Key, you can create one by following the below steps shown in this video

  1. After installing AWS CLI, you can configure it by running the following command in your terminal:
aws configure
  1. You will be prompted to enter your AWS Access Key, Secret Key, Default region name and Default output format. You can get your default region name from this link

Creating docker image and pushing it to AWS ECR

  1. Clone this repository to get the dockerfile and the flask application.
  2. Now open the ECR console and click on Create repository. You can also find the ECR console by searching for ECR in the search bar.
  3. Enter the repository name and click on Create repository. Now you will be able to see the repository you just created. repository_created
  4. Click on the repository name, and on the top right you will be able to see the commands to push your docker image to ECR. Simply run the commands in your terminal.
  5. After running the commands, you will be able to see the image pushed to ECR. image_pushed

Creating Security Group

Before creating the task definition, let's create a security group.
Since we will be using a load balancer, we need to create a security group for the load balancer.

  1. Search for EC2 in the search bar and click on Security Groups (EC2 feature).
  2. Click on Create Security Group.
  3. Enter the name and description of the security group and click on Create. names
  4. Now add Inbound rules to the security group(change the port number according to your requirement). inbound_rule
  5. Now click on Create Security Group.

We will also be requiring a security group for the EC2 instance. You can create a security group for the EC2 instance as follows:

  1. Click on Create Security Group.
  2. Enter the name and description of the security group and click on Create. names
  3. Now add Inbound rules to the security group. Note that this time we will be accepting ALL TCP traffic from the security group of the load balancer by selecting LoadBalancerSecurityGroup from the Source dropdown. inbound_rule
  4. Now click on Create Security Group.

Creating a new Cluster

  1. Search for ECS in the search bar and click on Clusters.
  2. Click on Create Cluster.
  3. Type the cluster name(eg: flask-app-cluster) and click on Create.

You will be able to see the cluster you just created. cluster_created

Creating a Task Definition

  1. Search for ECS in the search bar and click on Task Definitions.
  2. Click on Create new Task Definition.
  3. Add a name(eg: flask-task-definition).
  4. In the Infrastructure requirements section, select Task size as per your requirement.
  5. In the Container section, add the container name(flask-app) and the image URI you pushed to ECR.
  6. Click on Add port mapping and add the port number(eg: 5000) and the protocol. port_mapping
  7. Add any environment variables if required.
  8. Click on Create.

You will be able to see the task definition you just created. task_definition_created

Deploying the Task Definition

  1. Search for ECS in the search bar and click on Clusters.
  2. Click on the cluster you created.
  3. Click on Create.
  4. In the Deployment Configuration section, select just created task definition in the Family dropdown.
  5. Add Service name(eg: flask-app-service).
  6. Add the number of tasks you want to run in the Desired task(eg: 3).
  7. Under Networking section, select Use an existing security group remove the default security group and add the EC2InstanceSecurityGroup you created.
    NetworkingSG
  8. Under the Load balancer section, choose Application Load Balancer as the Load balancer type. And create a new load balancer using the following details: loadbalancer
  9. Add listeners and target groups as below: listenertargetgroup
  10. Click on Create. It may take a few minutes for the service to be deployed.

Setting up security group for the load balancer:

  1. Search for EC2 in the search bar and click on Load Balancers.
  2. Click on the load balancer you just created(eg: flask-app-loab-balancer).
  3. Click on the Security tab and click on Edit.
  4. Remove any default security group and add the LoadBalancerSecurityGroup you created. loadbalancersgchange

You will be able to see the service you just created. deployed_service

Accessing the application

  1. Click on the load balancer you created.
  2. Copy the DNS name and paste it in your browser(with the port number).
  3. You will be able to see the application running. result

Cleaning up

A. Delete the service:

  1. Search for ECS in the search bar and click on Clusters.
  2. Click on the cluster you created.
  3. Under the services, select the service you created. And click on Delete service. DeleteService

B. Delete the load balancer:

  1. Search for EC2 in the search bar and click on Load Balancers in the navigation pane.
  2. Select the load balancer you created and click on Delete from Actions dropdown.

C. Delete the task definition:

  1. Search for ECS in the search bar and click on Task Definitions.
  2. Select the task definition you created and click on Deregister from Actions dropdown.

D. Delete the cluster:

  1. Search for ECS in the search bar and click on Clusters.
  2. Select the cluster you created and click on Delete cluster.

E. Delete the security groups:

  1. Search for EC2 in the search bar and click on Security Groups.
  2. Select the security groups you created and click on Delete security group from Actions dropdown.

F. Delete the repository:

  1. Open the ECR console.
  2. Select the repository you created and click on Delete.

G. Delete the image from ECR:

  1. Open the ECR console.
  2. Select the repository you created and click on Delete.

References