Using the Serverless Framework to Deploy AWS API Gateway and AWS Lambda to AWS

Rocky Top Solutions
Jun 30 · 5 min read

So, you have heard all the recent hype around serverless based applications and was thinking about delving into it to see what all the hype is about? Here’s a quick simple way to get started in serverless using the Serverless Framework to deploy a serverless application to the AWS Cloud using AWS API Gateway, AWS DynamoDB, and AWS Lambda.


The first thing you will need to do is make sure you have Node.js version ≥ 8 and npm installed on your machine. To check whether it is installed, run the following command inside a terminal shell.

node -v
npm -v

If you do not have Node.js version ≥ 8 installed, there are a couple of ways to install it. The first and easier way is visiting the Node.js download web page and download the version you wish to install.

The other way, which is just a little more involved, is to use NVM (Node Version Manager). NVM allows you use have multiple versions of Node.js running on a single system and easily switch between them. To install NVM, run the following in a terminal shell:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

This will install NVM into the current user’s profile instead of being system wide. You will need to quit and restart the terminal application in order for the terminal to pick up the nvm command. Now we need to install Node.js using NVM by doing the following:

nvm install {node version}
nvm use {node version}

The next to do is install the AWS Command Line Interface (CLI). To do that, you can follow this guide to get that setup. Now lets setup our AWS credentials so we can actually deploy this sample application to AWS. If you don’t already have a Access Key/Secret Key pair of AWS IAM credentials, you will need to get that first and make sure it has the permissions to create/update/delete CloudFormation templates, API Gateway resources, and Lambda functions. Once you have you AWS credentials, follow this guide to setup your credentials on your local machine so that the Serverless Framework can use them to deploy to AWS.


The next thing you’ll want to do is install the Serverless Framework. You do this by running the following command inside a Terminal shell:

npm install serverless -g

This will install the Serverless Framework as a global package for your machine. If you wish to just install it inside a project’s directory as a dependency of a node application, you can leave off the -g part of the command when you are inside a node project directory.

Next, you will want to create a serverless application. There are many different parameters to customize the serverless project for using different languages, cloud providers, etc. However we will be using values to create a Node.js/Typescript based application with AWS as cloud provider. To create and initialize this project, we will run the following commands inside the terminal shell:

serverless create --template aws-nodejs-typescript --path {my-service}
cd {my-service} && npm install

Now open the new {my-service} directory. You should see a project structure that looks like the following:

The Serverless Framework packages your application into a zip package that is deployed via a AWS CloudFormation stack. If you open the serverless.yml file, you will see something like the following:

service:
name: my-service
#app: your-app-name
#tenant: your-tenant-name
# Add the serverless-webpack plugin
plugins:
- serverless-webpack
provider:
name: aws
runtime: nodejs10.x
functions:
hello:
handler: handler.hello
events:
- http:
method: get
path: hello

Let’s go over the different pieces of this default template.

  • service: This will be used to create the name of your AWS CloudFormation stack. The default naming convention of the Serverless Framework is to use {service.name}-{stage} where stage value can be passed in via command line argument or use default value of production.
  • plugins: This section allows you to extend functionality of the Serverless Framework using multiple plugins from a list of available plugins. There are plugins for many different tasks such as adding tags to AWS API Gateway stages, API Gateway APIs, creating custom domain names in API Gateway, using versioning in AWS Lambda along with auto pruning old versions, and many many others.
  • provider: This section specifies all configuration for the cloud provider you wish to deploy your Serverless application to. As of this writing, they support deploying to AWS, Microsoft Azure, Google Cloud, IBM OpenWhisk, Kubeless, Cloudflare, spotinst, and fn as can be seen here. We will be using AWS, whose Serverless config file options are all listed here.
  • functions: This section is where we configure our AWS Lambda function specification. This section will tell serverless where to find the code in your local project that will comprise your AWS Lambda function, as well as what AWS API Gateway endpoints will use the Lambda function, what tags and environment variables to assign to the Lambda function, etc.

Now, in order to deploy this sample application to AWS, all you need to do is run the following command:

serverless deploy -v [-r {desired region}]

Congratulations! You have deployed a Serverless based application to AWS API Gateway and AWS Lambda! In order to test this out, sign into the AWS Cloud Console, select the region you deployed your application to form the upper right menu, then go to the API Gateway service page. You should see your serverless applicaiton API listed on the left menu. Click that API, click Stages in the left sub-menu, and then click on the deployed stage. You should now see an Invoke URL link for your API in the right half of the page. Copy that link, paste it in your browser and append /hello to the end of that URL and hit Enter/Return. You should get the desired response that was created in your handler.ts file from the template application!


Now that you have a feeling for how easy it is to deploy Serverless applications to AWS, leave your feedback in the responses section and let me know what you all do with this framework and what you all think of this tutorial!

Rocky Top Solutions

Updates and news from Rocky Top Solutions, LLC

Rocky Top Solutions

Written by

We help business turn their software, web, and mobile development ideas into reality!

Rocky Top Solutions

Updates and news from Rocky Top Solutions, LLC

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade