Shoppers at a Whole Foods. AP Amazon is buying Whole Foods.

The online giant said Friday it was buying the high-end grocer for $42 a share in an all-cash deal, valuing the company at $13.7 billion.

Shares of the grocer were trading at $33.06 before the deal was announced, so the deal represents a 27% premium on its Thursday closing price.

"Millions of people love Whole Foods Market because they offer the best natural and organic foods, and they make it fun to eat healthy," Amazon CEO Jeff Bezos said in a press release. "Whole Foods Market has been satisfying, delighting, and nourishing customers for nearly four decades — they're doing an amazing job, and we want that to continue."

The activist investor Jana Partners took a 9% stake in Whole Foods in April and pushed the company to look into strategic options including a sale.

In October, reports surfaced that Amazon was planning to build grocery stores. The company has built physical retail locations for books.

This is also the largest deal ever for Amazon, outpacing its acquisitions of Twitch for $970 million in 2014 and Zappos for $850 million in 2009.

"This partnership presents an opportunity to maximize value for Whole Foods Market's shareholders, while at the same time extending our mission and bringing the highest quality, experience, convenience, and innovation to our customers," Whole Foods CEO John Mackey said in the release.

According to the release, Whole Foods will continue to operate under its name, and Mackey will stay on as CEO of the brand.

Mackey had called activist Jana Partners "greedy bastards" in an interview released on Wednesday.

"These guys just want to sell us, because they think they can make forty or fifty percent in a short period of time," Mackey told Texas Monthly's Tom Foster. "They're greedy bastards, and they're putting a bunch of propaganda out there, trying to destroy my reputation and the reputation of Whole Foods, because it's in their self-interest to do so."

The deal will be funded partly through debt financing from Goldman Sachs and Bank of America Merrill Lynch, Amazon said. It's pending approval by the Securities and Exchange Commission.

Whole Foods would also pay a $400 million termination fee to Amazon if the grocer received a better offer or the board of directors pulled the company out of the deal.

Following the news of the acquisition, Amazon shares were up over 3.5%, at $997.67, as of 10 a.m. ET. Whole Foods was halted.