Source code: https://git.io/fjlWo

In this blog post, we will be creating our first function using AWS Lambda and understanding intricacies related to it.
Creating Lambda function
Step-1:
Login to your AWS console with a user having Administrator privileges and open AWS Lambda service. You should be seeing the landing page as below

Step-2:
Click on Create function button and enter following details in the screen that opens.
Let Author from Scratch be selected by default.
Enter Function Name = myFullName
Select Runtime = Node.js 8.10
click Create function button

You should see a message that confirms creation of our function

Step-3:
Scroll down below and you will see that a small sample code has been written for us in a file named index.js. Replace that code with below code block

Step-4:
Keep everything else as default and click Save button

This completes creation of a function in AWS Lambda.
As you would have inferred, we are building a simple function that takes input values of First name and Last name and will return Full name.
Testing Lambda function
Step-1:
Now let’s test this function to see whether it works. Towards your top right there is a button called Test. Before that button, there is a drop-down arrow with text select a test event. Click that arrow and select Configure test events option

Step-2:
In the screen that opens, enter the following values
Event Template: Select HelloWorld (if HelloWorld is not there, select MobileBackend)
EventName: TestMyFullNameFunc
Enter the following JSON value in box below EventName



Click Create button
Step-3:
You should be back to the function page. Now click Test button

If the function executed successfully, you should be seeing the screen like one below

This shows our Lambda function executed successfully and it was able to return us Full name by concatenating supplied values. It also shows other details regarding its execution.
Step-4:
Now click the link “logs” which is the last word in the Exection:result:succeeded line. It will open AWS CloudWatch logs page.

You will see a Log Streams record created. Click on that and it will open detail logging captured while execution of our function

Hence, we now know that AWS captures all the logs of Lambda function execution in AWS CloudWatch
Step-5:
If you come back to function page, you will understand why in Designer you see AWS CloudWatch Logs also along with our Lambda function

It is because this function is using AWS CloudWatch Logs service to log events.
Step-6:
Let’s make slight modification to our code in index.js, as shown below

Now again click Test button and open “logs” link and you should see the details like shown below

You can see it logs the Entry and Exit as well as the input values we passed to it. This is how we can understand what is happening inside the code of Lambda function, by putting log entries at right places.
Congratulations. You have completed creating your first Serverless function and running it in AWS cloud.
All this without hassle of creating Compute (VM), Storage (EBS), Networking (VPC,Subnet), Installing and configuring operating system, AMI creation, Software installation, Security configuration, patching, managing, maintenance.……….you get the drift.
This is where Serverless computing excels. You just leave all those low-level activities to your cloud provider and just focus creating what is important for your business
Step-3: Looking under the hood
Step-1:
Now let’s look under the hood and understand what all happened during this process.
If you notice, there is a text box called Handler, which was automatically filled with value of index.handler

Remember that this is the combination of File name (i.e. index.js) and function name (exports.handler)
You are telling Lambda location of your code (index.js) and starting point from where to start execution (exports.handler function)
Step-2:
Creation of Roles and Policies
Open AWS IAM service and click Roles as shown below

You can see that a role exists, with our function name FullName. Click on name of the role and another page opens up

This shows the policy attached to the role. Click on the policy name link and check the JSON created for the policy

If you are familiar with AWS IAM, you will understand that this policy allows the following two permissions
- CreateLogGroup
- PutLogEvents
This is useful while creating the CloudWatch Log group for our function and to log events when the function executes.

This is very important to understand. This means Each Lambda function you create, can do only those activities for which you have given it permission. Nothing less and nothing more.
Step-3:
Check Role association with Lambda
You can open your Lambda function and scroll down to see this role associated with it.

To recap, we created a Lambda function, and AWS did the following for us
- Create a Role
- Created a Policy and
- Defined Permissions in a JSON file and attached to Policy
Then when the Lambda function was executed during runtime, these Role along with its Policy and Permissions were used to create LogStreamGroup and log events into it.
Conclusion:
This completes our first small step in Serverless world. Let’s hope this small step turns into a Giant leap in future
Happy Clouding!
[…] our previous blog post, we created Lambda function and used AWS Console Test button to invoke […]
LikeLiked by 1 person
[…] Created/Deployed Lambda function and […]
LikeLiked by 1 person
Oh man What might I say? Really… really loved the blogThank you so much!
LikeLiked by 1 person
you are welcome Francis 🙂
LikeLiked by 1 person