September 23, 2019
Creating a first project (Serverless framework tutorial, #6)

Creating a first project (Serverless framework tutorial, #6)


Hi, everyone and welcome back to the serverless series in the last video we created our aws credentials and in this video we’ll be creating our very first serverless project and explore the structure of a project Now we don’t have to setup everything ourselves because serverless has a built-in create command that can do this for us So let’s pull up the help of this command and see what options it takes, so I’m going to have sls create – – help and here we can see that it takes a template a Path and a name you can also see that the framework ships with many templates already available These includes templates for nodejs applications on aws lambda as well as other languages on aws Lambda But also other platforms such as azure and openwhisk Now for this series will be creating a node.js application, so let’s create one right now I’m going to start by creating a new directory on my desktop, and I’m going to call this awesome-api and I’m going to change into that directory and now I’m going to create a new serverless project, so I’m going to type sls create then I’m going to say that I want the aws nodejs template and that’s it I’m going to hit enter and now serverless has created our application. It says successfully generated a boilerplate template for aws and nodejs So let’s open up this directory in visual studio code to see what it looks like so I’m going to open on my desktop awesome Api and serverless has created three files for us. It has created a git ignore file which tells get the source control management system which files to ignore. it created a handler.js file which contains a source code for a simple function and a serverless.yaml file which is the project configuration file. Now the function that they have created is really simple it just Exports a hello function and this will receive details about the event that triggered it some context and then a callback function that it should call whenever it is done executing Now as I’ve said this function is really simple it just makes a response object with a status code of 200 and it passes along a body Now the status code is an HTTP status code that will be returned to the client so in this case 200 means that everything went fine, but we might also return a 404 to indicate that a resource wasn’t found or a 500 to signify that there was a server error Now in the body we can return any string that we want and in this case They just take a json object they stringify it and then they set that to the body after creating this response object serverless returns it to the user by calling the callback function and passing along the response now the callback function has two parameters it takes an error and a success message the error message shouldn’t be used because serverless framework uses the Lambda Proxy integration by default if you want to return an error simply change the status code to an error code such as for example? 404 or 500 i will talk about functions more detailed in a later video when we are creating our own function. Let’s now take a look at the serverless.yaml file this file contains all the configurations for your project so it starts by giving a name for your service in this case it’s aws-nodejs, and it says nodes update this with your service name So I’m going to start by doing that I’m going to say the common project awesome-api there you go and This file also contains some other parameters such as what our cloud provider is in this case at aws What run time that we want in this case? it’s the latest version of nodejs that is available on aws laminate and it also contains the definitions for our functions So here we declare a function with name hello, and we say that the handler for this function is in the handler.js File which is this one, and it’s the exported hello function. Which is this one? Now again more on this later as we will create our own functions Now is also a good time to make sure that you define the aws region that you want to use, so if I scroll up In the provider section you can see that It has a stage and region variable and so by default the region is us east one if that’s okay for you You can leave that untouched but for me. I want to use the eu region, so I’m going to use eu-west-1 and you can also specify the stage that your project is in so for example You might have multiple branches in your git repository and one branch might be a staging environment and the other branch might be production So in this case by default it says is the development environment you could also call it prod for production or any other stage name that you want and this will get appended to the name of your functions that serverless deploys Now also notice all the other comments in this file These show all the different things that you can configure for instance all the way at the bottom. We have a section called resources over here and Here you can specify all the aws resources that your functions need So save your app needs an S3 bucket to store user uploads well, then you can define that bucket here and in fact they give you this example they say that new resource is of type aws S3, bucket and The bucket name is my new bucket The nice thing about this is that serverless will deploy these resources for you And it will use cloudformation to do so and that means that your infrastructure changes will be versioned and if something would go wrong it will automatically rollback the changes So I’m going to save this file and that was it for this video We’ve created our very first project and look at the structure of the generated files in the next video I will show you how to deploy this function and Also how to run it offline on your machine for testing purposes Thank you very much for watching if you like this video leave a thumbs up subscribe to my channel And I’ll see you in the next video

11 thoughts on “Creating a first project (Serverless framework tutorial, #6)

  1. Really learning a lot, Savjee – thank you!

    However, when I created the new serverless service, the handler generated is of the asynchronous type with no callback. Any ideas why it differs from yours?

Leave a Reply

Your email address will not be published. Required fields are marked *