Compute Services

Building IBM Cloud Functions with TypeScript

Share this post:

Building IBM Cloud Functions with Typescript starter kit

When learning new technology, it’s often helpful to begin by exploring a starter kit. Beyond the proverbial “Hello World,” starter kits are pre-built to focus on a core technology and support libraries and packaging. This helps bring everything together into an opinionated and functioning example.

There are, however, so many development options that no single starter kit will satisfy everyone. And after not finding one for Cloud Functions and Typescript, I’ve created my own. Consider using the starter kit for your next project for the following reasons:

  1. Many serverless examples use native Javascript, but as projects grow in size, better type checking and code modularity are needed. This is why Typescript was chosen for the starter. Consider the Cloud Functions entry point main(params). Is the params.id property a string or a number? With Typescript, you can make this explicit and leverage ES6 syntactic sugar and range of additional programming constructs.
  2. Unit testing might be new to you. Typescript might also be new to you. And if both are new to you, that’s OK! Provided in the starter are examples of testing Cloud Functions using Mocha. Tests are executed using npm run test, which uses the ts-node utility to quickly test without having to build.
  3. The starter kit uses webpack to organize code. By using webpack, you can write multiple actions that are contained in a single project. And any external dependencies used by your actions will be packaged for you automatically. A working webpack.config.js is provided and sample actions declare (global).main = main; to make them compatible. Additionally, the starter will exclude any natively supported NPM modules from the final bundle.
  4. Finally, being declarative about deployment doesn’t mean writing complicated shell scripts—simply use wskdeploy. A sample manifest.yaml has been structured to work with webpack, and it deploys two APIs. See the wskdeploy documentation for more deployment options.

Ready to use the starter? Head over to GitHub and download the code.

IBM Offering Manager

More Compute Services stories
November 12, 2018

Configure Calicoctl for IBM Cloud Kubernetes Service

In the IBM Cloud Kubernetes Service, the Calico configuration file can now be generated automatically with just a single command. For clusters 1.10 and above, you can use the new flag to download your cluster config.

Continue reading

November 12, 2018

Application Modernization is Inevitable

Business pressures demand faster rollout and quality applications, but your existing estate will determine your modernization strategy. Watch the webinar: "Accelerate digital transformation by modernizing apps for the cloud."

Continue reading

November 8, 2018

On-Demand ALB Update Feature on IBM Cloud Kubernetes Service

Effective immediately, IBM Cloud Kubernetes Service customers can gain control over when the IBM Cloud Kubernetes Service ALB pods are updated in their clusters. The default setting for each newly created cluster remains—the Ingress controller will get updated automatically by IBM Cloud Kubernetes Service whenever a new version is available.

Continue reading