How do I change the legacy CDN URLs of images in posts?

It should (at very least) begin with changing the Discourse CDN URL in your yml file and then rebuilding discourse.

If You only have to replace links in database, there is a rake find & replace task there just for this purpose.
Documentation here: