DEV Community

Sarabpreet S.
Sarabpreet S.

Posted on

Deploying a Serverless Function on DigitalOcean

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

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

so the runtime support for the language is for Go, NodeJS18,14, Php, Python…

Image description

I went ahead and selected "node18"

and Boom! it created a function…

Image description

I added my name under parameters like : 

Image description

and then hit run…it showed the following :

Image description

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

and cleared the params….to make it look like : 

Image description

Hit save, and hit on "run"

Image description

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

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

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/

Top comments (0)