If you've ever dreamed of becoming a millionaire, that goal may be within reach. It's easier than you might think to get rich investing in the stock market, but it's important to start with the right strategy.

Becoming a millionaire investor takes time and patience. If you invest in risky stocks hoping to make it big overnight, you're more likely to lose a lot of money than get rich. Instead, it's better to focus on less risky investments.

Here's what it takes to accumulate $2 million or more by investing in S&P 500 index funds.

Large pile of hundred dollar bills

Image source: Getty Images.

Why invest in index funds?

An index fund is a group of stocks or bonds that track a certain stock market index. They may not be the most exciting investment, but they're a powerhouse when it comes to building wealth.

The fund mirrors the performance of the index it tracks. An S&P 500 index fund, then, will mirror the performance of the S&P 500. Some of the most popular of these funds include Fidelity 500 Index Fund (FXAIX 1.20%), Schwab S&P 500 Index Fund (SWPPX 1.19%), and iShares Core S&P 500 (IVV -0.04%).

The S&P 500 is one of the best representations of the stock market as a whole. It also has a strong track record and has returned an average of 10% per year since inception.

Like any investment, S&P 500 index funds are vulnerable to market downturns. However, because they follow the market as a whole, they're very likely to recover from even the worst crashes. This makes them fantastic long-term investments because no matter what happens with the market, your money will continue to grow over time.

How much to invest to earn $2 million

Are you're ready to invest in S&P 500 index funds? How much would you actually need to contribute to reach the $2 million mark?

Let's assume you're earning a 10% annual return on your investments and you're beginning to save at age 30. If you were to invest $525 per month, you'd have around $2 million in total savings by age 67.

If you're already past age 30 and haven't started saving yet, don't give up on your goal. You can still earn more than $2 million in savings, but you'll need to save a bit more each month. Just remember that the longer you wait to begin saving, the more you'll need to invest every month. Index funds are long-term investments, so it's best to start saving as early in life as possible.

In some cases, saving $2 million may not be realistic. For example, if you're 40 years old and want to save $2 million by age 67, you'd need to save around $1,400 per month, assuming you're still earning a 10% annual rate of return. But if you were able to save $700 per month, you could reach $1 million in savings, all other factors remaining the same.

Not everyone will need $2 million in savings, and investing whatever you can afford is better than doing nothing. Regardless of how much you're able to invest, starting now will make it easier to become rich someday.