axcess.io
Published in

axcess.io

Understanding AWS Lambda the other way!

AWS Lambda through definition.

AWS Lambda is an event-driven, serverless computing platform provided by Amazon as a part of the Amazon Web Services. It is a computing service that runs code in response to events and automatically manages the computing resources required by that code.

Let us try to understand the above definition in an unambiguous way as the definition seems to be very technical.

To understand how to write a Lambda function, you have to understand what goes into one.

  • A lambda has certain requirements, at the time you create a lambda function, you specify a handler, which is a function in your code, the handler is an entry point for the lambda function. A Lambda function accepts JSON-formatted input and will usually return the same.
  • The second requirement is runtime environment. While you create a lambda function(lambda function comprises of some codes), The runtime will usually correlate directly with the language you selected to write your function.
  • The another important requirement for a successful execution of a lambda function is Trigger. If you want to execute your code then you need to fire some commands in case of your local system. Here for AWS lambda also that is required and we usually invoke the code in response to an event. The event may come from S3/API Gateway/Cloudwatch/Manually through console etcetera.
This is how a sample lambda function looks like.

Understanding AWS lambda by doing!

Let us perform Some arithmetic operation and concatenation of strings through AWS lambda.

Our Sample input will look like this:

{
"Number1": 10,
"Number2": 20,
"FirstName": Govind,
"lastName": Sharma
}

We’ll be looking to receive the following in response:

{
"Number1": 10,
"Number2": 20,
"Sum": 30,
"Difference": 10,
"Product": 200,
"Quotient": 0.5.,
"FullName" : Govind Sharma
}

Here I’m using Python for demonstration, you may use any language of your choice that are available.

Here We go, for our first function!!

Name: That is going to be the name of the function that you will be using to identify the function at a later time.

Role: It defines a set of permissions for making AWS service requests. IAM roles are not associated with a specific user or group. Instead, trusted entities assume roles, such as IAM users, applications, or AWS services such as EC2, Lambda etc. Refer IAM for better understanding.

Go to AWS Console and Click on Lambda service, you will get this!
Go to edit code inline and paste the below code.

Here is the sample code:

import jsondef lambda_handler(event, context):
Number1 = event['Number1']
Number2 = event['Number2']
FirstName = event['FirstName']
LastName = event['LastName']
Sum = Number1 + Number2
Difference = abs(Number2-Number1)
Product = Number1 * Number2
quotient = Number1 / Number2
FullName = str(FirstName) + str(LastName)
# TODO implement
return {
'Number1' : Number1,
'Number2' : Number2,
'Sum' : Sum,
'Difference': Difference,
'Product':Product,
'quotient' : quotient,
'FullName' : FullName
}

Event & Context in Lambda function:

Event: AWS lambda uses this parameter to pass in event data to the handler. This parameter is usually of the Python dict type. It can also be list, str, int, float or NoneType.

Context: When Lambda runs your function, it passes a context object to the handler. This object provides methods and properties that provide information about the invocation, function, and execution environment. AWS lambda uses this parameter to provide runtime information to your handler. This parameter is of the lambda context type. Let us understand this with a sample code.

Go to configure test event and put the Input for those parameters.

Once you’ve entered the code, click on Save, and then click on the Test button to try it out. You should see a successful result like the one shown below:

The output console

In the next article we will learn how to invoke the Lambda function through various AWSservices.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Govind Kumar

Govind Kumar

Technology Evangelist | Head of Cloud Migration @Axcess IO | Cloud Arch. | RHC(SA/E) | AWS (SysOps/Developer) — Associate | CCNA | AWS Networking Speciality.