The question nobody could answer
A contact-form handler for a client's website runs as a Lambda
function. The handler itself is described in
its own
post. When every path in it fails, the visitor sees an error
and the office phone number, and that number is baked into the
code. In late September the number in my notes and the number in
the repo's source did not agree with the number in the repo's
function.zip, which was a day older than the source.
So which one was live?
My infrastructure tool had two Lambda commands at the time. One listed functions. The other read invocation, error and throttle metrics. Neither touched code. The packages had been zipped by hand and pushed by hand, which is exactly how a repo ends up holding an artifact that does not match either the source or the deployment. The usual answer to "which one is live" is "redeploy to be safe", and that answer destroys the evidence while it reassures you.
Get the deployed package
GetFunction returns the function's configuration and,
under Code.Location, a short-lived URL to the exact
package the service is running. Download it, unzip it into a
directory, and print the three facts worth knowing: the code hash,
the last-modified time, and the size.
fn = lam.get_function(FunctionName=name)
url = fn["Code"]["Location"]
with urllib.request.urlopen(url, timeout=60) as resp:
data = resp.read()
out.mkdir(parents=True, exist_ok=True)
with zipfile.ZipFile(io.BytesIO(data)) as zf:
zf.extractall(out)
names = zf.namelist()
cfg = fn["Configuration"]
logger.info("%s sha256=%s modified=%s size=%d",
name, cfg["CodeSha256"], cfg["LastModified"], cfg["CodeSize"])
Wrapped as a command, the whole check is two lines:
lambda-code-get account=prod name=form-handler out=/tmp/form-deployed
diff /tmp/form-deployed/handler.py src/handler.py
I diff the extracted files rather than compare hashes. The hash the service reports is the hash of the uploaded zip, and two zips of identical files can differ in their metadata, so matching hashes prove identity but differing hashes prove nothing.
What the diff said
Nothing. The deployed handler was byte-identical to the current source, correct phone number included. The stale thing was the committed zip, built from the source as it stood a day earlier and never rebuilt after the edit. Production had been right the whole time. I rebuilt the artifact from source and did not redeploy, because there was nothing to deploy.
That is the finding worth writing down. The assumption behind "redeploy to be safe" is that the repo is the truth and production drifts from it. Here the repo's artifact was the thing that drifted, because a built file checked into a repo is a snapshot of one moment and nothing forces it to follow the source. Without the download, I would have redeployed, seen the correct number, and credited the redeploy for a fix that never happened.
The mistake was mine. I committed a built artifact, then edited the source and did not rebuild it. The fix for that habit is in the deploy command, not in a reminder.
Deploy with a waiter
The second command packages a file or a directory and pushes it. The packaging rules are the ones that bite when a zip is built by hand on a laptop:
- Paths are relative to the archive root. A single file lands at the root, not under the directory it came from.
- Every entry gets
0644permissions, so the runtime can read what you uploaded regardless of the mode bits on the machine that built it. __pycache__directories are skipped.- Files are added in sorted order, so the same source produces the same archive.
Then the push, and the part that hand-built deploys usually skip: waiting for the update to finish before reporting success.
resp = lam.update_function_code(FunctionName=name, ZipFile=data,
Publish=False)
lam.get_waiter("function_updated_v2").wait(FunctionName=name)
logger.info("deployed %s sha256=%s modified=%s", name,
resp["CodeSha256"], resp["LastModified"])
The function is not ready the instant the call returns. The waiter blocks until the service reports the update complete, so the hash the command prints is the hash that is live, and a deploy followed by a test does not test the previous version.
Two options close the loop with the repo. --zip-out
writes the exact bytes that were uploaded to a path of your
choosing, which is how the repo's artifact stays current from now
on: the deploy produces it, so it cannot lag the source.
--dry-run builds the package and reports its size
without calling the service.
Six unit tests cover it, with the service client replaced by a
mock: a single file sits at the archive root, a cache directory is
skipped, permissions are 0644, a dry run makes no
update call, a missing source fails without calling anything, and a
real run calls the update and then the waiter.
If you run functions and build the zips yourself
- Before any "fix", download the deployed package and diff it against source. It takes seconds and it settles the question.
- Diff files, not hashes.
- Never hand-edit or hand-build a committed artifact. Let the deploy write it, or do not commit it at all.
- Zip with root-relative paths and readable permissions, and skip caches.
- Wait for the update to finish before you print success or run a test.
- Record the code hash and modified time the deploy reports. The next time someone asks what is live, the answer is in the log.