Subscribe to our Blog
We're committed to your privacy. SayOne uses the information you provide to us to contact you about our relevant content, products, and services. check out our privacy policy.
Jibu JamesJanuary 27, 20177 min read
Generating table of contents...
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.
The service costs us for two parameters.
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.
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
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
}
Example Method:
get_remaining_time_in_millis(): Returns the remaining execution time, in milliseconds, until AWS Lambda terminates the function.
Context Object Attribute example:
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:
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.
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:
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
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.
We're committed to your privacy. SayOne uses the information you provide to us to contact you about our relevant content, products, and services. check out our privacy policy.
About Author
Jibu James is the Team Lead at SayOne Technologies. He is passionate about all things related to reading and writing. Check out his website or say Hi on LinkedIn.
We collaborate with visionary leaders on projects that focus on quality and require the expertise of a highly-skilled and experienced team.