nSights Talks

Serverless Application Testing with Serverless Framework

Tutorial Highlights & Transcript

00:00 - Beginning of video
Hi, my name is Miguel Alvarado. I am a DevOps engineer. I’ve been at nClouds for almost a year now. I am going to present to you today serverless applications testing with serverless framework.
00:28 - What is Serverless Framework?
What is serverless framework? Well, serverless framework consists of an open source CLI, it has the dashboard. That means it’s a CLI that you can download and install on your local machine. You can create a local dashboard with simple commands. It also provides full Serverless Application Lifecycle Management. This means that when you want to deploy a serverless application, you can do it with serverless framework. You can do it from from scratch from the start until the end of the process that means that you can deploy your applications. Also, you can terminate those applications too. You can develop, deploy, troubleshoot, and secure serverless applications with less overhead and costs. This is thanks to the way the serverless framework works because we defined a configuration file where we centralize the configurations and we manage easier deployments than doing it with other tools. Also serverless framework is a multiple cloud provider. This means that we can use this framework either with AWS, GCP and Azure, too.
02:07 - Demo
Now that we have a better understanding, and an introduction of what is a serverless framework. Let’s get to the demo. I’m going to start with the serverless official website. It’s this one you can appreciate here. Well serverless has a bunch of functions as I was telling you, but the one we are going to see today is the Troubleshoot feature. That is a feature that helps us to test our serverless application before we go live, or even before we deploy to a cloud environment. To do that, we’re going to go to the documentation. Alright, we’re getting started. Here, we see the first steps, right? We see that we can install serverless by using NPM. Also, you can download the repository and install it with a Bash script. Although, I would say that this is the easiest way to do it.
03:26 - Creating & Testing Lambda Functions
We’re going to create two simple and basic Lambda functions and test them locally. We’re just going to create a serverless project. After that, we’re going to pass that function some code that we can test with this tool. First thing we should do is we should make sure that we are using the correct node version for our installation. In my case, I am going to use version 16.13.2. Next thing we we should do is we should create the serverless project. Serverless manages some templates. Let me show you. All of these templates are already pre-configured for you to add your code and deploy afterwards. Right now, we just want to add the code and test the code that we have written on the on our files. I am going to use a basic template which is AWS node.js. The code for this is serverless create dash dash template or hyphen template or hyphen T and the name of the template we want to use. I’m just going to grab the command from this page and I’m going to run this command. We have a hundred.js. This is the file where our code should be stored. We also have serverless.yml. This is a configuration file for the serverless framework to understand what we are doing. Here we specified the service that we’re using. In this case, it’s a JavaScript service, the Framework version, the provider we’re using. Remember, we can use multi cloud providers such as GCP, Azure, AWS, and other cloud providers. And we can also define IAM roles and a bunch of other configurations regarding the Lambda function itself. In this case, the configuration says that we are creating a function called Hello, and the handler that it’s using is handler.Hello. With that being said, we can pass the code. I’d like to show you how to test this. Right now, we are 100% sure that if we test this on a Lambda deployment on the AWS Lambda service, this should work. Because it’s only returning a status code, a message and an event. In this case, we’re not passing any events to the Lambda. The proper way to test this is with the command serverless invoke local. That way, we pass the function with the flag function, and we have to specify the function name. So let’s test it. Let’s run serverless invoke local-F, and the name of the function is specified on the serverless configuration, here in the serverless.yml. And the function is called Hello. That’s how we test a serverless application locally. As you can see, that’s quite simple. Although it could get a little bit more complicated.
07:01 - Testing the Serverless Application
I’m just going to show you some use cases. Right now, I am going to use this code. This code is on the official AWS documentation on how to create JavaScript with Lambdas. It’s actually a pretty basic Lambda, where we are just doing a GET request to a URL. This is an API that returns random images of dogs. We’re passing the URL and we’re receiving a response. After that, we are getting that response into a variable. We are doing a call back to the function so that we can return the response that is coming from the HTTP requests that we just did. Also, we are managing another part that handles the error and returns to the proper error message to the HTTP request in casesomething goes wrong. Now, we should be able to test this like we did previously. I’m just going to run again serverless invoke local-F Hello. And here, we can see that we get the HTTP response with a status of success. We can actually open the image and we should see a dog.
09:18 - Sending Parameters to Lambda
But what if we would like to send parameters to our Lambda, right? How we should be able to send information to our Lambdas. If you’re familiar with AWS Lambda, we should use the event prop. We should use event.URL. And that should be enough. But now how are we supposed to send it in our testing command? What we can do is specify the path where we have our event file. Usually, that event file is specified in a JSON format. I’m just going to create a JSON file here, just a simple JSON with a URL property. I am using another API that basically genderized the names. It says, what is the probability that a person is a woman or a man by knowing only the name. We have to specify the event file, and we go with event.JSON. And while that should be enough, here, you can see that the response of the request changed. We are sending the name Andrea. And the gender should be male with a probability of 62% or some other information that the API responds.
11:03 - Serverless Application with Python
I will also like to create a simple example with Python for you to see that this is a very flexible tool. So I’m just going to create this also, we are going to use a template. If you go over to templates, you can see that we have the AWS Python and AWS Python 3. I’m going to use the AWS Python 3. I’m going to create this serverless. If you already have a function that is already created, and you would like to do some changes and test those changes before you upload anything, I would recommend a simple serverless framework project, just as we are doing and replace the handler file with the code of your Lambda. Actually, you’ll have to do some configurations here on the serverless.yml and specify the handler that you’re going to use. That way you can test every change you want to do on your Lambdas before uploading anything to an environment. If right now when we test this here, we can see that the serverless configuration changed a little bit. Right now, the service is not JavaScript, it’s Python. The provider also changed a little bit with the runtime of Python 3.8. And the handler, it’s still the same. We can use the function Hello. Basically what this does is just returns an a status code with 200 and a basic message that this is executing successfully. I also have a little example, already working here on my test. What we are using here, it’s the same example we are just returning on a status code 200 specifying the header of a content type application JSON, and a body returns a region. But what’s different this time is that we are using an environment variable. We are getting the value we are storing it and then we are returning that value. We will this is just one many times to provide ARNs maybe some secrets or AWS regions to our Lambda functions. The proper way to test this is with a simple command serverless invoke local-F Hello. But this time, we’re going to use the -E. The E flag to specify an environment variable. The environment variable is called AWS region. Its value is going to be US East 1, the Virginia region. If we just test this, we should see a status code 200. And in the body, we should see the region that we are using, which is this one right here. I would say that sometimes we only use a couple of environment variables. It could be easy to specify this on the command. But sometimes we use too many environment variables, it could not be the best practice to specify all of the environment variables here. So what we can do is specify it here on the serverless configuration. With the environment property, we can specify different environment variables that we can use inside of the Lambda function without pecifying this E flag. Something that I didn’t explain at the beginning is that when we create a serverless configuration, what the serverless framework does is that when we deploy that configuration, it creates CloudFormation stack and executes it. That means that we can use CloudFormation functions here in the serverless configuration. So we can do a reference to an ARN or we could use function CloudFormation going back to the example. Right now if we test this but we remove the E flag with environment variable, we should see the same response because we are specifying this environment on the serverless configuration. And there it is.
Jasmeet Singh

Miguel Alvarado

DevOps Engineer


Miguel is a DevOps Engineer at nClouds and an AWS Certified Cloud Practitioner.