Skip to main content

Deploying a Python Flask Application with AWS ECS

Hands-On Lab

 

Photo of

Training Architect

Length

01:00:00

Difficulty

Advanced

In this live AWS environment you will deploy a Python Flask application to the AWS Elastic Container Service. Flask is an unopinionated Python micro-framework that makes it easy to develop web applications. In this activity, you will download and configure a Flask application, create a Docker container image for that application, push that image to the Amazon Elastic Container Registry (ECR), and then use AWS ECS and Fargate to deploy your containerized application. Understanding how to work with and deploy containerized applications is an increasingly important skill for development roles and this lab will provide you an opportunity to start using these jump straight into those topics. By the end of the lab you will have used key container technologies and feel confident you understand some of the basics of AWS ECS.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Deploying a Python Flask Application with Amazon ECS

Note: For Windows PuTTY SSH specific information, see: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/putty.html?icmpid=docs_ec2_console

Introduction

Flask is an unopinionated Python micro-framework that makes it easy to develop web applications. In this hands-on lab, we will deploy a Python Flask application to the AWS Elastic Container Service (ECS). We will complete the following tasks:

  • Download and configure a Flask application
  • Create a Docker container image for the application
  • Push the image to ECR
  • Use ECS and Fargate to deploy the containerized application

Log In to the Environment

  1. Log in to the AWS Management Console using the credentials provided on the lab instructions page. Make sure you are using the us-east-1 region.
  2. On the lab instructions page, copy the public IP address of the EC2 instance.
  3. Open your terminal application, and run the following command:
    ssh cloud_user@<PUBLIC_IP>
  4. Type yes at the prompt.
  5. Enter your password when prompted.

We are now successfully logged in to the lab environment.

Create an Image and Push It to Amazon ECR

Create the Image

  1. In your terminal window, update the EC2 instance.

    sudo yum update -y
  2. Enter your password at the prompt.

  3. Install Docker.

    sudo yum install -y docker
  4. Start Docker.

    sudo service docker start
  5. Modify the users and groups on the EC2 instance.

    sudo usermod -a -G docker $USER
  6. Verify that the previous command was successfully applied.

    docker info

    >Note: If this command fails, log out of the instance and then log back in.

  7. Download the Flask application from GitHub.

    git clone https://github.com/linuxacademy/cda-2018-flask-app.git
  8. List the contents of the current directory to view the application.

    ls
  9. Change to the application directory.

    cd cda-2018-flask-app
  10. List the contents of the application directory.

    ls
  11. Check out the ecs-master branch.

    git checkout ecs-master
  12. List the available images.

    docker image ls
  13. Create a new image.

    docker build -t cda-flask-app .
  14. List the contents of the Dockerfile.

    cat Dockerfile
  15. Verify that the image was successfully built.

    docker image ls
  16. List the contents of the app.py file.

    cat app.py
  17. Try connecting to the application.

    curl localhost

    This command will fail because we need to reference the localhost of our EC2 instance.

  18. Run the following command to run the image on our EC2 instance:

    docker run -p 80:80 cda-flask-app
  19. Open a new terminal window, and log in to the EC2 instance.

    ssh cloud_user@<PUBLIC_IP>
  20. Enter your password at the prompt.

  21. Run curl on the localhost.

    curl localhost
  22. Once you've finished testing our configuration, close your second terminal window.

  23. In your original terminal window, press Ctrl+C to cancel the previous command.

Push the Image to Amazon ECR

  1. Create a new repository.
    aws ecr create-repository --repository-name cda-penguin-app
  2. Copy the value forrepositoryUri from the output, and save it somewhere convenient — we'll need it later.
  3. Log in to the ECR repository.
    aws ecr get-login --region us-east-1 --no-include-email
  4. Copy the output of this command, then paste it into the command line and run it.
  5. List the currently available images.
    docker image ls
  6. Associate the cda-flask-app image with the repository URI we copied earlier.
    docker tag cda-flask-app:latest <REPOSITORY_URI>
  7. Push the image to Amazon ECR.
    docker push <REPOSITORY_URI>
  8. Switch to your AWS Management Console window, and navigate to the Elastic Container Service.
  9. Click Repositories in the left sidebar.
  10. The cda-penguin-app repository should appear in the list.
  11. Close your terminal window.

Deploy the Containerized Application to ECS

  1. On the Repositories page of the AWS Management Console, click Clusters in the left sidebar.
  2. Click Get Started.
  3. Under the Container definition header, click Configure in the custom box.
  4. In the Edit container menu, enter the following settings:
    • Container name: penguin-container
    • Image: <REPOSITORY_URI>
    • Memory Limits (MiB): Hard limit | 128
    • Port mappings: 80
  5. Click Update.
  6. Under the Task definition header, click Edit, and enter the following settings:
    • Task definition name: penguin-task-definition
    • Task execution role: ecsTaskExecutionRole >Note: Be sure to click into the Task execution role dropdown and select the ecsTaskExecutionRole option, even if it already appears by default. Otherwise, you may get an error.
  7. Click Save.
  8. Click Next.
  9. Under the Define your service header, leave the default None option selected for Load balancer type.
  10. Click Next.
  11. Under the Configure your cluster header, type "penguin-default-cluster" for the Cluster name.
  12. Click Next.
  13. Click Create.
  14. When your resources are finished creating, click View service.
  15. Click the Tasks tab at the bottom of the page.
  16. Click the task name link to open it.
  17. When the status of the container is RUNNING, open the ENI Id link under the Network header in a new browser tab.
  18. Copy the IPv4 Public IP address for the application, and paste it in a new browser tab.
  19. You should see our penguin website.

Conclusion

Congratulations, you've successfully completed this hands-on lab!