For those interested in a lua example, from this article [1], I found & cherry-picked bits from here [2]. The key ingredients (to aid searching) are init_worker_by_lua_block & balancer_by_lua_block.
[1] https://medium.com/@sigil66/dynamic-nginx-upstreams-from-con...