Serverless Computing with AWS Lambda

banner

Cloud computing has revolutionized the IT industry by providing infrastructure as a service. Companies/Individuals can launch their servers on a cloud without purchasing single hardware, moreover, they don’t have to spend a single penny on maintenance and administrations. Servers can be launched on AWS cloud in no time and you have to pay for what you use.

AWS Lambda

In 2014, AWS came up with something even more interesting called AWS Lambda in which you can deploy smaller on-demand applications without hosting any servers thus called Serverless computing. It is an event-driven framework that runs stateless code in the background in response to some events and automatically manages the computing resources (CPU/Memory) required for it.

Characteristics of AWS Lambda

aws_lambda
  • Lambda functions will run your code only when called directly or when triggered by some outside event.
  • It scales automatically which means it can handle from a few hundred requests to thousands of requests per second. You need not add or delete machines manually.
  • It manages the computing requirement automatically which means you need not provide any CPU, memory, network, and other resources.
  • You have to pay only for the time your code is running.
  • Lambda functions should be stateless functions, which means each invocation is independent of previous invocations, and you cannot save or exchange data.
  • It supports various runtimes – Node.Js, Java, Python, .NET, Go, Ruby, Rust
  • Lambda functions should not be a long-running process; it can run for a maximum of 15 minutes.

Invoking Lambda Functions

aws_lambda1
  • You can invoke Lambda functions directly using Lambda Console, Lambda API, or SDK
  • You can configure your lambda function to be invoked in case of a cloud watch event.
  • S3 can be configured to invoke your Lambda function in case a bucket is created or an object is added/updated in a bucket.
  • Amazon SQS, Kinesis stream, and Dynamo DB can also send data in batches and invoke AWS lambda
  • It can also be triggered with HTTP requests using API gateway (API gateway is another Java Web Development service provided by amazon to expose services as API and handle HTTP requests).

When not to use AWS Lambda

aws_lambda2
  • When you need more control over your application such as operating system, security control.
  • When your code is written in some language other than provided by AWS Lambda.
  • When you need to run a long process (more than 15 mins)

Launching your first Lambda Function

Step1: Select Lambda Service

step1

Sign-in into AWS console https://aws.amazon.com/console/ using your AWS credential. If you are new to AWS you can create a new account. AWS provides 12 months of free tier access for selected services. The Lambda free tier includes 1M free requests per month and 400,000 GB-seconds of compute time per month. Once you are on AWS console, click on services and then select Lambda, this will take you to lambda screen.

Step2: Create Function

step2

Click on build a function to create your first Lambda Function.

Step3: Choose Options

step3

On the next screen, you can choose whether you want to write your function from scratch, or to build a lambda function using some blueprints/sample code for common scenarios provided by AWS or you can get a sample lambda application from AWS repository. For this blog, I choose the “Use a blueprint” option to continue.

Step4: Choose Blueprint

step4

Once you hit continue you will get several blueprints available to start with and these blueprints can make your life easy since a lot of code is already written for some common scenarios. You can select any blueprint based on your requirements and language dependency and then configure the code accordingly. For example

S3-get-object-python: This is blueprint is written in python and it triggers the lambda function to retrieve the metadata for the object stored in configured S3 bucket whenever that object is updated.

Microservice-http-endpoint-python: In this blueprint/template, Lambda function is triggered by APIs’ (HTTP Requests) and these APIs’ are exposed to outer world using API gateway. This lambda function can read/write data from/to dynamo DB to serve those APIs’ calls.

Similarly, there are hundreds of blueprints available to start with but for this blog, I selected “s3-get-object-python” and then clicked on configure button on bottom right of screen.

Step5: Basic Info

step5

On this screen, you have to provide basic information about your lambda function such as function name. You also need to set permission for your AWS Lambda function to access S3 buckets so you need to create an appropriate IAM role for the same. In the execution role, you can choose whether you want to create a new role with basic permission or use an existing role, or create a role with some existing policy templates.

Step6: Role info

step6

When you scroll down on the same screen, you are required to provide role information such as role name and to select policy template, which will contain all access permissions that will be provided to your lambda function. For the selected blueprint, “S3 read-only permission” is already set, you can add other permissions such as providing read/write access to dynamo DB, SQS permission, and many more if you want to integrate your lambda function with other AWS services. For this blog, I will go ahead with S3 read-only permission.

Step7: Trigger Info

step7

In the next section on the same screen, you will be required to provide the trigger information, which means by what event your lambda function will be triggered. In this scenario, we have to define the S3 bucket name, event type (such as PUT/POST/DELETE, etc.) of a new object and you can define the prefix and suffix. For example, I configured that my lambda function should be triggered whenever any new object with a suffix of “.jpg” is created in my S3 bucket named “s3sample22”.

Step8: Lambda Function

Lambda Function Code import json import urllib.parse print('Loading Function') s3 = boto3.client('s3') def lambda_handler(event, context); #print("Received event:"+ json.dumps(event, indent-2)) #Get the object from the event and show its content type bucket = event['Records'][0]['s3']['bucket']['name'] key = urllib.parse.unquote_Plus(event['Records'][0]['s3']['object']['key'],encoding='uttry:) response = s3.get_object(Bucket-bucket, Key-key ) print("Content Type:"+response ['ContentType]'') return response['ContentType]'') except Exception as e: print(e) print('error generating object{} from bucket{}. Make sure they exist and your bucket') raise e

In next section on same screen, you can see preconfigured lambda code provided by a selected blueprint. This lambda function is written in python and is primarily reading the object from S3 bucket and then returning its content type to API. You can further configure this lambda function according to your need. You can also modify your lambda function later on. Finally hit the create function button on bottom right of your screen to continue.

Step9: Final Screen

step9

This will be the final screen for your lambda function where you can edit your lambda code, add new triggers, test your code by creating S3 events, set the environment variables, and can even change the runtime from python to any other language. You can view logs in Cloudwatch by clicking on the Monitoring tab.

Conclusion

Lambda functions are undoubtedly innovative, low maintenance, low cost, scalable, and effective way to create Dynamics CRM services without carrying the burden to host any servers. Lambda functions are the future of serverless applications.

Empower your Business with Team Aegis, CONNECT NOW!
captcha

Scale your Business with our Software Services Now!
Related article

Many Fortune 500 organizations are adopting AWS to deploy Java applications services, however,

Yes, cloud computing has indeed transformed the work activities of many sectors during the last decade. Many business owners, however, are still wary of the notion of transferring their activities to a virtual platform. It is normal to feel worried about making a change. However, studies and real-world experiences demonstrate that the advantages of the cloud exceed the dangers, which are in any case insignificant.

What is Lambda Expression in Java (Lambda Expression Syntax and details about it)?

DMCA Logo do not copy