Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

Learning Serverless in GCP

Usu­al­ly, when I want to learn how to use a tool, the thing that works best for me is to try to build some­thing us­ing it. Watch­ing some­one build some­thing in­stead is the sec­ond best thing.

So, join me while I build a lit­tle thing us­ing "server­less" Google Cloud Plat­for­m, Python and some oth­er bits and pieces.


Caveat: this was orig­i­nal­ly a twit­ter thread, so there will be ty­pos and things. Sor­ry! Al­so it's pos­si­ble that it will look bet­ter here in thread­er­app


A thread by Roberto Alsina

Ready for an af­ter­noon learn­ing with un­cle Rober­to? Come along, to­day's top­ic is "server­less"! Start thread.

As usu­al, server­less seems a bit daunt­ing. So, our code will run in that vast, anony­mous, vague­ly love­craftian in­fras­truc­ture? Like AWS or GCP?

Well, yes. But you on­ly need be scared of Cthul­hu if you have not seen Cthul­hu as a pup­py.

So, let's start with a pup­py-­like thing. Yes­ter­day @quen­er­a­pu men­tioned http­s://­car­bon.now.sh which shows pret­ty code snip­pets but just as im­ages. Pret­ty? Good.

On­ly pret­ty and no copy­/­paste? Bad ba­by Cthul­hu

So, let's do some­thing about it. This is a script that will pro­duce ei­ther a col­orized im­age of code, or HTM­L. Yes, that's a screen­shot. Yes, I no­tice the irony.

It pro­duces, when ran as shown, this:

Is it pret­ty? No. But re­mem­ber, no suc­cess­ful com­pli­cat­ed thing grew out of a com­pli­cat­ed thing. As long as the in­puts and out­puts of our ini­tial code are with­in the same postal code as what we re­al­ly would like to have, it's good enough for now. So, what can I do with that code? I can do a flask ap­p.

And if I run it, it works

So, this is the seed of an app that could re­place http­s://­car­bon.now.sh ... it would need fron­tend, and (to make it bet­ter) some­thing a lot like a URL short­en­er, but that's be­sides the point.

And now I can choose dif­fer­ent paths to fol­low.

One way I could go for­ward is to de­ploy it to my serv­er. Be­cause I have a serv­er. It's a tiny server, and it's very cheap, but this? It can run it a mil­lion times a day and I won't no­tice. But I said this thread is about server­less, right? So you know I am not do­ing that.

There are 2 main server­less en­vi­ron­ments I could try (be­cause they are the 2 ones I know about)

* AWS Lamb­da
* Google cloud func­tions

Let's try google's. Lat­er maybe we'll try the oth­er one. But the im­por­tant bit here is: do­ing server­less and do­ing "server­ful­l" is not re­al­ly all that dif­fer­en­t. As long as:

1. Your end­points are state­less
2. You don't re­ly on the filesys­tem for state
3. All the state you wan­t, you put on the data­base

Now I have to go set­up my billing in­for­ma­tion in Google Cloud, BR­B. Done, there is a free tier and some free cred­it for start­ing up, and it's not go­ing to cost any re­al mon­ey any­way.

So

I changed like, two lines.

And added re­quire­ments

It took some 30 sec­onds to de­ploy.

And it works for HTML out­put (…er­ing-­force-268517.­cloud­func­tion­s.net/­col­or-­cod­ing?f…)

But not for PNG, be­cause while the code runs just fine the ac­tu­al en­vi­ron­ment it's run­ning on is ... lim­it­ed.

So it has no fonts, which it needs to do the im­age.

These are the pack­ages GCP pro­vides in the im­age where our func­tions run. So, it's not the same as my desk­top ma­chine, to say the least.

Looks like we have lib­er­a­tion fonts, so "Lib­er­a­tion Mono" should be avail­able? Yep. (…er­ing-­force-268517.­cloud­func­tion­s.net/­col­or-­cod­ing?l…) So, how do we make this use­ful?

Let's de­fine a us­er sto­ry. I, a de­vel­op­er, want to show my code in twit­ter, but pret­ty and longer than 240 char­ac­ter­s.

Al­so, I want peo­ple to be able to copy all or part of that text with­out typ­ing it man­u­al­ly as if it were 1989. Now I will take a short break. Go have some cof­fee.

This us­es this twit­ter fea­ture: (de­vel­op­er.twit­ter.­com/en/­doc­s/tweet­s…)

So, if I make a sec­ond server­less func­tion that gen­er­ates some­thing like that, I am al­most there. Then it's a mat­ter of UI. Mind you, it's per­fect­ly ok to im­ple­ment this as a flask app and then just once it does what I want it to do re­de­ploy to google cloud.

The code looks ex­act­ly the same any­way.

That is just a mat­ter of some ba­sic string tem­plat­ing.

So, there you go, that Google cloud func­tion is all the back­end you need to im­ple­ment some­thing like http­s://­car­bon.now.sh

But I don't re­al­ly want to, so you go ahead.

EOT

(To­mor­row: run­ning this on AWS us­ing Zap­pa and on GCP us­ing gcloud)


Contents © 2000-2024 Roberto Alsina