i tried deploying flask application on ecs using fargate

i tried deploying flask application on ecs using fargate

Clock Icon2021.09.03

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Overview

Amazon ECR : Amazon Elastic Container Registry (Amazon ECR) is a fully managed container registry that makes it easy to store, manage, share, and deploy your container images and artifacts anywhere.

  1. Reduce your effort with a fully managed registry

  2. Securely share and download container images

  3. Provide fast and highly available access

  4. Simplify your deployment workflow

Amazon ECS : Amazon Elastic Container Service (Amazon ECS) is a fully managed container orchestration service that helps you easily deploy, manage, and scale containerized applications

  1. Use your existing tools

2. No control plane or nodes to manage

3. lower your computer costs

4. Meet security and regulatory requirements

AWS Fargate : AWS Fargate is a serverless, pay-as-you-go compute engine that lets you focus on building applications without managing servers. AWS Fargate is compatible with both Amazon Elastic Container Service (ECS) and Amazon Elastic Kubernetes Service (EKS).

  1. Deploy and manage your applications, not infrastructure. Fargate removes the operational overhead of scaling, patching, securing, and managing servers.

2. Monitor your applications via built-in integrations with AWS services like Amazon CloudWatch Container Insights. Gather metrics and logs with third-party tools.

3. Improve security through workload isolation by design. ECS tasks and EKS pods run in their own dedicated runtime environment.

4. Only pay for what you use. Fargate scales the compute to closely match your specified resource requirements. With Fargate, there is no over-provisioning and paying for additional servers.

Hands on

Step 1: Create Repository

in repository we are going to push our docker image

  1. go to ecr(Elastic container ragistory) i helps us to deploy,manage and control docker container images

  2. create 2 repository one for flask app and one for ngnix

click on create repository ,

give  repository name: flaskapp

2) tag imutability: disable (if you want the image not to be overwritten if pushed with same name then enable it)

3) Scan on push : Disable( if you want to check for any vulnerablity in image then enable it) click on create repository

*follow it again with repository name :webserver

Step 2: Create docker flask image and nginix webserver image in ec2

command as followed i am using a pre build repository for source code of flask and docker


git clone https://github.com/srcecde/aws-tutorial-code.git

run following command in ubuntu server after cloning the repo

//this command will install and build your docker images


sudo apt-get install docker
<p class="p1">sudo apt-get install docker-compose -y</p>
<p class="p1">sudo docker-compose up --build</p>

you can check that the containers are running properly or not by pasting ec2 public ip on browser

you can check total images by


sudo docker images

installing aws cli version 2 on ec2 instance


curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

 

Step 3: Create role for ec2 instance to give fullaccess to ec2 so that it can write image to ecr

permission: AmazonEC2ContainerRegistoryFullAccess

Step 4: push docker image to ecr

select the ecr repository from aws console and click on view push command you will find the command run in your ec 2 server

for eg:

below command will login to ecr


aws ecr get-login-password --region ap-south-1 | sudo docker login --username AWS --password-stdin 123456789.dkr.ecr.ap-south-1.amazonaws.com



if you got security error run



sudo apt install gnupg2 pass

gnupg2: it helps in secure communication

pass: it is password manager for linux

below command will help the image to tag with the name of ecr repo name we add local image name in first if its different(we can find name in "sudo docker images") thene ecr image name please edit the name

<

p class="p1">

sudo docker tag ecs-fargate-flask_flaskapp:latest 123456789.dkr.ecr.ap-south-1.amazonaws.com/flaskapp:latest</span></p>

we will push the taged image with bellow command


docker push <span class="s1">123456789</span>.dkr.ecr.ap-south-1.amazonaws.com/flaskapp:latest

*follow the same commands for webserver image also

Step 5: create task defination

from ecs click on task defination

click on fargate

give task defination name

  1. provision task memory(gb):
  2. Task CPU (vCPU):

Add conatiner:

add container for flaskapp name(flaskapp) and image(we can get url from ecr, copy the url of flaskapp and paste in image add :latest at end and click add

once again click on add container(for webserver)

add name, image url and for portmaping add 80

also add STARTUP DEPENDENCY ORDERING add flaskapp in container name and Start in condition

//it will start flaskapp before webserver

click create

Step 6: create a cluster

  1. select Cluster template: networking only
  2.  configuring Cluster : Cluster name: cluster1 and create

Step 7: create a service

  1. click on cluster1 from ecs
  2. from services click create
  3. in configure service :
    1. launch type:Fargate
    2. task defination: DevelopersIO
    3. number of tasks:1
  4. in configure network :
    1. cluster VPC: degault vpc
    2. Subnets: default subnet
    3. Security groups: create Security group with port 80 and 22 open
  5. click next with all other field as deafult option
  6. click create service

Summary

successfully deployed a flask app and NGINX using aws services. i hope this article will help someone.

This is aayush from india

 

 

 

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.