The new year is right around the corner, with now an excellent time to consider investing in stocks likely to flourish in 2024 and beyond.

As two companies that have won over consumers worldwide, Apple (AAPL 0.62%) and Amazon (AMZN 0.27%) are two attractive options. One dominates consumer tech, with leading market shares in smartphones, tablets, headphones, and wearables. Meanwhile, the other is killing it in e-commerce alongside a leading position in cloud computing that could see it profit significantly from artificial intelligence (AI).

Over the last five years, shares in Apple and Amazon have risen around 365% and 79%, respectively. While past growth isn't always an indicator of what's to come, these companies have the financial resources and brand recognition to continue expanding well into the future.

The massive potential of these tech giants means you won't need tens of thousands of dollars to see big gains over the long term. So, got $1,000? Here are two stocks to buy for the long haul.

1. Apple

Apple's stock has hit record heights this year, achieving a market cap above $3 trillion for the first time in June. The milestone came even while the company faced repeated declines in its product segments, which suffered from macroeconomic headwinds affecting businesses across tech.

The iPhone maker posted a revenue dip of 3% year over year in fiscal 2023. Yet loyal investors have largely stuck with the company, trusting its ability to overcome current challenges and deliver stellar gains over the long term.

Their faith in Apple is not unfounded. The tech giant remains a favorite among consumers, who continued to show preference for Apple's products. U.S. smartphone shipments fell throughout this year, tumbling 19% year over year in the third quarter of 2023.

As a result, Samsung's market share fell from 27% in Q1 2023 to 22% in Q3. However, Apple has outperformed its biggest competitor by growing its market share from 52% to 55% in the same period. The popularity of Apple products suggests it has much to gain from the market's inevitable recovery.

In the meantime, it is massively profiting from its digital services business, which posted revenue growth of 9% in fiscal 2023. Income from the App Store and subscription services like Apple TV+ and iCloud make up the company's fastest-growing division, delivering profit margins around 70%.

AAPL Free Cash Flow Chart

Data by YCharts

Apple's forward price-to-earnings ratio of 30 makes it a slightly expensive buy. However, as the chart shows, the company hit close to $100 billion in free cash flow, more than some of its biggest competitors in tech. The company has earned its high valuation and will likely go far over the long term, as it has the funds to continue investing in its business.

Dedicating a little over half of your $1,000 investment would yield three shares in Apple, costing about $580 at its current position.

2. Amazon

Amazon has come a long way since starting as an online book retailer in Seattle almost 30 years ago. The company now boasts majority market shares in e-commerce in dozens of countries and is home to the world's largest cloud platform, Amazon Web Services (AWS). Meanwhile, the potency of its services has resulted in leading positions in countless other sectors, such as attaining the second-largest market share in streaming with Prime Video.

The vast user base of its online retail site has even seen Amazon become the biggest video game retailer in the U.S., responsible for 68% of sales as of September (per Statista).

Amazon's expansion across tech means it has countless opportunities for growth over the long term. According to Fortune Business Insights, the cloud market alone is projected to hit a value of $678 billion this year and expand at a compound annual growth rate of 20% until at least 2030.

Amazon has the largest cloud market share and is heavily investing in expanding its position, adding new AI capabilities to AWS as it cashes in on increased demand for the technology.

MSFT PS Ratio Chart

Data by YCharts

Amazon currently has the lowest price-to-sales ratio (P/S) of the tech firms in the chart above. The metric is calculated by dividing a company's market cap by its trailing-12-month revenue, with Amazon's P/S a bargain compared to its peers. Regarding revenue, Amazon's stock offers the most value out of these companies, making it an attractive option right now.

The remainder of your $1,000 investment (and maybe an additional $20, depending on price fluctuation) would buy about three shares in Amazon. The company has a solid outlook over the next decade, with its shares the perfect buy for investors in for the long haul.