To make the most of this tutorial series, create a Serverless Framework PRO account for free
Let's go ahead and create our first serverless service using the Serverless Framework. To get started, I've already cd'ed into a project folder of mine and now I want to create a new folder for my first service project and we are going to essentially copy what we did manually inside the API Gateway, a console and Lambda console, but building it using the Serverless Framework instead. So I'm just going to create a hello folder here for myself. I then cd into hello and at this point I'm just going to create a new serverless service using the serverless create command.
The command I'm going to use is serverless create --templates aws-nodejs. What this tells the serverless framework is to bootstrap a very small service for me geared towards being deployed into AWS using the node JS runtime. After a second or two my boilerplate is created and now I'm going to get take a look inside my text editor to continue editing my configuration for my service.
I've opened up my text editor within that folder that I created and I can see that I've got three folders sitting here. Please ignore the coverage folder here as this is just a part of the extensions I have installed in my IDE, but let's take a look at the other files that are created automatically for me here.
gitignore is something we may use later and we can ignore this for now, but this just helps me when I want to use git to store the configuration of my servers to ignore the specific temporary directories that are created either by node or by serverless
handler.js; that sounds slightly familiar to us and this is where we will be writing our lambda function code and the serverless.yml file is probably the most important file to consider here and this is where we will be configuring our service to create the API endpoints and someone that we need in a portable and a reliable way. So let's continue with the serverless.yml.
You can see here there's a lot of commented sections here. I'm just going to skip to the first important line which is the service Hello. This is where we can name our service. I'm going to leave this as a hello as that is unique enough for our purposes and continue. In the provider section here, we named the provider that we are deploying to. In this case we are going to be deploying to AWS and what the Lambda runtime is going to be for our code, which is nodejs 10 in this case.
Continuing down, there's a section in the serverless.yml file where I can start specifying details about the functions that I'm going to be running. In our case, we really have a handler.js file with the dot hello function. The naming here links to the name of the file itself and I could put this inside directories as well. For example if I had defined in this way, that means I could have a folder, cold source, I could have another folder within that and I could have this handler file sitting in that functions folder itself. And then this would match. I know I'm going to leave it relatively simple, so I'm going to take it back out of there, but I could have it that way if I chose to just delete those folders, the dot hello portion links to the actual function name itself, the hello function here.
What you may not see here is how does AWS know how to run the code inside my function? And that is what we're going to add right now. We need to attach an event to our Lambda functions so that AWS knows, or the Serverless Framework knows how to instruct AWS on how to trigger the code in our lambda function. So to do that, we just going to add some additional configuration here and that's all we need.
Just bear in mind, this is yaml and if you're not familiar with yaml, just be careful of the indentation as yaml gets very specific on how you indent the items within it.
Remember with the API gateway that we set up manually previously, we could define the path that we wanted the Lambda function to execute under as well as the method that would affect it. So you can see here we are already duplicating the configuration we had before.
Now if I want to actually deploy this into AWS, I can run a serverless command. But first let's go ahead and edit our Lambda function behind the .hello file is where we're going to do that. So I'm going to open up my handler.js here and find my hello function, which is right here. And I'm just gonna edit this body to make it similar to what we had previously. Somehow take out the default values all the way to there. And I'm just going to add, just like we had before, hello from serverless, now we have everything configured for us to deploy. But before we do that in the next video, I'm just going to take a look at some of these events that I've been talking about in our serverless.yml file.
Obviously here you can see we have an HTTP event linked to our Lambda function here, but in the commented section below you can see there is reference to a lot more events that we could potentially be using. This is important to realize with developing services with the Serverless Framework and serverless in general.
Using all the events available to you in your cloud provider is a very powerful way to build serverless applications and we will be using a mixture of these in our own application that we will be building.
But just remember that this is a core premise of how serverless applications work is that we have code sitting within Lambda functions that are triggered by events caused within the cloud vendor that we use and obviously the HTTP events that come through.
So thinking of Lambda as a glue for all of these various services within the cloud vendor that we may be using is a very good way of thinking about how code works and how we reduce our dependency on having to write so much code.
[coming soon] S3