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.
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.
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.
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.
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.
get_remaining_time_in_millis(): Returns the remaining execution time, in milliseconds, until AWS Lambda terminates the function.
function_name: Name of the Lambda function that is executing.
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.
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.
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.
The Cloud Watch: To find your logs in CloudWatch you need to know the log group name and log stream name.
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.
The error information depends on Invocation type e.g. if it is RequestResponse invocation type, the client who invoked may get error response.
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.
Save this file (for example, as hello_python.py).
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.
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.
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.