Deploy it
The shortener is tested and runs locally: three files, two contexts, two Workers. In this last chapter it goes live on Cloudflare. You’ll read what a deploy will do before it does anything, deploy, watch the live service’s logs, and see how to undo a release.
Work in the project from Test it, with its three
files in src/.
Before you start
Section titled “Before you start”Deploying needs two things that the earlier chapters didn’t:
- A Cloudflare account. The shortener uses Workers and, for its
Linkagent, Durable Objects. Check Cloudflare’s own pages on Workers pricing for what your plan includes; they change, so this chapter doesn’t repeat them. - Wrangler, signed in.
bynk deploydrives Cloudflare’s ownwranglertool. Sign in once withwrangler login, or setCLOUDFLARE_API_TOKEN(andCLOUDFLARE_ACCOUNT_ID) in an automated setting.
You can do the next step without either.
Read the plan first
Section titled “Read the plan first”A deploy creates real things on your account, so look before you leap:
bynk deploy --dry-run--dry-run compiles the project and prints what a deploy would do, and changes
nothing, on Cloudflare or in the project. Without credentials it works offline.
For the shortener it prints:
durable object Link (sqlite; advisory — Cloudflare reconciles it)secret set BYNK_CALL_FROM_SHORTENER (managed)deploy registrysecret set BYNK_CALL_TO_REGISTRY (managed)deploy shortenerorder registry → shortenerEach line is something the code you wrote asked for:
durable object Link. On Cloudflare, an agent is a Durable Object: one object per key, with its own storage.Linkfrom Add a stateful agent becomes one, and each short code gets its own. The line is advisory: Cloudflare creates the namespace if your account hasn’t got it, so Bynk doesn’t claim to know.- The two
secret setlines.shortenercallsregistryacross a service binding, because ofconsumes registry. Bynk secures that call with a pair of secrets it creates and manages, so that onlyshortenercan callregistry’s services. You never handle them. deploy registry, thendeploy shortener. Cloudflare checks a service binding when a Worker is uploaded: a Worker that binds to one that doesn’t exist yet is rejected. Soregistrygoes first, and the last line says why. Bynk works the order out from yourconsumesdeclarations; you never write it down.
Deploy
Section titled “Deploy”bynk deployIt checks that Node and Wrangler are installed and that you’re signed in,
shows the same plan, and asks before it changes anything. Then it provisions
what the plan lists, deploys both Workers in order, and prints the address of
each. The shortener Worker is the one that answers HTTP.
Try it, with the address bynk deploy printed in place of <shortener-url>:
curl -X POST <shortener-url>/links \ -H 'content-type: application/json' \ -d '{"target":"https://example.com"}'The answer is 201 Created, with a fresh random code. Follow it with
curl <shortener-url>/links/<code>, and the hit count goes up each time. A
target that starts with the shortener’s own address gets the 400 from
Handle failure with Result.
Watch it
Section titled “Watch it”bynk tail --context shortenerbynk tail streams a deployed Worker’s logs. With two contexts, --context
says which one. Each POST you send shows the
Logger.info("created …") line from
Reach out with capabilities. If anything
throws, the stack frames point at lines of your .bynk files, not at the
generated TypeScript.
Change it, and undo a change
Section titled “Change it, and undo a change”Change something small, such as the Logger message, and run bynk deploy
again. The plan reads the same, and nothing is lost: each Worker gets a new
version, and the Link agents keep their stored links and hit counts.
If a release goes wrong, bynk revert makes the previous deployment live again,
without a rebuild:
bynk revert --dry-run--dry-run prints every check and changes nothing. A rollback is refused
whenever it would be unsafe, for example if agent state was already written by
the newer release in a form the older one can’t read. Unlike
bynk deploy --dry-run, it reads your account to make those checks, so it needs
CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID. Drop --dry-run to roll
back for real. Roll back a deployment
has the details.
What you have done
Section titled “What you have done”You took the shortener live. bynk deploy --dry-run showed what your code asks
of Cloudflare: a Durable Object for the agent, a secured binding between the two
contexts, and the order they must go in. bynk deploy made it so,
bynk tail showed the live logs, and bynk revert is there if a release goes
wrong.
That’s the end of Learn Bynk. In ten chapters the shortener went from one function to a tested, deployed service of two contexts, with typed values, errors as values, declared effects, state in an agent, and an architecture the compiler checks.
Where next
Section titled “Where next”- Concepts, for why Bynk works the way it does.
- Guides, for the next thing you’re building: storing data, calling out, background work, reliability and more.
- Reference, for exact answers.
- By Example, for short programs to run and complete projects to read.
- The blog, for the thinking behind the language, one idea at a time.
Everything bynk deploy can provision, and how it handles secrets,
environments and a deploy that fails part-way, is in
Deploy to Cloudflare.
How a context maps to a Worker is in
Cloudflare Workers,
and every flag is in the CLI reference.