Deploying a Serverless Function on DigitalOcean
Published: 2022-07-22 08:29:58

---
title: Deploying a Serverless Function on DigitalOcean
published: true
description: 
tags: 
# cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xt026mf0nw00vv54kplo.png
# Use a ratio of 100:42 for best results.
---
 
I'll start this with a confession. 

I have read about AWS's lambda, serverless computing feature. However, I found it pretty tough to get started and try out something of my own.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lm1q5jtl4ihusvrvfi9w.png)
 
Saw the above on my Digitalocean dashboard this morning….
Functions (new) and decided to get on with the flow…and within a few minutes, I was able to pull off a basic function. here's a quick recap of how I managed to create a function within a few minutes…

let's begin…

*drumrolls*

So what exactly is "functions" by digitalocean? 
DigitalOcean Functions is a serverless computing solution that runs on-demand, enabling you to focus on your code, scale instantly with confidence, and save costs by eliminating the need to maintain servers

yes, you are right…Digitalocean Functions is literally a function that you can deploy and it will scale up/down accordingly. 
so I started a thing and took screenshots along. (something that helps power this article as well), so here it goes…

I obviously took the easy way….

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0rjq80iy4e6ifl0y7grd.png)
 
so the runtime support for the language is for Go, NodeJS18,14, Php, Python…

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/laplmnq6oace861scazr.png)
 
I went ahead and selected "node18"


and Boom! it created a function…

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z6r29z5eh654710k8s1r.png)
 
I added my name under parameters like : 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jowz9ar7yphy540682j9.png)

 
and then hit run…it showed the following :

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6nhdjq93a11djqis93q4.png)

 
So, let's come back to the code part now…

function main(args) {
 let name = args.name || 'stranger'
 let greeting = 'Hello ' + name + '!'
 console.log(greeting)
return {"body": greeting}
}

so the function is taking an argument (ie args variable), if it is present it will show the following. else it will show "hello stranger" 

let's try that out now. 

I went back to the params dialog…


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3et321fdgw6wt2og3vav.png)
 
and cleared the params….to make it look like : 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kcn0lohd017raxnqk2t3.png)
 
Hit save, and hit on "run"

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bwjd0vk26d0jib5qo0dn.png)

 
and it ran….:) 

see, I told ya. it would say "hello stranger" 

okay so the big question now, is how do I make it work for the web? how do I send an argument via a query? or param? 

turns out it's as simple as it was with arguments….every digital ocean function comes with a public link that you can access. 
here's mine: 

https://faas-blr1-8177d592.doserverless.co/api/v1/web/fn-0c1fb899-43af-41ca-ad15-02e0cb85346a/default/firstfunction 

here's you can copy and use yours. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fq22nscqfkzd7bc9rus7.png)
 

I visited the link and it said : 

"Hello stranger"
so how do I now pass an argument here to say "Hello There"? 

well, it's simple to send it as a URL query. 
append '?name=There' on to the url 
so my link became? : 

https://faas-blr1-8177d592.doserverless.co/api/v1/web/fn-0c1fb899-43af-41ca-ad15-02e0cb85346a/default/firstfunction?name=There

and it said : 

"Hello There" 

yeah so, now we know how to add an argument we know how to print it on the browser too….let's bake some logic now :) 

how about?…adding 2 numbers? 

something like "link?numberOne=23&numberTwo=37? and the result would be 50? 

let's modify our 'function' then…

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6hfapt2g4evd7ts2hlzv.png)
 

final code: 

function main(args) {
let firstNumber = args.firstNumber || 0;
let secondNumber=args.secondNumber || 0;
let logic = parseInt(firstNumber)+parseInt(secondNumber);
console.log(logic)
return {"body": logic}
}

final URL: 
https://faas-blr1-8177d592.doserverless.co/api/v1/web/fn-0c1fb899-43af-41ca-ad15-02e0cb85346a/default/firstfunction?firstNumber=30&secondNumber=40 

*mic drops*That's all folks.
Follow me on Twitter maybe? 
Link : https://twitter.com/saarabpreet/