Create a simple serverless stack from scratch with Serverless Framework

Tomasz Łakomy
InstructorTomasz Łakomy
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

Creating resources in AWS using the AWS Console gets tricky after a while - after using the UI in order to build an architecture of our app, it can be really complicated to replicate that same structure in another region for example. There's also another issue - there's no clear place where we can see what our stack looks like.

Serverless Framework is a way of addressing that - it simplifies our workflow of building serverless applications in the Cloud.

To quote the Serverless Framework docs:

The Serverless Framework gives you everything you need to develop, deploy, monitor and secure serverless applications on any cloud.

In this quick lesson we're going to learn how to install the Serverless Framework, how to create a new project and lastly we'll deploy a Lambda function to AWS Cloud.

Instructor: [00:00] In order to get started with Serverless Framework, run npm install -g serverless in order to install Serverless Framework on your machine. Once this is done, we're going to see a notification that a Serverless Framework has been successfully installed on our machine.

[00:14] In order to start our first project, we could run the serverless command. Just to make sure, we're going to run serverless --help, in order to verify whether it was successfully installed. Since we do get the help information, we are good to go.

[00:26] Just to make sure, in order to use the Serverless Framework, we have to have the AWS CLI configured. If I'm going to run aws configure, we are going to see that I already have the secret access key, as well as a default region name, and a default output format. If you don't have that, you have to configure the AWS command line interface beforehand, before moving on with Serverless Framework.

[00:47] With that out of the way, let us run serverless in our terminal. It's going to ask us whether we want to create a new project. We do want that, so I'm going to say, "Yes." Next up, it's going to ask us, "What do I want to make?" If I want to make a simple project with Node.js, Python or something other. I'm going to go with Node.js. Next, "What do I want to call this project?"

[01:05] I'm going to go with "Hello Egghead." Next up, it's going to ask us whether we would like to get a free serverless account in order to be able to monitor, troubleshoot, and test our new servers. This is of course very useful, but we don't need that for now, so I'm going to go with "No."

[01:20] Now our project has been created. Of course, we can always run the serverless command again if we would like to connect this project to our free servers account in the future. Now our project has been created and we can see a bunch of files that was created for us. Let's open up our code editor and see what's inside.

[01:33] I'm going to open my Visual Studio Code. Our first serverless project has three files. First up, there's the gitignore, which we're going to ignore for now. There's also a handler.js, which contains a body of a very simple lambda function.

[01:45] This lambda function is going to receive an event, return a status code of 200. Inside of the body of the response, it's going to say, "Go serverless 1.0your function has executed successfully," and it's going to return this event that was provided to it.

[01:58] The most important file is the serverless.yml. This is the core of serverless framework. This is the main config file that we're going to use for our servers. Basically, this file is going to contain everything that we want to have deployed in AWS Cloud. As you can see, most of this file contains useful comments showing us what exactly can we do with Serverless Framework in the future.

[02:17] For now, we're going to start very simple. We have a serverless name of "Hello Egghead." We're also going to use a provider. This provider is going to be AWS, because Serverless Framework supports multiple cloud providers including AWS, but also Google Cloud, and other cloud providers.

[02:31] We're going to use Node.js 12 in order to run our lambda functions. Speaking of functions, if I scroll down, we're going to see the definitions of our functions. Right now, we have a single lambda function which is called "Hello," and its handler is handler.hello.

[02:44] If we open up our lambda function in another window, we're going to see that the function that this handler points to is the file name of handler.js and the function Hello. We can see that over here -- handler.hello.

[02:55] Since we do have a serverless stack defined in the serverless.yml file, we can go ahead and deploy it. In order to do that, we're going to run serverless deploy. You can also run sls deploy, which basically is going to do exactly the same thing. I'm going to go ahead and run it. This is going to take a second, because it has to create a stack and also deploy it to AWS Cloud.

[03:15] Now it's done, and we're going to see that we've managed to successfully deploy a stack into AWS. It's been deployed to us-east-1 region, which is the default one in this case, and we've managed to deploy a Hello World function that we can see over here.

egghead
egghead
~ 2 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today