Lambdify - a new take on AWS Lambda

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

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 . What it is and what it is eaten with has already been written, for example here . In this regard, there are many tools for working with Lambda, including those written in Python, for example Zappa , python-lambda , etc. But all of them 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 lambda-handler code, which prevents them from being used programmatically.

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

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 go into the AWS console , you can see the brand new echo lambda function. One of the features is that the signature of the original function is automatically reduced to the signature defined in the documentation http://docs.aws.amazon.com/lambda/latest/dg/python-programming-model-handler-types.html .

If the above example did not seem too convincing, then here is a snippet for thought:

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

... yes, yes, this is a lambda that creates new lambdas ...

image

... and all this happens in the cloud. True, for this you will need to play with the roles and policies of the Amazon, but this is no longer included in the scope of this article.

Now back to the real world. Users who have encountered using task queues (hereinafter OZ), such as Celery or RQmay 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 the need to administer workers and provide scalability (unlike traditional OZ). Perhaps in the future, this tool will help someone migrate from using OZ to AWS Lambda.

Of course, it’s worth noting that the library is in the alpha version: there are a lot of plans for new features, new features, bug fixes and more. The purpose of this article was to share with readers a new look at Lambda in terms of meta-programming and introspection of Python. I hope that this will serve as an impetus for the development of this project.

References:


Also popular now: