My Authors
Read all threads
Ready for an afternoon learning with uncle Roberto? Come along, today's topic is "serverless"! Start thread.
As usual, serverless seems a bit daunting. So, our code will run in that vast, anonymous, vaguely lovecraftian infrastructure? Like AWS or GCP?

Well, yes. But you only need be scared of Cthulhu if you have not seen Cthulhu as a puppy.

So, let's start with a puppy-like thing.
Yesterday @quenerapu mentioned carbon.now.sh which shows pretty code snippets but just as images. Pretty? Good.

Only pretty and no copy/paste? Bad baby Cthulhu
@quenerapu So, let's do something about it.
@quenerapu This is a script that will produce either a colorized image of code, or HTML. Yes, that's a screenshot. Yes, I notice the irony.
@quenerapu It produces, when ran as shown, this:
@quenerapu Is it pretty? No. But remember, no successful complicated thing grew out of a complicated thing. As long as the inputs and outputs of our initial code are within the same postal code as what we really would like to have, it's good enough for now.
@quenerapu So, what can I do with that code? I can do a flask app.
@quenerapu And if I run it, it works
@quenerapu So, this is the seed of an app that could replace carbon.now.sh ... it would need frontend, and (to make it better) something a lot like a URL shortener, but that's besides the point.

And now I can choose different paths to follow
@quenerapu One way I could go forward is to deploy it to my server. Because I have a server. It's a tiny server, and it's very cheap, but this? It can run it a million times a day and I won't notice.
@quenerapu But I said this thread is about serverless, right? So you know I am not doing that.

There are 2 main serverless environments I could try (because they are the 2 ones I know about)
@quenerapu * AWS Lambda
* Google cloud functions
@quenerapu Let's try google's. Later maybe we'll try the other one.
@quenerapu But the important bit here is: doing serverless and doing "serverfull" is not really all that different. As long as:

1. Your endpoints are stateless
2. You don't rely on the filesystem for state
3. All the state you want, you put on the database
@quenerapu Now I have to go setup my billing information in Google Cloud, BRB.
@quenerapu Done, there is a free tier and some free credit for starting up, and it's not going to cost any real money anyway.

So ...
@quenerapu I changed like, two lines.
@quenerapu And added requirements
@quenerapu It took some 30 seconds to deploy.
@quenerapu But not for PNG, because while the code runs just fine the actual environment it's running on is ... limited.

So it has no fonts, which it needs to do the image.
@quenerapu These are the packages GCP provides in the image where our functions run. So, it's not the same as my desktop machine, to say the least.

Looks like we have liberation fonts, so "Liberation Mono" should be available?
@quenerapu So, how do we make this useful?

Let's define a user story.
@quenerapu I, a developer, want to show my code in twitter, but pretty and longer than 240 characters.

Also, I want people to be able to copy all or part of that text without typing it manually as if it were 1989.
@quenerapu Now I will take a short break. Go have some coffee.
@quenerapu So, how would I make something that at the same time shows the code as an image and lets you copy/paste it?

Well, like this (and yes, I know it's not ideal, but it's *something*)

ralsina.me/large_card.html
@quenerapu This uses this twitter feature:

developer.twitter.com/en/docs/tweets…
@quenerapu So, if I make a second serverless function that generates something like that, I am almost there. Then it's a matter of UI.
@quenerapu Mind you, it's perfectly ok to implement this as a flask app and then just once it does what I want it to do redeploy to google cloud.

The code looks exactly the same anyway.
@quenerapu In any case, all that's needed is to return a properly formatted HTML page with the correct metadata.

Like this one.

…ering-force-268517.cloudfunctions.net/color-coding?l…
@quenerapu That is just a matter of some basic string templating.
@quenerapu So, there you go, that Google cloud function is all the backend you need to implement something like carbon.now.sh

But I don't really want to, so you go ahead.

EOT

(Tomorrow: running this on AWS using Zappa and on GCP using gcloud)
Missing some Tweet in this thread? You can try to force a refresh.

Enjoying this thread?

Keep Current with Roberto Alsina

Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

Twitter may remove this content at anytime, convert it as a PDF, save and print for later use!

Try unrolling a thread yourself!

how to unroll video

1) Follow Thread Reader App on Twitter so you can easily mention us!

2) Go to a Twitter thread (series of Tweets by the same owner) and mention us with a keyword "unroll" @threadreaderapp unroll

You can practice here first or read more on our help page!

Follow Us on Twitter!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just three indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3.00/month or $30.00/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!