Introduction
Today we will discuss on everything you need to know about How to deploy a Python Microservice in AWS in simple and easy to follow guide.
Did you know Python and AWS Microservices go hand in hand and are super easy to implement?
We will start by explaining what a Microservice is and how it may be useful for your needs and why Python is a great choice for those starting their venture in the Microservices world. Then we will outline the most popular types of Microservice frameworks and how each of them can be used to deploy and manage code. We will further outline the advantages and disadvantages of each framework in order for you to understand what fits better your needs.
I have been working in the Software industry for over 23 years now and I have been a software architect, manager, developer and engineer. I am a machine learning and crypto enthusiast with emphasis in security. I have experience in various industries such as entertainment, broadcasting, healthcare, security, education, retail and finance.
What is a Microservice
Microservice is an architectural paradigm that structures an application as a collection of different compoenents that have the characteristics listed below:
- Each component is loosely coupled with the rest
- Each component can be individually composed and deployed
- Parallel programming can take place without one affecting another (exclude data dependencies)
- Microservice logic is tied directly to business logic and requirements set forth by the operational side
- Each microservice can have behind it a separate team managing and maintaining it
- Microservices are easier to test as they are isolated between them thus unit testing and functional testing is more efficient
All of the characteristics mentioned above directly allow an application to do the following:
- Scale both horizontally and vertically
- Include complex business logic broken into smaller pieces
- Allow parallel and rapid development by various teams
- Share and extend the technology stack of an organization
A lot more can be said about Microservices that are beyond the scope of this post so I’m going to stop here and dive right into the specifics of why and how they work with Python and the AWS Ecosystem.
Can Microservices be written in Python for AWS
Python is one of the best languages someone can start when trying to move to a Microservice based model. As you already know most of the Python frameworks and AWS services have been written from the ground up to support the power and flexibility Python gives to a project. Thus making Python an ideal if not the best language someone can use in the microservices landscape. The main reasons why Python is great for this are listed below:
- Python offers speedy development as it does not rely explicitly on data types
- It can be broken into separate pieces which is one of the fundamentals of microservices
- Libraries and functions can be re-used across the ecosystem fairly easily using pip or other packaging mechanisms
Is Python good for AWS Microservices
Similar to above Python is great specifically for the AWS ecosystem along with outside it. I’m going to list some options below on why I think it’s great option:
- AWS has great support for Python and has a special library called Boto3 which is rich on features and abstracts a lot of the boiler plate code that’s needed to perform various functions in your microservice
- Python has various frameworks for deploying your code as a microservice which we will describe in detail later in this article
- Python has great unit and system testing which complements the microservice paradigm
- Python has a lot of libraries and frameworks that support the Microservice model out of the box allowing you to have options when designing your system
How to deploy Python AWS Microservice
Now that we went over the main reasons as to why Python is great for the AWS Microservice ecosystem we will discuss some options on how you can deploy and use it to implement your microservices. I’m going to analyze the most popular solutions and go over what advantages each has compared to the others.
Below we will go over 4 alternatives and outline the advantages and disadvantages of each in order for you to make the right decision on which one is closer to your project needs and background technologies that may already be present in your stack. There’s no single right solution to this you should pick what works best for you. The platforms that help you deploy and manage that we will cover are:
- Zappa
- AWS Command Line / AWS Web Console
- AWS Python Boto3
- Serverless
- Docker containers
How to deploy Python AWS Microservice using Zappa
Zappa is a framework that lets you build, bundle and deploy your Python application on AWS Lambda. It handles all the boiler plate code and abstraction for you offering a few easy to use commands. Installing it and running it is as is as follows:
$ pip install zappa
$ zappa init
$ zappa deploy
As you can see above the init and deploy is all you need to get going. Setting it up as a virtual environment or just installing it locally in your system using pip takes care of everything you need.
Advantages
- It’s Pythonic so you stay in Python not needing any external systems
- Works out of the box with AWS no need for you to do complex configurations
- Deploys instantly to your environment
- Quick learning curve
Disadvantages
- It’s not as powerful as other frameworks when it comes to defining your deployment mechanism
- It’s limited to API Gateway/Lambda
- Only supports WSGI based frameworks
How to deploy Python AWS Microservice using AWS Cli / Web console
This is probably one of the best alternatives out there to deploy your AWS Microservice that uses Python. AWS has the web console which is an interactive UI and complements the AWS command line interface. Both work in the same way and support the same functionality. The difference is that with the AWS command line interface you can put it as part of your batch jobs as it allows you to do tasks in an automated way. So if you have a shell script as part of your deployment workflow in Jenkins or Gitlabs you need to use the AWS command line code. If you are on a browser and you are doing things manually then it’s better to just use the web console and you’d be a few clicks away after you zip your code.
I have written an article detailing this which you can find below:
How to Setup an AWS Lambda Python Function From Scratch
Advantages
- Full framework support as it’s native to Amazon AWS
- Easier and no learning curve AWS has very good documentation describing the process step by step (or you can read my guides which make it even easier for you)
- Quick deployment with the click of a button once the data has been uploaded
Disadvantages
- Lack of WSGI framework support so if you are coming from a Django world you will need to do some work
- Lots of boilerplate code and commands to deploy things
How to deploy Python AWS Microservice using Boto3
Deploying using the Boto3 interface is very similar to using the AWS Command line interface. Both rely on leveraging an API and Secret key to do things in a batch way. Offcourse using Boto3 is more Pythonic and a better way to approach this if you want to stick strictly with Python code. The advantages and disadvantages of this approach are similar to using the API CLI or Web console deployment approach.
How to deploy Python AWS Microservice using Serverless
Serverless is a third party application/service that lets you deploy your code into AWS for microservice driven models. It supports a wide range of languages and AWS services one of which is Python and AWS Lambdas. It lets you define a template in YAML what you want to deploy and then it automates the whole process in a simple hands off way which can be scripted, run manually or done as a recurring batch job.
An example of how easy it is to define a yml file for a Python microservice follows:
service: heartbeat-test
provider:
name: aws
runtime: python3.9
stage: dev
region: us-east-1
functions:
heartbeat:
handler: heartbeat-handler.main
The above defines a heartbeat microservice that will get deployed in AWS with a Python3.9 runtime. Off-course all of these are customizable and will vary based on what you are trying to accomplish.
Advantages
- Support for multiple frameworks
- Fast deployments
- Rich documentation and good community
- Great support team standing behind the product
- Very good debugging tools built into the framework
Disadvantages
- External application (not pythonic)
- Not a lightweight solution
- Initial learning curve
How to deploy Python AWS Microservice using Docker containers
Another popular approach is to use docker containers. As you know docker containers are very well supported in the AWS ecosystem. There’s services such as ECS and EKS which are very prominently used lately to abstract managing your own Kubernetes service. I have written a detailed article explaining the differences between the two managed service Amazon offers for this task which you can find here: Amazon AWS ECS vs EKS.
Advantages
- No dependency problems
- More control of fine tuning packages
- Works with any service or programming language
- Easier to add dependencies such as third party or system apps
Disadvantages
- Too much of an overhead in terms of resources and disk space
- Requires some registry and orchestration to deploy
- Hard to debug
- Harder to maintain security issues need patching of instances
- Slower to deploy
Conclusion
If you found How to create a Python Microservice in AWS useful and you think it may have helped you please drop me a cheer below I would appreciate it.
If you have any questions, comments please post them below I check periodically and try to answer them in the priority they come in. Also if you have any corrections please do let me know and I’ll update the article with new updates or mistakes I did.
What Microservice solution do you use to deploy into AWS?
My personal favorite is docker containers when I’m working on a complex microservice. The reason for this is that Docker allows me to configure and add all the dependencies pre-packaged into my container and also be able to fine tune and optimize the performance of where it’s running. Being a power-user Docker has an answer for all my needs however as outline earlier it does come with a lot of disadvantages and it’s not recommended for beginners or those that want to start with an easy to go solution with effortless maintenance.
For simple applications these days I stick to vanilla AWS with the command line interface as it satisfies all my needs and I can add one liner code to deploy my instances via jenkins or gitlab.
If you would like to learn more about AWS Microservice related information please take a look at the articles below: