Python Serverless Microservices - PyConES 2020

Oct 3, 2020 11:30 · 3138 words · 15 minute read credit card telling 17 system

okay so welcome to this presentation my name is nilo menezes i will talk about serverless micro services in the pycon 2020 the pandemic edition so i will try to to recap a little bit not too much because we we only have 15 minutes so let’s see how it it works first let me make a clarification regarding the serverless application framework the name of the the framework is a little bit ambiguous because it’s it’s the name of the technology it helps should apply as well so sometimes it’s difficult if you are talking about serverless as the concept of serverless as the framework so from now on i will try to make it the the difference between both of them okay so initially the serverless application framework is a set of tools created in javascript but that work very well with other languages like python go and others they are also using node python and chocolatey to deploy this package on windows so it works very well on linux and windows and it will help you to deploy a serverless infrastructure on the cloud regarding the cloud provider you can use amazon web services google cloud or microsoft azure others are also supported i will show everything using aws because the one i’m most used with okay - the idea of the serverless application framework is to simplify the configuration of the infrastructure you need to deploy your application in the cloud - you can configure everything using yaml files and you can download and read more about it at serverless.com but what do we mean by serverless? we know that the server is still there so when i say serverless i put this a distinction with a space in between so two words uh the idea is you should not care about the server - somebody is taking care of this server usually it’s your cloud provider who is responsible to apply patches, security updates and keep the server running so when we are working with serverless does not mean that the server does not exist - it only means that you should not care about the server as the cloud provider will instantiate the server for you only when you need and you will only pay when it’s running so most of the time it’s an industry term to say somebody else is taking care of the server for you okay - and what about microservices? microservices is a concept that i cannot explain in 15 minutes so i have a few bullets here to describe the main topics and i will also suggest some books that may bring more information if you need so it’s an architecture choice that we use to develop and apply new system it’s antagonize the monolithic approach meaning that instead of deploying the whole system in a single executable or for example a single django application you break the the application following the business domains so instead of having a single application that does everything you specialize these applications in microservices how to split when you split and when to use the the micro services we would need a little bit more time to to talk about last year i presented our experience in the pycon spain so you may find the video in the history of the conference but basically the idea of microservices that you have parts of your application that you share no database with your other parts they will communicate only using the api so you are free to change and deploy the microservice independently if you keep the api they should remain compatible and the goal of this is is for example you can you can do changes you don’t have to ask the other team it works very well if you’re working in big companies in distributed teams and it also allows the each team to use their own tools and development languages depending on the scenario okay - so a bit about lambdas: lambdas are a function as a service it’s a kind of a cloud concept where we deploy an application already split by functions meaning that your code is like a function call in the whole application and the lambda is the name they they are using in aws for this service - you need to have an account on aws to host and test your code you can get a free web account a free account on their website i think they still require your credit card but if you don’t use it a lot you won’t pay anything in the first year at at least but i advise you to read a little bit how the free tier works and if you don’t have the command line application already installed on your system you can install it very easily with pip install awscli in the remaining of the presentation i assume you are using python 3 and you know the basics of python so i will try to focus in the serverless framework and what we are trying to show here - so about the python serverless microservices the idea is to combine python with the serverless concept and microservices and as i said this subject itself is too big so if you want more information i recommend these four books uh they provide lots of hints two are by Sam Newman the the author that coined the microservices and this is built on top of ddd domain driver design so even if you don’t want to join the serverless or the microservice movement it’s very important that you figure out how domain driving design works what’s the benefits it can really helps if you develop enterprise applications so as we don’t have a lot of time let’s try to install and deploy your first lambda the idea is that you will install node.js from the website the url is in the presentation uh you can probably download this presentation with the videos so check the description and to install just open a command prompt here i’m showing how it works on windows but it’s the same on linux so npm install -g severless and you should download everything here i assume you already installed node.js in the first step on windows it’s a simple executable on linux just use your package manager so it should not be very complex - after you npm install serverless you have a new command called serverless that you can use to create the simple application so in the example you can see i just called serverless it will ask three or four questions try to answer as i’m presenting in the image and you should be ready to go this will create the initial configuration file and a simple python application that i will show in the next slides so this is the uh step application this is the handler.py it was created by the serverless framework in the from the comment we we gave in the previous slide and as you can see it’s very very simple it’s a python function that receives two parameters: one is the event the second one is the context - here we will only be using the event and the rest is pure python we are just creating a dictionary we attach the event in the input so we will return the input we receive it and we build the response it’s another dictionary so you can see we are just setting the status code to 200 and we created the body to be the json dump as body that we just built using the standard json module from from python so no framework we didn’t use anything yet just the the serverless framework to deploy the lambda okay so what about the configuration that was automatically generated for the sake of the presentation i removed the comment comments but this file is is quite big i think it has 200 lines but if you remove the commands you get something like 17 lines i added the the events especially the the line is between 13 and 17 so you need to add this to have the same result as you have in fact what we did here is that we thought we are telling the serverless framework that we want to deploy a lambda function and that this lambda function will be deployed using aws and python 3.

