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.
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….
so the runtime support for the language is for Go, NodeJS18,14, Php, Python…
I went ahead and selected "node18"
and Boom! it created a function…
I added my name under parameters like :
and then hit run…it showed the following :
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…
and cleared the params….to make it look like :
Hit save, and hit on "run"
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:
here's you can copy and use yours.
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? :
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…
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}
}
*mic drops*That's all folks.
Follow me on Twitter maybe?
Link : https://twitter.com/saarabpreet/











Top comments (0)