0% found this document useful (0 votes)
18 views25 pages

Ex. No 10

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views25 pages

Ex. No 10

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Practical no 10

AWS Lambda - Overview & Environment Setup

AWS Lambda is a service which performs serverless computing, which involves computing
without any server. The code is executed based on the response of events in AWS services such as
adding/removing files in S3 bucket, updating Amazon dynamo dB tables, HTTP request from
Amazon API gateway etc.
To get working with AWS Lambda, we just have to push the code in AWS Lambda service. All
other tasks and resources such as infrastructure, operating system, maintenance of server, code
monitoring, logs and security is taken care by AWS.
AWS Lambda supports languages such as Java, NodeJS, Python, C# and Go. Note that AWS
Lambda will work only with AWS services.

What is AWS Lambda?


Definition of AWS Lambda as given by its official documentation is as follows :
AWS Lambda is a compute service that lets you run code without provisioning or managing
servers. AWS Lambda executes your code only when needed and scales automatically, from a few
requests per day to thousands per second. You pay only for the compute time you consume - there
is no charge when your code is not running.

How AWS Lambda Works?


The block diagram that explains the working of AWS Lambda in five easy steps is shown below:
Step 1: Upload AWS lambda code in any of languages AWS lambda supports, that is NodeJS, Java,
Python , C# and Go.
Step 2: These are few AWS services on which AWS lambda can be triggered.
Step 3: AWS Lambda which has the upload code and the event details on which the trigger has
occurred. For example, event from Amazon S3, Amazon API Gateway, Dynamo dB, Amazon
SNS, Amazon Kinesis, CloudFront, Amazon SES, CloudTrail , mobile app etc.
Step 4: Executes AWS Lambda Code only when triggered by AWS services under the scenarios
such as:
 User uploads files in S3 bucket
 http get/post endpoint URL is hit
 data is added/updated/deleted in dynamo dB tables
 push notification
 data streams collection
 hosting of website
 email sending
 mobile app, etc.
Step 5 : Remember that AWS charges only when the AWS lambda code executes, and not
otherwise.

Advantages of using AWS Lambda


AWS Lambda offers multiple benefits when you are working on it. This section discusses them in
detail:

Ease of working with code


AWS Lambda gives you the infrastructure to upload your code. It takes care of maintaining the
code and triggers the code whenever the required event happens. It allows you to choose the memory
and the timeout required for the code.
AWS Lambda can also execute parallel requests as per the event triggers.

Log Provision
AWS Lambda gives the details of number of times a code was executed and time taken for
execution, the memory consumed etc. AWS CloudWatch collects all the logs, which helps in
understanding the execution flow and in the debugging of the code.

Billing based on Usage


AWS Lambda billing is done on memory usage, request made and the execution, which is billed
in increments of minimum 100ms. So for a 500ms execution, the billing will be after every 100ms.
If you specify your AWS lambda code to be executed in 500ms and the time taken to execute is
just 200ms, AWS will bill you only for the time taken, that is 200ms of execution instead of 500ms.
AWS always charges for the execution time used. You need not pay if the function is not executed.

Multi Language Support


AWS Lambda supports popular languages such as Node.js, Python, Java, C# and Go. Theseare
widely used languages and any developer will find it easy to write code for AWS Lambda.

Ease of code authoring and deploying


There are many options available for Lambda for authoring and deploying code. For writing your
code, you can use AWS online editor, Visual Studio IDE, or Eclipse IDE. It also has support for
serverless framework which makes writing and deploying of AWS Lambda code easy. Besides
AWS console, we have AWS-cli to create and deploy code.

Other features
You can use AWS Lambda for free by getting a login to AWS free tier. It gives you service for free
for 1 year. Take a look at the free services offered by AWS free tier.

Disadvantages of using AWS Lambda


In spite of many advantages, AWS Lambda possesses the following disadvantages:

 It is not suitable for small projects.

 You need to carefully analyze your code and decide the memory and timeout. Incase if
your function needs more time than what is allocated, it will get terminated as per the
timeout specified on it and the code will not be fully executed.

 Since AWS Lambda relies completely on AWS for the infrastructure, you cannot install
anything additional software if your code demands it.

Events that Trigger AWS Lambda


The events can trigger AWS Lambda are as follows:
● Entry into a S3 object
● Insertion, updation and deletion of data in Dynamo DB table
● Push notifications from SNS
● GET/POST calls to API Gateway
Use Cases of AWS Lambda
AWS Lambda is a compute service mainly used to run background processes. It can trigger when
used with other AWS services. The list of AWS services where we can use AWS Lambda is given
below:

S3 Object and AWS Lambda


Amazon S3 passes the event details to AWS Lambda when there is any file upload in S3. The details
of the file upload or deletion of file or moving of file is passed to the AWS Lambda. The code in
AWS Lambda can take the necessary step for when it receives the event details. For example,
creating thumbnail of the image inserted into S3.

DynamoDB and AWS Lambda


DynamoDB can trigger AWS Lambda when there is data added , updated and deleted in the table.
AWS Lambda event has all the details of the AWS DynamoDB table about the insert
/update or delete.

API Gateway and AWS Lambda


API Gateway can trigger AWS Lambda on GET/POST methods. We can create a form and share
details with API Gateway endpoint and use it with AWS Lambda for further processing, for
example, making an entry of the data in DynamoDB table.

SNS and AWS Lambda


SNS is used for push notification, sending SMS etc. We can trigger AWS lambda when there is
any push notification happening in SNS. We can also send SMS to the phone number from AWS
Lambda when it receives the trigger.

Scheduled Events and AWS Lambda


Scheduled Events can be used for cron jobs. It can trigger AWS Lambda to carry out the task at
regular time pattern.

CloudTrail and AWS Lambda


