Performance Testing With AWS Lambda

  • On April 13, 2018

Getting started

The first thing we have to do is to create an AWS account – once you do this your account is subscribed to all services in AWS, but you are only charged for the services you use and then create an administrator user in your account. Next thing while still here is to set up the AWS command line interface (AWS CLI). In this example, we are going to create a random number generator that gives us random numbers whenever we call it.

Configuring Lambda function

Creating our function

First up, log into the AWS management console and then click on the Lambda console.

Now Lambda offers a simple Hello World function so that you can get how it works. At the top right, next, we are going to click on get started now. We are going to choose a runtime; here we will pick the latest node.js and not the old node.JS. Below these options are a number of blueprints that Amazon has given us, we are going to go for the simplest which is hello world, click on it.

In the configure trigger section we are going to leave it as it is. We can connect the trigger function to an API gateway later. So skip it and click next.

Once here we are now we going to name our function random-number generator. The description and the runtime will be left will be left as they are since we had specified them previously.

Upload code to Lambda

Now there are three ways of uploading code to lambda. You can either;

  1. Edit code online for smaller snippets of code such as this one.
  2. Upload code in a zip file
  3. Upload a file from Amazon S3

We are going to stick to editing code online.

  1. Enable strict mode not really required
  2. The loading function is used for debugging later on.

We are going to delete some code that Amazon gave us and we are going to write our own code.

Generate randon numbers

First, we are going to define a minimum number that is 0 and the max value which is 10.

Then we are going to generate our numbers and define new variables we are going to call it generated a number and we are going to use math.floor for this and then math.random multiplied by max value and add the minimum value. The reason for this is because math.random only generates random numbers between 0 and 1.

Our final code on line 10. The callback takes two parameters firstly the error message and a success message. So since no error, we are writing null and our success message is the generatedNumber.

That’s it for our code. It should work.

Next, we define our handler. Index refers to the file name in our case index is the index.js file.

Handler is the name of the variable that you attached to exports.

So we are going to leave the handler as it is and we are going to create a new role to execute this function and we are going to name it basic-lambda-execute-role .

Assign permissions

We are going to give it some permission on policy templates; here you can give it any permissions you want. We will give it the simple Microservice permission

Next thing is the advanced setting; we are going to pick how much memory should be allocated your function so 128 MB is more than enough because of our simple function. So we leave them as they are and set our time out correctly.

We are not going to configure VPC so we click next. On this page, Amazon lets us review the details of our function. So we are going to click create function

And we are done.

Load testing

When we run the code, lambda will give us random numbers generated by our code. Once we run the code. Lambda shows the code, configuration, triggers and finally monitoring. We can connect the trigger to an API gateway. If we click test and scroll down we can see the results of our function, the log output, how many resources we used, the memory allocated and the time it took to run.

Connect to Lambda

When we want to connect lambda to an API gateway we can do it in two ways;

  1. we go to triggers and add a trigger, choose API gateway

We are going to call it randomgenerator.

Under resource name, we call it number. Get method and deployment stage are left as they are finally at security we pick open so that the API can be invoked by API. Finally, click submit and that’s it!

When we open the APIs URL and open in a new tab the random number generator has a webfront.

  1. We use the API console instead of the lambda console

Once here click on create API. Let’s give it a name e.g. random-generator-2 then click on create API

Now here we can configure our resources. So click actions and create a new resource call it number

because of our random number generator. Click create resource.

Next, click actions and create a new method for this resource and add the get method just like the previous way. Next, we can choose the integration type

Once here, choose to connect to a lambda function since we already have a function. Next, pick a region and finally the name of the function. Then click save. That is it we are done.

Click Actions, deploy API create a new stage and call it prod. And click deploy. To check it. Click prod then number then GET. It will give you a URL and if you click on it. It generates a random number.

Conclusion

Now invoking your functions in lambda is costly. The first one million requests monthly are free but once you exceed that, you pay $0.20 for each one million request. So that means you have to be careful with the timeout time you set as well as the number of requests your functions execute.

The other thing you have to watch out for is time. This depends on the number of executions and memory allocation. For proper testing of your applications, you have to have the following 3 things:

  1. An appropriate dataset
  2. A test scenario
  3. Properly simulated infrastructure

While testing locally, you can simulate a lambda environment using either a serverless framework or Docker combined with lambda.