DEV Community

Cover image for AWS Lambda Basics: Part 1
Prabhjeet Singh
Prabhjeet Singh

Posted on

AWS Lambda Basics: Part 1

Usage of Cloud Computing services for Software development has been on the rise in the recent years, given its cost effectiveness due to economies of scale and the flexibility it provides in capacity utilization.

AWS provides services for various use cases and requirements. AWS Lambda is one such service. Here, we will discuss it with the help of an example.

AWS Lambda is a serverless compute service that lets you run code without provisioning or managing servers, creating workload-aware cluster scaling logic, maintaining event integrations, or managing runtimes. AWS Lambda can be categorized as Platform as a Service(PaaS) or more specifically as Function as a Service(FaaS) as it delivers a framework for developers that they can build upon and use to create customized applications.

Lambda runs your code on a high-availability compute infrastructure and performs all of the administration of the compute resources, It is highly scalable, and significantly reduces the development effort for your use case.

In simple words, Lambda is event driven service, and can be triggered in response to changes due to concerned event and do not require CPU resources to be manually managed to invoke it whenever such event occurs.

Lambda supports multiple runtimes, which can be found here.
We will use Java 8 as runtime for our purpose. In Our example, whenever a video is uploaded in a AWS S3 (Simple Storage Service) bucket, the Lambda function is invoked which generates a thumbnail image for it and stores it in another bucket. You can find the source code in the repository for reference.

Please note, AWS S3 is a service which provides cheap secondary storage on cloud.

On AWS Lambda Console, click on Create Function

Alt Text

On the next screen, give Function name, choose runtime of your choice and create execution role. Lambda creates an execution role by default, with permission to upload logs to Amazon CloudWatch Logs.

Please note, Amazon CloudWatch collects monitoring and operational data in the form of logs, metrics, and events, and visualizes it using automated dashboards.

For our example, We create a role beforehand via IAM Console, and Use an existing role option and choose the already created IAM role from dropdown.

Alt Text

To be able to select an existing role for above, create an IAM role through IAM Console beforehand.

Click on Roles link in IAM console as below.

Alt Text

Click on create Role

Alt Text

Next, choose Lambda as Use Case and click on Next:Permissions as highlighted below.

Alt Text

Complete the forms that follow. For our purpose, the role will comprise below policies. AWSS3FullAccess policy allows Lambda to observe when a read/write event occurs in S3.CloudWatchFullAccess policy enables to upload logs to CloudWatch whenever Lambda Function is invoked.

Alt Text

Click on +Add trigger from below and select S3 from drop down that follows. After the trigger is added, Upload the source code as jar file from Upload button under Code tab as highlighted below.

Alt Text

The Handler function as highlighted above, has the syntax

<package>.<class>::<HandlerFunction>

Under Test Tab, create a Mock event of S3-put Type from the drop down list. A JSON Body will come up, which is passed to Lambda Function as a mock input, Modify the input JSON body and update awsRegion, bucket's name, arn and object's key as below.

Alt Text

Click on Invoke button. The Test event is triggered and success/failure logs are generated in AWS CloudWatch.

This gives us basic understanding of working with AWS Lambda Console. Actual Implementation of the Lambda Function will be covered in a separate blog.

Click here for part 2 of the blog post.

Oldest comments (1)

Collapse
 
vishwaranjan06 profile image
Vishwa Ranjan

Keep up the good work ..