CloudTrail can be helpful in monitoring the logs on the account. We can use AWS Lambda to
further process the CloudTrail logs .

Kinesis and AWS Lambda


Kinesis is used to capture/store real time tracking data coming from website clicks, logs, social media
feeds and a trigger to AWS Lambda can do additional processing on this log
etup

Before you start working with AWS Lambda, you need to have a login with Amazon console. AWS
Lambda supports two IDEs: Visual studio and Eclipse. In this chapter, we will discussabout the
installation of AWS Lambda stepwise in detail.

Create login in AWS Console


You can create your login in AWS Console for free using Amazon free tier. You can follow these
steps given below to create a login with amazon to make use of the Amazon services:

Step 1
Go to https://aws.amazon.com/free/ and click on create free account. You can see the screenshot
as given below:
Step 2
Click on Create a Free Account button and you will be redirected to the screen as shownbelow:

Now, fill in the details of email address, password and AWS account name as per your choice in
this form shown above and click Continue.
Step 3
Now, you can find the screen as shown below:

Enter all the required details in this form.


Note that there are minimum charges to be paid based on country selected. The same is refunded
once the details entered are validated. You need credit or debit card details to create the free account.
For Indian users Rs 2/- is deducted and for US $1 is charged.The same isrefunded to the respective
card user once the user is validated.
Please note the account is free and there is limit to the usage of the services. If the usage exceeds
the limit, the user will be charged for it.
Once the details are entered in the form shown above click Create Account and Continue.
You will be redirected to the next screen as shown below.
Step 4
You need to enter the payment details, that is either credit card or debit card, along with its expiry
date and the card holder's name as shown below:
Step 5
Once all the details are entered, click Secure Submit and it will validate the card with the bank
and will give you the OTP on your mobile which is linked with the card. You can find a window
as shown below:

Now, enter the OTP details and click Make Payment.You are charged based on the country
selected.
Step 6
Once the payment is done the next step is phone verification.You need to enter your mobile number
as shown below:

Once details are filled click Call Me Now. AWS will call immediately using automated system.
When prompted on call, enter the 4-digit number that will appear on your AWS site to your phone
using your phone keypad. This will verify your number and you will get the mail activation in the
mail id specified at the start while creating login.
Step 7
Click the mail link and enter the account name or email id and the password and login to you to the
AWS services as shown below:

The account name is displayed at top right corner as shown above. You can now start using the
AWS Lambda service. For AWS Lambda service the languages supported are NodeJS, Python,
Java, C# and Go.
Installation of Visual Studio 2017
There are 2 IDEs compatible with AWS: Visual Studio and Eclipse. In this section, we will
discuss installation of Visual studio 2017 on Windows, Linux Mac. Go to the official site of Visual
Studio : https://www.visualstudio.com/downloads/. You can find the welcome screen as shown:

Download the community version ie Visual Studio Community 2017 as its a free now for
practice. Once installed, it will run you through the installation steps where you need to select
packages to be used later. You can select nodejs, python, c# package for us to work later.
AWS Toolkit Support for Visual Studio 2017
Once you have Visual Studio 2017 installed, you will have to follow the given steps for installing
AWS Toolkit support for Visual Studio 2017:
Step 1
Go to https://aws.amazon.com/visualstudio/ and download the AWS toolkit for Visual Studio.The
display is as shown below:

Note that the package downloaded for Visual Studio 2017 is vsix package. If your visual studio
version is between 2013-2015, it will install a msi installer. Click the Download buttonas shown
below.
Step 2
Now, double click the vsix package downloaded and it will run you through installation steps as
shown below:

Once Visual Studio is successfully installed, you can see a window, as shown below:
Step 3
Now, open Visual Studio 2017 and you should see a welcome page from AWS as shown below:

Note that you need to add the access key, secret key, account number to get started and use the AWS
services from visual studio.s
AWS Lambda BoilerPlate for NodeJS
You can use it with visual studio code as shown below.

Step 1
You can download Visual studio code for free from the official
website:https://www.visualstudio.com/downloads/. The home page of Visual Studiodownloads
looks like this:
Step 2
Now, open Visual Studio code as shown below:

T
Step 3

To install support for AWS, support for nodejs option is available inside extensions. You can
search for AWS and it will display the option as follows:

Step 4
Now, install the boilerplate for AWS Lambda in nodejs as shown:
Step 5
Click the repository and clone it in Visual Studio to start writing the Lambda function in Visual
Studio. It redirects you to this repository which we can clone in Visual Studio :
https://github.com/loganarnett/vscode-lambda-snippets. Now, open command palette from View
option in Visual Studio.

Step 6
Click on it and choose git clone as shown below:
Step 7
Enter the repository url and save it as per your choice locally. Create index.js file as shownbelow
to work with lambda function:
InstallationofEclipseIDE
Now, you will have to install latest eclipse Java EE IDE.You can download it from Eclipse official
site: https://www.eclipse.org/downloads/
AWS ToolkitSupportforEclipseIDE
Once Eclipse is installed, perform the following steps:
Step 1
Go to help from the menu and click Install New Software.
Step 2
Enter https://aws.amazon.com/eclipse in the text box labeled Work with at the top of thedialog.
Step 3
Now, select the required AWS Core Management Tools and other optional items from thelist
shown below.

Step 4
Now, click Next. Eclipse will guide you through the remaining installation steps as given in the
further steps given below.
Step 5
The AWS core modules are displayed in the grid below as shown in the screenshot given
below:
Step 6
Once installed the AWS tool will be available in Eclipse as shown below:
Step 7
You can see the following screen when you click on the Amazon service.

Now, click on AWS Explorer to see the services available. We shall discuss how to work with the
installed IDE in upcoming chapters.

You might also like