The argument nobody could remember

My infrastructure tool is a Makefile with around eight hundred targets in front of a few hundred Python scripts and Ansible playbooks. Each target does one thing to one AWS resource in one account. The trouble was the second argument. Describing an instance wanted instance_id=. A zone wanted zone=. A bucket wanted bucket=. A listener wanted listener_arn=, a target group wanted target_group=, an AMI wanted ami_id=. Each was the natural name when that target was written. Together they meant that nobody, me included, could run a target without reading its help line first.

The help listing is the learning surface for a tool like this. It cannot teach anything if every line is a special case.

The convention

name= is the resource. Every target that acts on a resource takes it as name=, whatever the resource type. An instance by its Name tag. A zone by its domain. A bucket by its bucket name. A key by its alias.

Five verbs per family. list, describe, update, create, delete. A new family gets all five with the same spelling, so once you know one family you know the next.

BeforeAfter
tool ec2-describe account=dev instance_id=i-0a1b2c3d4e5ftool ec2-describe account=dev name=web-01
tool r53-records account=dev zone=example.comtool r53-records account=dev name=example.com
tool s3-describe account=dev bucket=dev-logstool s3-describe account=dev name=dev-logs
tool ami-update account=dev ami_id=ami-0abc tags='{"env":"dev"}'tool ami-update account=dev name=ami-0abc tags='{"env":"dev"}'
tool elb-listener-describe account=dev listener_arn=arn:aws:...tool elb-listener-describe account=dev name=web-alb port=443

A legacy key survives only as an alias, and the help line says so.

## r53-record-describe: Show a DNS record | Required: name (legacy: zone), record
r53-record-describe: _require_account
	$(if $(or $(name),$(zone)),,$(error name= is required))
	... -e zone=$(or $(name),$(zone)) -e record=$(record) ...

The $(or) means an old runbook that still passes zone= keeps working. The help line means the next person learns name= instead.

When the name is not the resource

Some resources have no name. A listener has no Name tag; it belongs to a load balancer and sits on a port. So the listener targets take name= for the load balancer and port= to select the listener, and a rule adds priority=. A backup job has only an AWS job id. A GuardDuty finding has only a finding id. The caller identity has nothing to name. Those targets are exempt, each with a one-line reason, and the exemption is permanent.

The lint

The convention shipped for about 45 targets and then stopped. Targets added afterwards were written the old way, because nothing stopped them. So the lint is a ratchet with a baseline file.

  • A new target that takes an identifier argument and does not support name= fails the build.
  • A PENDING entry in the baseline whose target now complies also fails. You delete the line in the same change that converges the target, so the baseline cannot rot.
  • An entry for a target that no longer exists fails.
  • EXEMPT entries are permanent and carry their reason.

Detection is recipe-based: a target supports name= when its recipe references $(name). Help text lags; the recipe is the truth. The lint is pure standard library, no AWS, no network, so it runs in CI on every change and costs nothing to wait for.

Reads come from a cache

The describe and list targets read a cache first: an inventory snapshot per account, kept hourly fresh, with live=1 to go to the API instead. The name resolver reads the same cache, which is why resolving name= to an id needs no credentials and no API call, and is fast enough to sit inline in a recipe. The cost is staleness. A resource created a minute ago is not in the cache until the next refresh, and live=1 is the answer.

Names are not unique, so the resolver refuses to guess

This is the trade-off. An id is unique by construction. A name is a tag, and two instances can carry the same one. The resolver's rules, as the code has them:

  • An argument that already looks like an id for the type (i-, sg-, vpc-, subnet-) passes through untouched. Old runbooks keep working.
  • Otherwise it looks for an exact name match. If several resources share the exact name, it exits non-zero, lists the ids, and asks for the id directly.
  • With no exact match, a unique substring match resolves. More than one substring match exits non-zero with the name and id of each.
  • No match exits non-zero with how many of that type are cached, how many carry a name, and which list target to check.

The live EC2 resolver, the one that asks the API, follows the same rule with two details that came from mistakes. It excludes instances in the terminated and terminating states, because they linger in the API for about an hour, and a rebuild that reused a Name tag was reported as ambiguous. And it paginates, because without pagination the instances past the first page resolved as not found, and the start, stop and delete targets aborted on hosts that were plainly there. When a substring matches several and exactly one is an exact match, the exact one wins, so web still resolves once web-backup exists. Genuine ambiguity prints every match and stops.

Not picking the first match is the whole design. A tool that guesses right most of the time trains people to stop reading its output, and the one time it guesses wrong it stops the wrong instance. A name is allowed to be ambiguous. The tool is not allowed to resolve the ambiguity for you.