I think it depends on what you mean by "cheap foreign engineers." I am born and raised Canadian. I make a decent chunk less than my American counterparts, even when working for the same company. Not because they are necessarily better suited for the job, but literally because of the country I live in. The same is true for engineers all over the world.
So yes, companies absolutely can get cheap(er) labour and have the same, or at least similar, quality.
The foreigner from your point of you is a local to the market that companies like Amazon or Meta are serving. It's easier to understand what appeals to that market from the ground rather than the US west coast.
Believe it or not there is talent even in countries where salaries are lower. For 3000 - 4000 USD month you can get good english speaking senior engineers in Vietnam used to work in international environments. Not FAANG tier but strong fit for most $CRUD companies. This is cheaper than US by far, although the gap with Europe is unironically closing.
As someone that routinely works in offshoring projects, most business don't care, even if there are escalations as long as total cost remains under doing everything in the country, upper management is happy.
An example is the amount of crap software that exists out there, that people would rather use with a race to bottom prices, instead of paying for quality.