📜 ⬆️ ⬇️

Lambdify - a new look at working with AWS Lambda

This article will tell you about trying to make friends with AWS Lamba and python in the true sense of the word. By true meaning, I mean the ability to interact with a service (create, update, and call lyabda functions) directly from Python. If you are interested in AWS Lambda and python, I present to your attention the proof-of-concept library lambdify .

Disclaimer
This library is work-in-progress as well as proof-of-concept, so any feedback is very welcome.

Recently, a service such as AWS Lambda has been very popular. About what it is and what it is eaten with, it has already been written, for example, here . In this regard, many tools for working with Lambda appeared, including those written on Python, for example Zappa , python-lambda , etc. But all they are united by the lack of the ability to create lambda-functions dynamically. One way or another, the user has to indirectly manipulate files containing the code of lambda handlers, which makes it difficult to use them programmatically.

In turn, lambdify bypasses this limitation, allowing you to get a working lambda function by writing a few lines of code.

Installation
pip install awscli aws configure pip install lambdify 


Now you can create your lambda function in 5 lines of code:
')
 from lambdify import Lambda @Lambda.f(name='echo') def echo(*args, **kwargs): return args, kwargs # ,     ,    #         echo.create() if __name__ == '__main__': import getpass echo(msg='Hello, {user}!'.format(user=getpass.getuser())) 

Now, if you log into the AWS console , you can see the new echo lambda function. One of the special features is that the signature of the original function is automatically converted to the signature defined in the http://docs.aws.amazon.com/lambda/latest/dg/python-programming-model-handler-types.html documentation.

If the above example did not seem too convincing, then here's some snippet for you to think about:

 @Lambda.f(name='meta') def meta(name=None): @Lambda.f(name=name or 'foo') def foo(*args): return 42 return foo.get() 

... yes, it's lambda, which creates new lambda ...

image

... and it all happens in the cloud. True, for this you will need to play around with the roles and politicians of the Amazon, but this is no longer part of the scopes of this article.

Now back to the real world. Users who are faced with the use of task queues (hereinafter OZ), such as Celery or RQ , may notice some analogy with their interfaces. And they will be right: one of the goals of lambdify is to try to combine the convenience of using OZ to scale the system, with the advantages that AWS Lambda provides, in particular, the lack of need to administer workers and provide scaling (unlike traditional OZ). Perhaps in the future, this tool will help someone migrate from using OZ to AWS Lambda.

Of course, it is worth noting that the library is in the alpha version: there are a lot of impructs in the plans, new features, bug fixes and so on. The purpose of this article was to share with readers a new look at Lambda from the point of view of meta-programming and Python's introspection. I hope that this will trigger the development of this project.

References:

Source: https://habr.com/ru/post/281216/


All Articles