8 09:59 - aws support different versions of python so you can use python 3.6 3.7 3.8 and even the old python2.7 it’s up to you it says that the function when it started to call handler.hello it’s simply the module name with the function name inside it as we have a very simple application the module name is the file name and hello is the name of the function we we just saw it also specifies the uh how we will be using to access this function so the events http will add this path to a url that will be created by serverless so when we call something pycones2020/hello it will call a - okay so how do we move these to the cloud uh to go to the cloud we simply type sls deploy sll s is the the short form of the serverless command okay so here we have the full results of of the deployment so sls deploy and all the rest is automatically created by serverless before you deploy you have to put your aws credentials using aws click configure so follow the instructions uh to to get the account and to get the the key and the secrets needed to deploy but besides that all you need is is on this slide okay so the serverless deploy will package your function meaning that it you prepare the code to be uploaded to aws if you check for example when the pen is needed to to deploy and then it will create the infra infracture as code needed to to create everything you need on aws to have your function working so you see that uploading cloud formation file to s3 - this is automatically generated - cloud formation is a service that does the the infrastructure as a code on aws side so it is the one that will create the api gateway that will also link the api gateway to your function and so on and then once this is done everything is applied to the cloud it will display what it just do so we have here that we deploy the pycon 2020 pandemic edition the staging development the region i deployed in the united states the stack is pycones 2020 pandemic dev so it’s just composed of the stage i’d apply 12 resources even if i’m using a single function there is some boilerplate infrastructure that’s that needs to be deployed later on and oh sorry it’s it’s automatically generated and deployed all together by serverless it also says i’m not using api keys and then i have a new endpoint so in the endpoint you have a full domain this was created by the serverless framework using the api gateway on amazon so as you can see our function is already running under https which is is great so you don’t have to care about the certificate and the function we deployed is the real hello function and we are not using lambda layers okay so to visit your homepage we just have to access this url i created a short shortened version with bitly so if you go to to bitly pycon’s 2020 serverless you should see it and this is the result so as you can see we are just writing the dictionary we receive what i recommend you to do is to copy and paste this to your text editor okay and try to reindent the code so you see lots of useful information you receive in the event when your function is called so you have all the all the parameters in including uh the path parameters the query parameters but you also have for example the IP, the country of the caller the url they use the http method and you can access all these using a simple dictionary okay lots of useful information unfortunately we don’t have a lot of time so what do we have so far? so far we we used our standard browser in my case it was chrome to do a get request as in any other web application this web application instead of being served by a standard web server server by the api gateway on amazon website uh this was created by the serverless framework so serverless created the cloud formation script to tell aws hey i need an api gateway that you receive http request for me and if you receive something at this path so it’s pycon 2020 slash hello follow this request to my lambda function that i’m also uploading to you so the api gateway you forward the request headers through the lambda hello a function will be called in a server that we do not control and if you return that dictionary with the json response that will then be forwarded to the browser the idea of serverless is that you don’t have to keep the function running so this code is only active when there is a call usually amazon keeps it’s active for a few minutes and after a while they will destroy the the instance or reuse the the space for other functions - what happens if you have multiple calls? usually amazon will create multiple copies of your functions in different servers to generate the reply so this is one of the benefits of serverless you you should not care about the scaling directly it’s not as hard as in a django application where you you would have to choose to add more servers, check the cpu load and so on - so this is this is pretty automatic if you use lambda functions of course it’s not free: there is no free lunch there are a lot of complexity behind the scenes uh but it’s a very good resource that i think it’s it’s worth being explored by you okay so what did they accomplish here we didn’t use any framework and we have an endpoint running on the internet yes we didn’t use django we didn’t use flask on the python side the only thing we are using is the json module choose to convert the dictionary to json format so it can be understood by the browser but we had everything we we didn’t need this we didn’t need to use any special code we didn’t have to run anything before our functions we can we can simply read the function from top to the bottom and we know everything that is that will be done the only framework we are using is the framework that prepares and deploys the function to aws but this is part of the infrastructure so it’s like what serverless does is like the the scripts you would need to deploy your django application to a remote server but we are not using django we are not using flask we are not using any python framework just serverless okay that’s the idea you can keep it very simple you can choose which libraries you want to use what database how do you access it even which version of python and you can also have access to all services your cloud provider enables make available to you like different database, different api gateways different queues and etc okay as i said the main advantage that it’s cheap millions of invocations for less than one euro per month it’s simple you can simply read the code you don’t have to think about what was running before what was running next and so on it’s a pay-as-you-go system so you know that if you don’t use you won’t pay it’s very very simple to to understand and you can start very small so it’s very good to try new ideas you can deploy the functions independently so for example i have a bug here i can deploy just that function so serverless we’ll let you do that no downtime because when you you are uploading a new version the old version is removed but the not until the new one is ready to receive requests so what the api gateway does is that when you are uploading a new function all requests will be handled by the old version and when the new version is available it will automatically reroute the the new request to the new version so your system is always on - disadvantage is that it’s difficult at the beginning it’s new so this is something you have to learn - it’s you you get more dependent on your cloud provider because once you start using lambda functions uh there is a tendency to use more and more cloud services uh you have to use the the serverless framework install nodes so more tools to use and maybe you missed the framework so what’s next for you? i recommend you to have a a look and and create a dynamodb table so you’ll be able to access the database a very good python client for dynamodb is called pynamo - pynamo is is excellent and it’s very easy to use they have a good documentation you can also add more functions more endpoints so we just have a get request but you can have any kind of http request and read a bit more about lambda check the serverless python requirements plugin in the serverless framework because it automatically creates the zip you need for example if you are using external libraries just imagine you you want to install jinja and to use templates you you can install with the serverless python requirements it will automatically detect that and and include the the jinja package in in the zip that will be applied to lambda and it’s almost automatic you would just see it passing there are also other frameworks that help building lambda functions with python so you can also have a look in aws chalice and also in aws sam - i’m letting the links in the presentation and i thank you very much it was very very short i i hope i didn’t went so fast and i provided at least some hints if you have any questions do not hesitate to send any my email is on on the last slide okay see you next time bye bye .