Installing Serverless Framework

Part ɪ: Building Serverless Api│Story 04: Installing serverless framework toolkit

GIT : Repo | Branch

In the previous few posts, we created an AWS account, got an IAM user created and installed AWS CLI on our development machine.

We are now all set with the prerequisites and ready to start creating our Serverless API service.

We would use the popular serverless toolkit to create our AWS serverless service.

( From Serverless documents ) The Serverless Framework helps you develop and deploy your AWS Lambda functions, along with the AWS infrastructure resources they require. It’s a CLI that offers structure, automation and best practices out-of-the-box, allowing you to focus on building sophisticated, event-driven, serverless architectures, comprised of Functions and Events.

Installing Serverless toolkit

Install serverless toolkit:

Setting up Serverless API Project for AWS with Typescript

In the terminal, cd to your working folder where you would like to create the project folder for the api and run below command:

👆In the above serverless create command, we have specified aws-nodejs-typescript as template for our serverless application. The path has been specified as noteWordy-api

This would scaffold a basic Serverless application with typescript that is ready to be deployed to AWS. Because we used aws-nodejs-typescript as template for creating our serverless application, you would notice the scaffolded project would have handler.ts instead of the usualhandler.js file. Also the project folder would have for a tsconfig.json and webpack.config.js

📝 serverless.yml

In serverless framework, you need to define services. A service is like a unit of task organization for the framework and it comprises of a function, an event that would trigger the function and the resource(file.method) to trigger when function the service is called. And by convention, you define these services of your app in one file entitled serverless.yml (orserverless.json ).

Open the folder in your code editor and lets check the serverless.yml file :

In the provider section of serverless.yml, we would have aws set as our serverless provider and runtime is set as nodejs8.10

The function section currently has only one function - hello, which points to hello function defined in handler.ts. Its set to be triggered by get http event and the event handler is set to to the path handler.hello, which maps to hello function in a file named handler.ts

📝 handler.ts

Open handler.ts in the code editor. This file is currently exporting a single function hello that responds with a hard-coded message with a status of HTTP 200.

Test locally

Let’s test the function locally. The serverless framework provides a CLI command invoke local to test a function locally👇:

Above command would run the function locally and should respond with the expected message:

🎇 great! it’s working as expected.

Configuring AWS Credentials

We have already set AWS IAM credentials locally in our system using when configuring AWS CLI.

Serverless toolkit would use these AWS credentials that are configured on the system. However if you don’t have AWS IAM credentials set on your system or if you want to configure another IAM for serverless to use, you can configure it with below command:

Note the --profileargument in the above command, with this argument provided, serverless won’t override any default AWS credentials configured on your system. For Serverless to use this profile for deployment, add a profile property under the provider section in the serverless.yml file:

Deploying the application to AWS

To deploy our initial scaffolded serverless application to AWS, run the below command:

The above command would connect to AWS with the configured IAM credentials, create a CloudFormation stack, provision a S3 bucket and uploads the application to it. Along with, it would also provision the required IAM Roles, Log Groups, ApiGateway End-points, and Lambda function needed to run the service. By default, the dev stage is assumed. Note the provisioned endpoint url output from command. Open the url in browser: 👇

You can also invoke the function from the terminal using below serverless command:

Scripts for deploying to Dev and Prod environments

We would add a couple of scripts in package.json to help us easily deploy our serverless application in dev or prod modes.

In package.json, add the two scripts to the scripts object 👇

With the above scripts in package.json, now onwards we can simply run the command npm run devDeploy to deploy to dev, npm run prodDeploy to deploy to prod.

👆 Note the stage option in the two scripts above. In the next post, we would read this option in serverless.yml to tag the name of DynamoDB table to be provisioned, with the env/stage in which project is being deployed to.

☑ That completes our Serverless API project initialization and initial configuration. In the next post, we would add provisioning profile for the DynamoDB table in serverless.yml.

Prev:AWS CLI 🏠Next:provisioning DynamoDB

--

--