Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
AWS Lambda Quick Start Guide

You're reading from   AWS Lambda Quick Start Guide Learn how to build and deploy serverless applications on AWS

Arrow left icon
Product type Paperback
Published in Jun 2018
Publisher Packt
ISBN-13 9781789340198
Length 184 pages
Edition 1st Edition
Languages
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Markus Klems Markus Klems
Author Profile Icon Markus Klems
Markus Klems
Arrow right icon
View More author details
Toc

AWS Lambda

In the previous section, we learned about the Amazon Web Service Management Console. Now we will look into AWS Lambda. Here, we are going to take a look at the Lambda web dashboard, the Lambda function blueprints, how to configure and deploy the Lambda function, and how to test it—all from the Management Console. So let's head over to the Lambda service.

To learn how to access the Lambda service, read the AWS Management Console section.

Once you are in your Lambda web dashboard, click on the blue Get Started Now button to create your first function.

You can select from a number of blueprints, which give you Lambda functions with a little bit of dummy code. You can filter by runtime, and since this is volume one, we want to use Node.js. So click on Node.js 4.3:

Let's use the Blank Function blueprint. If I want, I can create a trigger that triggers my Lambda function. There are different kinds of triggers, but for the first exercise, let's not select any trigger. Let's leave it empty and just click on the Next button.

Now we need to enter some configuration information for our function, such as a function name:

The runtime is correct, so we will scroll down a little bit. Here you can see that I have a function handler, as shown in the highlighted portion in the following code:

exports.handler = (event, context, callback) => {
// TODO implement
callback(null, 'Hello from Lamda');
};

This function will be assigned to the exports.handler property, which is then exported through the Node.js export. My Lambda function handler takes up to three arguments. The last argument, the callback, is optional. The first argument is my event, so my Lambda function; is triggered through an event. The caller of my Lambda function can pass in information. For example, if an S3 object-created event invokes my Lambda function, I can retrieve object metadata. If an HTTP request invokes my Lambda function, I can retrieve, for example, a JSON body from the HTTP event. The second object is the context of my Lambda function, I can access runtime information through this context object. Last but not least, the optional callback function is the typical Node.js error-first callback function. I can invoke it in this case without an error, so I will set the first parameter, or the first argument, to null. I also set the result, the second argument, to Hello from Lambda. So the caller will retrieve the message Hello from Lambda when the Lambda function is invoked.

What we also need to do is set the right permissions for the Lambda function. So scroll down to the Lambda function handler and role. Click on the Role dropdown, and create a custom role. Select lambda_basic_execution in the IAM Role dropdown and click on Allow. This will set the role to Lambda basic execution, as shown in the following screenshot:

You can even configure the amount of memory that you want to use in your Lambda function by scrolling down. Remember, the more memory you give it, the faster it executes, but the more you have to pay. Let's stick to the smallest amount, 128 megabytes. You can also specify a timeout so that if the Lambda function doesn't terminate within this amount of time, then it times out. Let's leave it at the default of three seconds.

Scroll down and click on the Next button. Have a look at the settings, scroll down, and click on Create Function. You will be able to obtain similar details to those shown in the following screenshot:

Congrats! You have created your first Lambda function! Let's test it.

Click on the Test button and this will execute the test that results in Lambda saying Hello.

You can also configure your test event in this way and give it sample event data. Since I don't use the event in my simple Lambda function at all, it doesn't matter what is pasted—simply click Save and Test.
You have been reading a chapter from
AWS Lambda Quick Start Guide
Published in: Jun 2018
Publisher: Packt
ISBN-13: 9781789340198
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime