Create and deploy a globally scalable serverless function in 5 minutes


16 August 2019 by Tim Giblin

In this post, we’ll walk through writing this as a serverless function and seeing it’s benefits of simplicity, maintainability, scalability and cost-effectiveness.

Imagine you have created a brand new landing page for your new product and want to start gathering email addresses for potential leads into a CRM like HubSpot. You want this to be able to scale to accept hundreds of requests per second during your big marketing pushes. From a code perspective, all this requires is a backend service that calls HubSpot with a secret API key, like so:

Prerequisites:

  • install serverless cli (npm i -g serverless)
  • get cloud provider keys (for AWS, see here)
  • get HubSpot api key (optional, you can skip the Hubspot part if you want)
  • a bit of node.js knowledge (although you can use other languages)

 

What are your options?

 

You could provision a cloud server like Amazon EC2, and configure it to autoscale and spend some more time figuring out how to securely connect, deploy and run your code, then open up an HTTP endpoint to connect from your website. After a few hours of reading EC2 blog posts and Googling Linux commands, you’re good to go!

 

Better yet, you could create a Docker container for your code and spend some time figuring out the exact sequence for installing dependencies, copying code to the correct directory, injecting environment variables, and running on and exposing the correct ports. Then, you could configure Kubernetes to manage scaling and 50 lines of configuration later, you’re all done!

 

While one of the above paths may sound easy to you - anyone unfamiliar with your system will need hefty onboarding to understand your ecosystem and become familiar with EC2, the Linux filesystem, AWS command line, Docker, Kubernetes, deployment pipeline, etc. Not only that, but any service you choose will be charging you by the minute since your server is always up and running.

 

With a serverless function, you can have your code deployed in minutes by writing just a few lines of straightforward configuration and without having to learn any infrastructure. Serverless functions allow for limitless scalability, zero server management, with simple and quick deployments. Let’s do it:

 

1. Create your serverless function from template


Let’s start out by using the serverless cli to create a local serverless project in a new-lead directory. This contains a boilerplate function (handler.js) and serverless configuration (serverless.yml). Feel free to choose another language as a template via serverless create --help.

 

2.  Initialize app, add HubSpot dependency and logic

Initialize your app and add the HubSpot dependency.

Open up handler.js, and input your logic for creating a new lead in HubSpot, assuming the lead’s email address is in the request body.

 

3. Register your function for HTTP Post and secret key

You need a secret environment variable to access the HubSpot API, so let’s add that to our serverless configuration. While we’re in here, we’ll register our new function and open it up to an HTTP Post request.

 

4. Deploy

Ensure you have the correct environment variables available, then deploy.

 

This will deploy your function to the AWS Cloud and return your public API endpoint which you can now invoke via an HTTP request.

 

You can then test by sending a post request, with {“email”: “jill@test.com” } in the body. If you use postman, you can download the collection here

 

You now have a globally scalable function running in the cloud in just a few minutes with just two files and a simple deployment command. Behind the scenes, the serverless framework packages up your application and runs cloud-specific deployment tasks. In this case, it creates or updates the CloudFormation stack, creates an API gateway endpoint the first time, etc. If you make any changes, you can use the same deploy command which will update your cloud function, keeping the same URL.

 

There are a few drawbacks I’ve seen. Mainly, cold starts can be unpredictable and vary in duration. Functions are great for writing fast microservices that don’t have a lot of dependencies - not for long running processes or memory intensive applications. Serverless functions can also be very expensive if not used in the right use cases. I would advise looking at cost calculators for whichever cloud service you choose to estimate monthly costs.

 

However, in many cases, the benefits can outweigh the costs. Serverless functions can truly simplify an organization not only from a code perspective, but allow for less time spent on devops and more time on your business differentiators. Most cloud providers, like AWS, will give you your first million calls per month for free. They can do this because they don’t have to constantly be running servers, so in the end, you are also using fewer resources and making the world a better place.

 

Code for this post can be found at https://github.com/timmyg/new-lead-serverless

 


Tim Giblin
Tim Giblin

Tim has 10 years of full stack development experience, from founding a startup to working for large enterprises. He is a big fan of npm scripts and for some reason likes CoffeeScript. Tim enjoys Xavier basketball as it is the only Cincinnati sports team to not let him down.