AWS Lambda Basics: Part 1
Tags: aws, java, lambda
Published: 2021-03-15 17:17:08

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](https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html).
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](https://github.com/prabhjeet6/ThumbnailGenerationLambda) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/imc2bpnpvgpoqs9lrvco.PNG)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m181ab0q97paje6piolk.PNG)
 
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dzlpqf9fsvzu29gslr9x.PNG)

Click on create Role

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vs8fbiklfu8szvar7b9a.PNG)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g9l86pazm78hnyqr8vei.PNG)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5bjwu1zjn5jzucb5clel.PNG)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1djy2mc1pf0v827lpbzq.PNG)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/buz642nkkto000iornty.PNG)

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](https://dev.to/prabhjeet6/aws-lambda-basics-part-2-1301) for part 2 of the blog post.