Slide 1

Slide 1 text

Python Serverless Microservices Nilo Ney Coutinho Menezes ([email protected])

Slide 2

Slide 2 text

Serverless Application Framework • Written in Javascript, but works with multiple languages • Uses Node, Python and Chocolatey (alternative Windows Package Manager) • Deploys the serverless infrastructure in the cloud. Compatible with Amazon Web Services, Google Cloud, Microsoft Azure and others • Simplifies configuration and application updates • Configuration files written in YAML • https://www.serverless.com/

Slide 3

Slide 3 text

Server less? • We know the server still there • But we don’t have to keep it running • We don’t have to update it frequently • We can have more as we need them (autoscaling) • We only pay if we use them • So, it is an industry term to say somebody else is taking care of the server for you.

Slide 4

Slide 4 text

Microservices • Microservices are an architecture for development and deployment of new systems • It antagonizes the traditional monolithic architecture that deploys the full system in a single application • Microservices means we should split our systems into smaller ones • Smaller parts are easy to understand, can be deployed independently • Share no database with other parts • Share data and services via a well-defined API • Can be rewritten one by one (less coupling)

Slide 5

Slide 5 text

Lambdas • Lambdas are Functions as a Service (FaaS) provided by Amazon Web Services (AWS) • We will be using AWS as our example provider • You need to have an account to test the code shown in this presentation • You can create a free account on their web site: https://aws.amazon.com/es • If it is your first time on AWS, I strongly suggest you check their web site and documentation, as you will be paying for the resources used beyond the free tier. • To start using AWS with Python, install their command-line client: pip install awscli

Slide 6

Slide 6 text

Python Serverless Microservices • Because we use Python • Because we use AWS Lambdas, FaaS, serverless • And we would divide each API call in a different lambda, grouped in a domain, following the directives of Microservices and Domain-Driven Design (DDD). Check these books for more info:

Slide 7

Slide 7 text

Installing • Download and install node.js from https://nodejs.org/en/ • Install the Serverless Application Framework: npm install –g serverless • Let’s created the application stub type: serverless • Try to answer the questions as in the image below:

Slide 8

Slide 8 text

The stub application: handler.py

Slide 9

Slide 9 text

Serverless.yaml • This is all the configuration we need for our simple function. • It says we are going to deploy a Python 3.8 Lambda Function • It defines the handler (the module and function name) • It also specifies the URL path to use and the http method

Slide 10

Slide 10 text

Deploying

Slide 11

Slide 11 text

Visiting our new page • Go to : • https://977optmvi2.execute-api.us-east-1.amazonaws.com/dev/pycones2020/hello • Or • http://bit.ly/PyConES2020-Serverless

Slide 12

Slide 12 text

The super user friendly page

Slide 13

Slide 13 text

What is in this json? • In the input field, all parameters passed to your function • Copy and paste it to your preferred text editor that supports JSON. Reformat the code to make it easier to read. • You get user location with IP and country information • The URL and path used on this call • Any parameter used (query string and path) if any • You can access all these values as in simple python dictionary

Slide 14

Slide 14 text

What do we have now API Gate way Browser Lambda Hello

Slide 15

Slide 15 text

Wait, no huge framework? • Nope • You have a very simple function deployed to the cloud • You choose the batteries • You can combine multiple Python libraries depending on your needs • Leverage the cloud provider services like databases, api gateways, queues, etc

Slide 16

Slide 16 text

Advantages • Simple, you can read the function from top to bottom, no hidden magic code • Cheap, millions of invocations for less than 1 euro • Pays as you go (no upfront costs, if you don’t use, you won’t pay either) • Functions can be deployed independently • No downtime, old version keeps running until the new one is deployed

Slide 17

Slide 17 text

Disadvantages • As any new thing can be difficult at the beginning • You get dependent to a cloud provider • More tools to use • Lack of framework to start with

Slide 18

Slide 18 text

What’s next • Create a DynamoDB table • Add more functions • Play with AWS Cognito to support authenticated users • Read about lambda layers • Check serverless-python-requirements, a serverless plugin to create dependencies packages for Python • Other frameworks to help with Lambdas and Python:  AWS Chalice https://github.com/aws/chalice  AWS SAM https://aws.amazon.com/serverless/sam/

Slide 19

Slide 19 text

Thank you • If you have any questions, do not hesitate to contact me: [email protected] • You can also visit my book’s website (Python for Beginners): https://librodepython.com/ • See you later this year on Python Brasil and Python Sweden