Three doors
Console, CLI and SDK are three doors to one API
Click, command or code: the same request reaches the same API.
There are three ways to ask a cloud platform for something. Take one request: create a storage bucket. In the console, you click a button on a web page. With the command line interface, you type one command. With a software development kit, you call a function from your own code. All three end as the same HTTPS request to the same API. Nothing can be done in the console that cannot also be scripted.
CLI grammar
Every CLI has one grammar: tool, group, action, options
Tool, group, action, options. Learn one, read them all.
Command line tools look cryptic until you see that they share one sentence structure. First the tool. Then the service or group it should work on. Then the action. Then options that adjust it, such as the region. The Azure, Google Cloud, GitHub and Docker tools are built the same way. When you meet a new one, look for those four parts.
CLI grammar · Going deeper · 1 of 2
Exit codes: how scripts know a command failed
Exit code 0 is success. set -e stops a script at the first failure.
Output for scripts · Going deeper · 2 of 2
Filter and format in the CLI, so the script gets only what it needs
Use --query and a script-friendly output format. Never parse the table.
Credentials
Credentials are found in a fixed order; best is a role
Option, environment, profile, then the machine's role.
A command needs to prove who is running it, and the tools look for credentials in a fixed order. First, an option on the command itself, such as a named profile. Then environment variables. Then profiles saved in a configuration file: one for development, one for production. And last, a role that the platform has given to the machine. That is the best kind. No key is stored anywhere, so no key can leak.
Three clouds · Going deeper
Each cloud has the same pieces under different names
IAM role, managed identity, service account: one idea, three names.
Idempotency
A script you can safely run twice is one you can trust
State a goal, not an order. Then a rerun changes nothing.
Every script gets run more than once, whether you plan it or not. A script that says "create a server" does exactly that, each time. Run it three times and you are paying for three servers. A script that says "make sure this server exists" checks first. The first run creates it. The second and third find it there and do nothing. That property is called idempotency.
Waiters · Going deeper
The API answers before the work is done: wait for the state you need
Use a waiter, not sleep: it polls until the resource is ready, with a limit.
A whole task
A good script: no secrets, every page, safe to rerun
A role for credentials, a paginator for all pages, a filter for the target.
Here is a complete script, in eleven lines, that stops every running development server. It contains no keys: the SDK finds credentials from the role of the machine it runs on. And it asks for a paginator, so that it sees every page of results. It lets the API do the filtering: only servers tagged as development, and only those that are running. Run it twice and the second run finds nothing to stop. And it prints one line, so the log shows what happened.
Lesson complete
You finished “CLIs and SDKs”
Nicely done.