Deploy your Python app with AWS Fargate — A Tutorial | by Daniel Deutsch | Towards Data Science

About this article
What AWS infrastructure type should I use
AWS account
Set up AWS credentials
Set up credentials with users and roles in IAM
Create a Repository in Elastic Container Registry (ECR)
Configure a cluster
Create task to run Docker container in AWS
Execute Task
Expose defined port within webpage
Watch your app online
Bonus: Investigate Errors
Disclaimer
About
In this article, I will go over the steps to launch a sample web app within AWS. In my last articles, I talked about creating an own web app with python and also how to deploy it with AWS Lambda:
Develop and sell a machine learning app
Develop and sell a python app
Now I want to test out another infrastructure type. This article assumes that you are familiar with building and containerizing a web app. In a python setting, I always use Flask and Docker (see previous articles). However, this article focuses on the “devops” perspective of deploying such an app. There are still more aspects that I will cover in the following articles, like setting up a proper domain name and use additional services like load balancing.
I use AWS because I already got into it in my last projects and find it well documented and intuitive to handle even though there are so many options to choose from.
As you can see from the table of contents the main parts of this article will be:
set up and connect local development with AWS credentials
create a repository in an Elastic Container Registry (AWS ECR)
create cluster and task to run a docker container in the cloud
Here is a fantastic article I want to recommend: https://medium.com/thundra/getting-it-right-between-ec2-fargate-and-lambda-bb42220b8c79
He summarized it perfectly in my opinion. So what I want is to deploy my container, but do not want to get any deeper into the infrastructure. That’s why I go with AWS Fargate.
First, you need to create an AWS account. They will guide you through the process and there are no difficulties to be expected.
If you having trouble in the credentials section, check out this article for reference.
First, you need to get an AWS access key id and access key
I break it down as simple as possible:
At the IAM Dashboard, click the “Add User” button.Give the user a name and select the Access Type for Programmatic access.In the Set permissions screen, select the the permission for AmazonEC2ContainerRegistryReadOnly, AmazonEC2ContainerRegistryFullAccess, AmazonEC2ContainerRegistryPowerUser.Tags are optional.Review the user details.Copy and save the user’s keys in a secure location as they will be needed in later stages.
At the IAM Dashboard, click the “Add User” button.
Give the user a name and select the Access Type for Programmatic access.
In the Set permissions screen, select the the permission for AmazonEC2ContainerRegistryReadOnly, AmazonEC2ContainerRegistryFullAccess, AmazonEC2ContainerRegistryPowerUser.
Tags are optional.
Review the user details.
Copy and save the user’s keys in a secure location as they will be needed in later stages.
Create a .aws/credentials folder in your root with
mkdir ~/.awscode ~/.aws/credentials
and paste your credentials from AWS
[dev]aws_access_key_id = YOUR_KEYaws_secret_access_key = YOUR_KEY
Same with the config
code ~/.aws/config[default]region = YOUR_REGION (eg. eu-central-1)
Note that code is for opening a folder with vscode, my editor of choice.
Save the AWS access key id and secret access key assigned to the User you created in the file ~/.aws/credentials. Note the .aws/ directory needs to be in your home directory and the credentials file has no file extension.
Search for ECR and then you will get to the page where you can create your new repository.
Afterward, you have your new repo visible:
Next, we want to get the push commands:
Just follow those instructions to set up your local repo for the AWS connection. Be aware that you are at the root of your project so everything works out smoothly.
Make sure that you have installed the AWS CLI or just follow the installing according to the official docs.
If you have problems here, just google the error message. Often there is an issue with policies and your user. This SO question helped me: https://stackoverflow.com/questions/38587325/aws-ecr-getauthorizationtoken
After executing the push commands you will have the image online:
Next, go to “clusters” (under Amazon ECS not EKS!) in the menu.
Choose “Network only” template
add a name
create
In this section, I sometimes got buggy error messages. If this happens to you you can simply re-create the cluster. If everything is set up correctly it should work.
Go to “task definitions” and create a new task with Fargate compatibility.
then
add a name
specify task size (I use the smallest options)
add container
add the name of your container
copy the image address you got from the CLI command where the image is pushed
add the exposed port (specified in your Dockerfile)
Then create the task
Now you should have defined a task within Fargate and connected your container.
Now you can run your defined task:
Select Fargate as lunch type and add the provided dropdown options within the form (cluster VPC and subnets)
Afterward, you can run the task and it should work like this:
As we defined a specific port we now need to expose it through the security tab.
Click your defined task
Click on ENI Id to get to the network interface
Click on your network ID
Go to “security groups”
Add your port as custom tcp in inbound rules
In the task tab, you will see your public IP displayed. Simply navigate to the page with the corresponding port and you will see your website
The IP address shown in the screenshot is not working anymore to avoid AWS costs. However, this article is part of a larger project, which I will launch under www.shouldibuycryptoart.com. The app is under development. If you wish to follow its development feel free to reach out to me or follow me social media accounts.
Due to some specific flags, I run on my local container development I get some errors for specific functions within my app.
In your task overview under containers, you can check the logs in cloudwatch. Click on the link and it shows you why your app crashes:
Now you can debug your app.
Happy coding!
I am not associated with any of the services I use in this article.
I do not consider myself an expert. I merely document things besides doing other things. Therefore the content does not represent the quality of any of my professional work, nor does it fully reflect my view on things. If you have the feeling that I am missing important steps or neglected something, consider pointing it out in the comment section or get in touch with me.
This was written on 6.3.2021. I cannot monitor all of my articles. There is a high probability that when you read this article the tips are outdated and the processes have changed.
I am always happy for constructive input and how to improve.
Daniel is an artist, entrepreneur, software developer, and business law graduate. His knowledge and interests currently revolve around programming machine learning applications and all their related aspects. To the core, he considers himself a problem solver of complex environments, which is reflected in his various projects.