How to Build a Web Services with Serverless + Typescript + DynamoDB

In this article, we are focusing on the setup and use of three technologies to help you start developing Web Applications and Services. The technologies are Serverless, TypeScript and Amazon DynamoDB.

Serverless

Serverless is a deployment platform that focuses on providing a powerful and capable deploy system that supports multiple cloud platforms. Serverless (the platform) is not to be confused with the "serverless computing" ideology of using managed autoscaling cloud infrastructure instead of server-based cloud infrastructure. 

Serverless (the platform) is an implementation of the "serverless computing" ideology. 

Serverless abstracts the cloud platform-specific implementation for better usability and improved cloud portability. As a developer, this means you spend less time learning and dealing with platform-specific issues and instead learn cloud transferrable skills!

Serverless is designed to be used in place of AWS CloudFormation, Azure Resource Manager, Google Cloud Deployment Manager.

TypeScript

TypeScript is a programming language based on JavaScript and compiles code to JavaScript. Then why should you learn and use Typescript?

1) As the name implies, TypeScript allows data types to be assigned to variables, functions, and properties. This allows for better error checking and handling prior to run-time, and more importantly better tooling support to increase development speed.

2) It's possible to mix TypeScript with JavaScript, this means regardless if a type definitions file exists for a Javascript library it's possible to use any JavaScript library in TypeScript. Although the DefinitelyTyped community has created type definition files for nearly 90% of the top JavaScript libraries.

3) TypeScript compiles to JavaScript, anywhere you can use JavaScript you can use TypeScript. This is perfect as the primary serverless computing infrastructure (think AWS Lambda, Azure Functions, and Google Cloud Functions) supports JavaScript.
Amazon DynamoDB is Amazon's very own NoSQL solution. It is fully managed, meaning you simply specify the read/write capacity and Amazon handles the rest. Proper assessment of whether DynamoDB is a good solution for your specific use-case is beyond the scope of this article, but DynamoDB is a key-value and key-document store commonly used for content and information storage. Transactional data is usually better served using a SQL database.


Let's Get Building

Install NodeJS and NPM:
sudo apt install nodejs -y
sudo apt install npm -y
sudo npm install -g npm

Install Serverless:
sudo npm install -g serverless
Create a new Serverless project using the TypeScript Template:
serverless create --template aws-nodejs-typescript --path <directory of project>
Navigate into the directory and install the required npm packages:
cd <directory of project>
npm install

You now have a working Serverless deployment. Set up the appropriate credentials to access your AWS account by following this: AWS - Config Credentials.

Deploy to AWS

With the credentials setup deploying this example is as simple as:
serverless deploy
To see more deployments options:
serverless deploy --help
NOTE: sls can be used as shorthand for serverless

What makes this a Typescript project?

If we take a look at the package.json and the devDependencies:
"devDependencies": {
    "@types/aws-lambda": "8.10.1",
    "@types/node": "^8.0.57",
    "serverless-webpack": "^5.1.1",
    "ts-loader": "^4.2.0",
    "typescript": "^2.8.1",
    "webpack": "^4.5.0"
},

The @types/aws-lambda and @type/node packages are packages that provide the types definitions to use the respective JavaScript libraries natively in Typescript. The ts-loader package will take the Typescript files and compile them to JavaScript so that Serverless can package them and deploy them. The last requirement is the typescript package that contains the Typescript compiler.


Serverless Offline

Reduce downtime and run and test your Web Application without having to deploy to AWS first. 

The serverless-offline plugin emulates AWS Lambda and API Gateway functionality locally on your development computer. Allowing you to run and test your code without having to deploy to AWS. To set up your project to support this, first install it:
npm install --save-dev serverless-offline
This will install the package and add an entry to the package.json file, but you will need to modify the serverless.yml to make Serverless aware of this plugin. In the plugins section of the serverless.yml add - serverless-offline:
# Add the serverless-webpack plugin
plugins:
- serverless-webpack
- serverless-offline

Offline DynamoDB Support

To get DynamoDB emulation locally on your development computer use serverless-dynamodb-local plugin.
npm install --save-dev serverless-dynamodb-local
Modify your serverless.yml file:
# Add the serverless-webpack plugin
plugins:
- serverless-webpack
- serverless-offline
- serverless-dynamodb-local
You must install a local instance of a DynamoDB table, and you can do this by:
sls dynamodb install

To run your WebApp completely locally on your machine

sls offline start

There you have it, a complete starting point for your Web application or service that can be run locally using a single command and with another command be deployed to AWS!

Enjoy!

Comments

  1. Hi,
    Great article!

    I am trying to divide my stack in multiple services. Can you tell me how would you organise your tests for multiple services? I have a shared library folder as well.

    ReplyDelete

Post a Comment

Popular posts from this blog

How Salesforce uses AWS to Improve The Support Call Experience

Apple Pay, Android Pay, contactless credit cards, is it safe?

Failed CUDA Toolkit Install? Ubuntu 18.04 stuck on boot of Gnome Display Manager?