AWS Lambda

Kamil Gałuszka 

Kato.py #5

21.06.2016

Do we have a problem?

For sure there is architectural question.

The problem

class _void(object):
    pass

The problem

class _void(object):
    pass

AWSAuthConnection = AWSQueryConnection = _void

The problem

class _void(object):
    pass

AWSAuthConnection = AWSQueryConnection = _void
RawMessage = Message = MHMessage = \
    EncodedMHMessage = JSONMessage = _void

The problem

class Object(object):

The problem

class Object(object):
    """Common base class supporting automatic kwargs->attributes handling,
    and cloning."""
    attrs = ()

    def __init__(self, *args, **kwargs):
        for name, type_ in self.attrs:
            value = kwargs.get(name)
            if value is not None:
                setattr(self, name, (type_ or _any)(value))
            else:
                try:
                    getattr(self, name)
                except AttributeError:
                    setattr(self, name, None)

    def as_dict(self, recurse=False):
        def f(obj, type):
            if recurse and isinstance(obj, Object):
                return obj.as_dict(recurse=True)
            return type(obj) if type else obj
        return {
            attr: f(getattr(self, attr), type) for attr, type in self.attrs
        }

    def __reduce__(self):
        return unpickle_dict, (self.__class__, self.as_dict())

    def __copy__(self):
        return self.__class__(**self.as_dict())

What
exactly
AWS Lambda is?

AWS Lambda

  • Compute service
  • Responding to events (event driven)
  • Server less infrastracture
  • You don't have to scale anything

AWS Lambda Pros and Cons

 

pros

  • Python support
  • It works outside of the box
  • Good permission model
  • You don't have to scale anything
  • Fast as hell

AWS Lambda Pros and Cons

 

cons

  • At some level it's hard to manage
  • For much more complicated cases it maybe overenginnering
  • Amazon vendor lock-in
  • Lack of Python 3 support

AWS Lambda
Supported languages

  • Python (Yay!)
  • JavaScript (NodeJS)
  • Java (uhm...)

AWS Lambda
Pricing. Is it cheap?

  • First 1 million requests per month are free
  • $0.20 per 1 million requests thereafter ($0.0000002 per request)
  • Request is billed every 100ms

We all know where this goes...

Lambda handler

import boto3 # we have default Boto library
from base64 import b64decode # std library also available

# we can add our own libraries 
# when we upload with ZIP or S3

def lambda_handler(event, context):
    req_body = event['body']
    return "Kato.Py We love You!"

Lambda event

from __future__ import print_function


def lambda_handler(event, context):
    
    print(event) #dict

    return "I don't know really!"

Lambda context

from __future__ import print_function

import logging; logger = logging.getLogger()
logger.setLevel(logging.INFO)

def lambda_handler(event, context):
    
    print("Name:",context.function_name) #KatoPyExample
    print("Version:",context.function_version) #Version: $LATEST
    logger.info("Request ID: {}".format(context.aws_request_id))
    # [INFO] 2016-06-21T14:26:36.151Z 28d0a390-37bc-11e6-b1bd-15e2e8950966 
    # Request ID: 28d0a390-37bc-11e6-b1bd-15e2e8950966
    print("Mem. limits(MB):", context.memory_limit_in_mb) 
    #Mem. limits(MB): 128
    print("Remaining:", context.get_remaining_time_in_millis()) 
    #Remaining (MS): 2999


    return "Yeah!"

Deployment package

For 3rd party packages we have do:

 

mkdir our_lambda
cd our_lambda
pip install requests -t .

vim lambda_work.py

After that we only need to specify which function will be invoked by lambda.

Source of events

  • AWS IoT
  • CloudWatch Events - Schedule
  • CloudWatch Logs
  • Cognito Sync Trigger
  • DynamoDB
  • Kinesis
  • S3
  • SNS
  • API GATEWAY -> Uber cool!

Questions?

Thanks for great time!

AWS Lambda

By Kamil Gałuszka

AWS Lambda

  • 784
Loading comments...

More from Kamil Gałuszka