KATIE MCLAUGHLIN: Hey, Martin.
Have you lost something?
MARTIN OMANDER: Hey, Katie.
I'm trying to find my wallet.
I forgot to pay my internet bill,
and I need to find my credit card.
KATIE MCLAUGHLIN: Well, why not set up automatic payments?
I was just in a meeting with the team from WebNet,
and we helped them set up their nightly billing job.
Stay tuned if you want to find out more.
[MUSIC PLAYING]
WebNet is a pay-as-you-go internet provider where you can
set up automatic payments through their app.
They built a nightly billing job which
charges the credit card of any user who
has built up a significant balance during the day.
MARTIN OMANDER: Their billing job
runs in Cloud Run, which is serverless.
This means they don't have to manage servers or pay
for idle capacity.
Also, it scales up automatically as load increases.
KATIE MCLAUGHLIN: They use Cloud "Shed-juler"
to kick off the nightly billing job.
MARTIN OMANDER: Don't you mean Cloud Scheduler?
KATIE MCLAUGHLIN: Nah, I meant Cloud "Shed-juler."
Cloud Scheduler is fully managed by Google,
which means that the team doesn't have
to worry about maintenance.
It just works.
MARTIN OMANDER: Cloud Scheduler triggers code
by either making an HTTPS call, or by publishing a Pub/Sub
message.
The team chooses HTTPS.
KATIE MCLAUGHLIN: They configure Cloud Scheduler
to make a POST request to their billing service
nightly at 1:00 AM GMT.
They also set a JSON payload that specifies
the minimum balance.
Only customers with a balance of $5 or more will be billed.
MARTIN OMANDER: Cloud Run lets you run code in any programming
language you want, as long as it's a web service that
can be containerized.
The team has experience with JavaScript,
so they pick Node.js.
At the top of their code, they create and initialize
an express app that listens for incoming HTTP calls.
Note that Cloud Scheduler uses text/plain content-type,
so they have to tell bodyParser to parse
more than just the default application JSON type.
KATIE MCLAUGHLIN: The post handler
parses the minimum balance that was sent to it from Cloud
Scheduler, and then sends that off
to the billing method later in their code.
MARTIN OMANDER: The billing job will now be triggered nightly.
But this billing service is publicly exposed
to the internet, meaning anyone can trigger it
by posting to the URL.
We need to make it private so only
Cloud Scheduler can trigger it.
KATIE MCLAUGHLIN: To do that, the team edits the Cloud Run
service, removing the allUsers member from the service, which
makes it private.
MARTIN OMANDER: Next, they create a new service account
they call Billing Initiator.
KATIE MCLAUGHLIN: They then give the new service account
the "Cloud Run Invoker" role.
This means that the Billing Initiator service
account can trigger Cloud Run services, but do nothing else.
MARTIN OMANDER: Next, they copy the email
of the Billing Initiator service account--
KATIE MCLAUGHLIN: And then they open up
the job in Cloud Scheduler and under the Auth header setting,
select "Add OIDC token."
OIDC, or Open ID Connect, is a small layer on top of OAuth
that handles identity.
Using OIDC, they can tell Cloud Scheduler
to use the Billing Initiator service account
as the identity for the job.
MARTIN OMANDER: Now the billing service
is closed to the public, but it can still
be triggered by Cloud Scheduler.
KATIE MCLAUGHLIN: So the team at WebNet gave me this US $1 bill.
How much Cloud Scheduler could they get for that $1?
MARTIN OMANDER: Well, you get the three Cloud Scheduler jobs
for free.
If they've used those three jobs already,
they would have to pay $0.10 per month for this job,
regardless of how many times the job executes in a month.
So that $1 bill would pay for 10 months of Cloud Scheduler.
They would, of course, also have to pay for Cloud Run, execution
time, and database storage.
KATIE MCLAUGHLIN: That makes sense.
So what about the--
Martin?
Oh, I guess he didn't pay his bill in time.
Oops.
[MUSIC PLAYING]