Building Lambda function And Best Practices

hancil

What Is AWS Lambda?
AWS is a lambda service which helps in running code without managing the servers. It executes the code only when need and scales accordingly, to the request, got per day to thousand per seconds. With AWS Lambda we can run the code for virtually any type of application or backend services. The code is always run on high-availability compute infrastructure and performs administration of the resources, including server and operating system maintenance etc.Currently, AWS Lambda supports Node.js, needJava, C# and Python

AWS Lambda can be used trigger a response to an event such as to do changes to data in Amazon S3 bucket or Amazon DynamoDB Table, which is done through a response to HTTP request using Amazon API Gateway, or access the code to Amazon API calls made using Amazon SDK’s.

Building Application with AWS Lambda function

While building the application on AWS Lambda, core combo needs to be considered are Lambda function and event source. An event source is also as a custom application that publishes events and Lambda function with custom code that can process the event. For example, consider

1)File processing – Where we can share the photo which sharing application.people can use this application to upload the files, and which are stored in Amazon S3 bucket, by this application will create a thumbnail image and then they are used to display on user’s profile page. In this scenario, we can create a Lambda function which will create automatically with the help of Lambda function.Our Lambda function code can read the photo object from the S3 bucket which creates a thumbnail version, another S3 bucket, and another S3 bucket.

2)Data and Analytics – Suppose we are building an application and storing data using a DynamoDB table, then we need to write, update, delete to each table which needs to be done usually. Whereas DynamoDB can stream and publish item update events which are associated with the table. In this case event, data will be providing the item key, event name to insert, update, delete and also other relevant data which can be written using Lambda function which is used to generate custom metric by aggregate raw data.

3)Website – Suppose if we want to create a website and want to host the website with the backend logic for the website which can be done with the help of Lambda function.We just need to invoke our Lambda function over the HTTP using Amazon API Gateway as the HTTP endpoint.Now, our web client can be invoked the API, and then API Gateway will route the request to Lambda.

4)Mobile Application – Suppose if we need to have a custom mobile application that produces events.we can create a Lambda function to process such event published by our custom application.For example, we can configure a Lambda function to process the clicks within our custom mobile application.
Each of these events sources will have a specific format for the event data.When a Lambda function is invoked it will receive the event as the parameter for the Lambda function.

AWS Lambda will also support many AWS Services as an event source. We can configure this event sources to trigger a Lambda function, which is invoked automatically when an event is occurred.we define the event by source mapping, which identifies what events to be tracked and which Lambda function need to be called. We can also build our own custom events sources. This custom event sources can invoke a Lambda function using AWS Lambda invoke an operation. An application such as client mobile, a web application can publish the event and can also invoke the Lambda function on demand using the AWS SDK or AWS Mobile SDK.

Example 1: Amazon S3 Pushes Events and Invokes a Lambda Function.
Amazon S3 can publish events such as PUT, POST, COPY and DELETE object on a bucket. Using bucket notification we can configure event mapping source that directs Amazon S3 to invoke Lambda function with a specific type of event.

This figure illustrates the following sequence:
1)The user can create an object in a bucket.
2)Amazon S3 detects the object event created.
3)Amazon S3 can invoke the function using the permission provided to for role execution.
4)AWS Lambda can execute the Lambda function.,having events has parameters.

aws lambda 1


Example 2: AWS Lambda Pulls Events from an Amazon Kinesis Stream and Invokes a Lambda Function.

For stream based event source, AWS Lambda polls the stream and invokes Lambda function when records are detected to the stream. These stream sources are special in the event source mapping information which is stored in Lambda. AWS Lambda allows us to create API to create and manage these events.

This diagram shows us how custom application writes a record to an Amazon Kinesis stream.

aws lambda 2

1)The custom application writes a record to an Amazon Kinesis stream.
2)AWS Lambda will continuously poll the stream and invokes Lambda function which detects new record on stream.
3)Lambda function is invoked with an incoming event.

Best practice with AWS Lambda function
-Must write the Lambda function code in a stateless style, and must ensure that there is an affinity between the code and underlying computing infrastructure.
-Must instantiate AWS clients outside the scope of the handler so that it can be re-used.
-Set +rx permission on our uploaded zip files to ensure Lambda can execute the code on behalf of us.
-Low cost and improved performance by minimizing the use of startup code which does not directly relate to the current processing event
-Can use built-in CloudWatch to monitor the Lambda function.
-Can delete Lambda function no longer used?

 

 

 

about the author

hancil