The purpose of hardship is to help people remember Hashem. When everything goes smoothly, people tend to forget about Hashem. It shouldn't be that way. We should remember Hashem during the good times as well to praise Him, but the nature of people is that they forget Hashem when things go well. But when they are going through hard times, they turn to Hashem.
The Jewish nation was near Eretz Yisrael, but they couldn't enter. Edom didn't let them pass through their land. The nation worried they would have to remain in the desert, and they began complaining about the manna, the water, etc. As a punishment, Hashem sent snakes that bit many people. When the Yidden repented, Hashem told Moshe (21:8) עשה לך שרף ושים אתו על נס והיה כל הנשוך וראה אתו וחי, "Make a snake and put it on a staff. Whoever was bit will see it and live."
The Mishnah (Rosh Hashanah 29.) explains: "Could a snake cause death, and could a snake generate life? The answer is: When Yisrael turned their eyes to heaven, and connected their heart to their Father in heaven, they were healed, and otherwise, they were ill." It wasn't the copper snake that healed them. But, when the Jewish nation looked up towards the copper snake, they also turned their hearts to Hashem and put their trust in Him, and that is what healed them.
The Or HaChaim (21:8) makes an important observation: "Hashem didn't remove the snakes, He allowed them to continue biting, and the nation was healed when they looked up to their Father in heaven. This is because Hashem wanted their heart to always be close to Him, like a son who eats at his father's table morning and night."
Hashem could have removed the snakes, but this wasn't Hashem's plan in creation. Instead, Hashem wants there to be problems and that we should turn to Hashem for salvation because only in that manner we will remain close to Hashem.
For forty years, Hashem led the nation in a desert where they were completely dependent on Him for food and water. He purposely didn't lead them through inhabited areas. This was to train them to place their trust solely in Him. The Or HaChaim concludes that a life of ease causes people to completely lose sight of Hashem's hashgachah!
Similarly, the Rabbeinu B'Chaya writes on the passuk (21:5) "כי אין לחם ואין מים": "This complaint is surprising since they had man; it fell every single day. They also had plenty of water because of the miracle of the sela." The Rabbeinu B'Chaya answers: "Their complaint was: Our life is unusual since we don't have bread and water like all the other nations. We are led on a path of reward and punishment. We are led in a very peculiar way, in comparison to the nations of the world."
They didn't realize that these difficulties were all for their benefit. As the Rabbeinu B'Chaya writes: "Because of the greatness of the dor hamidbar, they received man every day so that they should turn their eyes to heaven. The purpose was to get them accustomed to having emunah and bitachon in Hashem."