Amazon has lost more than $10 billion from its investment in electric vehicle startup Rivian so far this year.

After Amazon reported better-than-expected sales for the second quarter and an upbeat revenue forecast for the second half of 2022, the stock gained more than 12 percent in extended trading hours.

Amazon generated revenue of $121.23 billion in the second-quarter period, representing analysts' average forecast of $119 billion and a 7 percent increase from the prior year.

After expenses, Amazon posted a net loss of $2 billion, or $0.20 per share, for the quarter. It was expected to post a net profit of $0.12 per share.

Further Amazon is forecasting sales in the third quarter to be between $125 billion and $130 billion, representing a growth of 13 percent to 17 percent.

Amazon CEO Andy Jesse said, "Despite inflationary pressures in fuel, energy and transportation costs, we are making progress on the more controllable costs we referred to last quarter.

According to experts, excluding the Rivian effect, Amazon would have made a net profit from its online retail and cloud services businesses.

While Amazon owns about 18 percent of Rivian, Rivian stock has declined sharply so far this year amid a market sell-off.

Rivian has lost two-thirds of its market value since the beginning of this year. In the first quarter, Amazon reported a loss of $7.6 billion from its Rivian stake.