Deploy an AWS Lambda Function with the Serverless Framework

Lukas Ruebbelke
InstructorLukas Ruebbelke
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

You can create endpoints without drastically changing your function writing workflow by using the Serverless framework.

With Serverless you can generate a new handler function from the command line. Then, you can change the configuration, write your function, and deploy it to AWS.

Instructor: [0:00] Let's take a moment to talk about streamlining our development workflow in the Cloud using a framework like serverless. It's not my intention to turn this into a serverless framework course.

[0:12] I do believe that as developers, the closer we can get to our pre-existing workflows, the better off we will be. We won't be going in depth about installing the serverless framework, but there's a solid right up on their website about how to do that.

[0:28] Let's assume that you've already installed the serverless framework and integrated it with AWS. You can verify this using SLS, which is an alias for serverless-V. You can see in this case that I do, in fact, have the framework installed.

[0:47] With that verified, let's go ahead and create a project. We'll use SLS create-t for template. We'll use the AWS node template. Finally, -p for project, and we'll call this project Egghead Streamline API. That's going to generate a project for us. Now, we can step into this project we just created and open it in VS Code.

[1:15] Inside of my project, you can see that there are three files. The first file is your handler.js, and it's very similar to the previous example. It's a handler that takes an event and returns a response object. The next file is the serverless.yml file, which has quite a bit, what I would call, cruft in it. It's serverless trying to be helpful, but in this case, we don't need it.

[1:40] We'll delete everything in the comments. When we're done deleting all of that, we should have about 12 or 13 lines of YAML remaining. That's all we need to get started. This is going to define the infrastructure that we're going to use to deploy this Lambda into the Cloud.

[2:01] From the command line, let's use sls deploy. This will take everything, zip it up, put it into S3, and finally, deploy it into the Cloud on our behalf. The steps that we've done in the console up to this point are now being done behind the scenes. Now, we can move over to the AWS Console inside of our browser.

[2:25] If we Refresh this, we'll see that we now have our function inside of the service, which we can step into and check out. You can now see that the function that was in our code is now in the Lambda. There are a number of ways that we could go about testing this. In this case, let's do it from the command line.

[2:45] From the terminal, we use sls invoke -f hello. Hit Enter, and you will see the response object inside our terminal indicating that we have been successful. The next thing we want to do is to expose this as an API endpoint using the API Gateway.

[3:02] Fortunately, this is even easier using the serverless framework. Underneath the handle of property, let's add in an events property. Then like so, we'll define an HTTP API entry. From there, we're going to double tap over. This is really important.

[3:24] We're going to add in our route, which this case is going to have a root path of forward slash and use the getMethod. Let's go ahead and deploy this from the command line with sls deploy.

[3:40] Once this is fully deployed, you will see that we have an endpoint that we can click on and see in the browser. Not only do we have our messages, but we can also see the event itself with a bunch of interesting details.

[3:55] If we go back into our AWS console and refresh the page, you'll see that along with our Lambda, we also have a API Gateway that is attached to it with all the details, such as an API endpoint, it's security details, etc., etc.

[4:13] Let's go back to our Egghead Hello API function that we created previously. We are going to copy the event itself, the schedule, and the pretty print method. We'll go into our handler here and we'll paste this all in.

[4:37] Once this is pasted in, we'll take the response object and modify it, like so, to return the result of calling prettyPrint() with the schedule. From here, we can go back to the command line and deploy it with sls deploy.

[4:57] After this is deployed, we can click on the endpoint link that's generated, and we should see our weekly schedule. Back in our AWS Console, we can navigate to our streamlined API definition.

[5:17] When we look at the code inside of this Lambda, you'll notice that as expected, the code that we deployed from our editor is now inside of the console.

[5:30] I want to show you one other thing quick, and that is how to spin down a deployment. Using sls remove from the command line, everything that was defined within our YAML will be removed. If we head to the AWS Console and go back to our functions and Refresh, you'll see that our function is no longer there.

[5:57] This has been a quick tour of how to accelerate your development workflow using the Serverless Framework. What I appreciate about this approach is that, when I'm using the Serverless Framework, it's business as usual. I feel like I'm programming how I always do, but somehow, my code ends up in the Cloud. I think it does a great job of shortening the distance between function and endpoint.

egghead
egghead
~ 13 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