Imagine a scenario where your application is handling a lot of users at the same time with various functionalities. To implement such a scenario smoothly we should have a server that is able to handle all this. If the users keep on increasing we should scale the server and do all the necessary steps for the smooth working of our application. This is time-consuming and costly. Think if there is a way to automate it, that too at low cost.

AWS Lambda does the work for us. AWS Lambda is a new AWS service that helps us to not only scale the most time and server consuming part for us but also carry out error logging, patching and monitoring. All we need to provide is the code. And we pay as per our use. 

 

When does it call?

You can use AWS Lambda to run your code in response to events, like changes to data in an Amazon S3 bucket, an Amazon DynamoDB table, to run your code in response to HTTP requests using Amazon API Gateway, or invoke your code using API calls made using AWS SDKs. With these capabilities, you can use Lambda to easily build data processing triggers for AWS services like Amazon S3 and Amazon DynamoDB process streaming data stored in Amazon Kinesis, or create your own back end that operates at AWS scale, performance, and security.

 

Where does AWS Lambda cost us?

The service costs us for two parameters.

  1. Requests: You are charged by the total number of Requests to your lambda function. Lambda counts a request each time in response to an event notification, invokes call, test invokes from the console. The first 1 million requests per month are free and the cost thereafter is $0.20 per 1 million requests ($0.0000002 per request)
  2. Duration: Duration is calculated when you invoke a function till it finishes or loses connection. This is rounded to the nearest 100m. The price depends on the amount of memory you allocate to your function. You are charged $0.00001667 for every GB-second used.

Apart from this, the amount of memory we define to a service while setting it up and the execution time also add to the price.

 

How to write Lambda function for Python?

There are many ways to do it, similar to already built blueprints available in console build by AWS. Merely invoke this. AWS SDK can be used to interact between different AWS services. There will be always options to create a custom function.
A Lambda function will be having some basic parts

  • Handler: This will be the initial thing which will be called when a Lambda function is invoked.The event details will be passed by lambda to this.It is handlers duty to take the process further.Arguments which are passed into this handler include event and context.

Event-This contain event details .It will be python dict format type. It can also be list, str, int, float, or NoneType type.
Context-It provides RUNTIME information of the handler.
Return- HTTP response,Invocation etc
sample :

 

 def first_handler(event, context):  
        message = 'HelloWorld {} {}!'.format(event['first_name'],  
                   event['last_name'])   
        return {  
        'output' : message  
        }   

 

  • The Context Object: This deal with communication with Lambda function and Lambda service.eg remaining time for AWS service terminate, Cloud watch log details, AWS requests ID etc.

Example Method:
get_remaining_time_in_millis(): Returns the remaining execution time, in milliseconds, until AWS Lambda terminates the function.
Context Object Attribute example:

  1. function_name: Name of the Lambda function that is executing.
  2. function_version: The Lambda function version that is executing. If an alias is used to invoke the function, then function_version will be the version the alias points to.
  3. invoked_function_arn: The ARN used to invoke this function. It can function ARN or alias ARN. An unqualified ARN executes the $LATEST version and aliases execute the function version it is pointing to.
  4. memory_limit_in_mb,aws_request_id,log_group_name etc

 

  • Logging: As mentioned early the Lambda function will lock details for us we just have to define that in our function.There are two possible ways to using print statement or Logger functions in the logging module.Both will log details but logger functions write additional information to each log entry, such as timestamp and log level.

sample using logger:

 import logging  
 logger = logging.getLogger()  
 logger.setLevel(logging.INFO)  
 def my_logging_handler(event, context):  
        logger.info('got event{}'.format(event))  
        logger.error('something went wrong')  
        return 'Hello World!'   


sample using print statement:

 from __future__ import print_function  
 def my_other_logging_handler(event, context):  
   print('this will also show up in cloud watch')  
       return 'Hello World!  


The logs can be seen in three different places:

  1. The AWS Console:
  2. The Cloud Watch: To find your logs in CloudWatch you need to know the log group name and log stream name.
  3. Response Header if we invoke Lambda Programmatically: we can add fields to retire this last 4 KB of log data from Cloud watch.

 
Exceptions: If Lambda function come across an exception it will parse the and serialize the error and return a JSON.
sample:

 def always_failed_handler(event, context):  
   raise Exception('Oops!')  
 it will return following JSON  
 {  
  "errorMessage": "Oops!",  
  "stackTrace": [  
   [  
    "/var/task/lambda_function.py",  
    3,  
    "my_always_fails_handler",  
    "raise Exception('Oops!')"  
   ]  
  ],  
  "errorType": "Exception"  
 }  

 

The error information depends on Invocation type e.g. if it is RequestResponse invocation type, the client who invoked may get error response.
 

How to upload code to Lambda

There is a way to create code in the inbuilt, it editor and upload from theirs .We can also use blueprint and use them, so setting up is easy and fast. The difficult part in custom function is that we should upload the code and dependencies in .zip format. And it should follow a particular format.
 
Steps:

  1. Save this file (for example, as hello_python.py).
  2. Package the file and any dependencies into a .zip file. When creating the zip, include only the code and its dependencies, not the containing folder.
  3. Upload the .zip file using either the console or AWS CLI to create a Lambda function. You specify the function name in the Python code to be used as the handler when you create a Lambda function. Note that the Getting Started uses a blueprint that provides sample code for a Lambda function. In this case, you already have a deployment package. Therefore, in the configure function step you choose to upload a zip.

 
The following create-function AWS CLI command creates a Lambda function. It specifies the --handler parameter to specify the handler name.

 aws lambda create-function \  
 --region us-west-2 \  
 --function-name HelloPython \  
 --zip-file fileb://deployment-package.zip \  
 --role arn:aws:iam::account-id:role/lambda_basic_execution \  
 --handler hello_python.my_handler \  
 --runtime python2.7 \  
 --timeout 15 \  
 --memory-size 512  

 

Conclusion

 The application of Lambda function is endless in Web Apps, Data analysis, Mobile Apps, etc. The catchiest thing about this service is the easy automation of a huge and complicated process into single module so that we can work more on the code part. Developers and customers can improve the user experience of their product in a less expensive manner. Better user experience means more advantages and hence this service can add more value to everyone.

 

Subscribe to our newsletter. Get updates on awesome happenings in the technology world!
Newsletter subscription successfull! Something went wrong! Email is already registered!

TAGS