Amazon agreed to sign a membership deal for all desks in the WeWork space at 2 Herald Square, a Midtown office building that has been on the brink of foreclosure, sources told The Real Deal.

The e-commerce giant is taking over the full 122,000-square-foot space and some employees have already begun working there, sources said. WeWork, which has the address of 950 Sixth Avenue on the eastern side of the 11-story, 354,000-square-foot building, signed a lease there late last year.

Multiple sources also said Amazon is negotiating to lease part of the building’s retail component. Avison Young has been marketing 59,000 square feet of vacant retail space there. Victoria’s Secret is the building’s only retail tenant.

Sitt Asset Management and more than 80 investors own the property, which has been facing a flurry of troubles – a family ownership dispute, a defaulted senior mortgage and the long-delayed sale of the leasehold. Paramount Group, which has a preferred-equity position in the building, has held off on its plans to take control of the leasehold, even as foreclosure looms.

SL Green Realty, which owns the $250 million leasehold mortgage, was expected to begin foreclosure proceedings in October. Sources said the real estate investment trust is still planning that move.

WeWork declined to comment, and Amazon and Avison Young could not be reached.