So, you’ve heard all the hype around serverless and wanted to delve in to see what all the hype is about? Here’s a quick simple way to get started in serverless using the Serverless Framework. We’ll be using AWS Cloud using AWS API Gateway, AWS DynamoDB, and AWS Lambda.
Initial Setup
The first thing you will need to do is make sure you have Node.js version ≥ 8 and npm installed. 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}
Installing AWS CLI
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 let’s setup our AWS credentials so we can actually deploy this sample application to AWS. If you don’t already have AWS IAM credentials pair, you will need to get that. Make sure it has the permissions to create/update/delete CloudFormation templates, API Gateway resources, and Lambda functions. Once you have AWS credentials, follow this guide to setup your credentials so that the Serverless Framework can use them.
Installing Serverless Framework CLI
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.
Creating the Serverless Framework Application
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-webpackprovider:
name: aws
runtime: nodejs10.xfunctions:
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}. The stage value can be passed in via CLI argument or 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. These include adding tags to numerous AWS resources, 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. 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.
To deploy this sample application to AWS, run the following command:
serverless deploy -v [-r {desired region}]
Conclusion
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 application 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!