Those "Shepherds with AK47s and junior chemistry sets" were trained militia - like literally trained in Saudi Arabia, Afghanistan, Syria, and pre-invasion Iraq by actual military. Iraq had stockpiled caches of arms and explosives for a resistance going back to the Iran-Iraq War. They were able to get support because they were rallying against a foreign invader, which will almost always unite otherwise opposed factions. They were being bankrolled by other nations and regional powers. The local authorities in Iraq were largely underpaid, unmotivated, seen as traitors supporting the foreign invaders, and at constant risk for their lives. We have had occupying forces there for nearly 18 years, a war which is domestically unpopular and expensive and suffered several costly mistakes in the use of private military contractors.
And yet the idea that they "kicked us right the fuck out of Iraq" is for wrong. IS has been losing territory and control steadily in Iraq for years.
https://www.bbc.com/news/world-middle-east-27838034
But sure, imagine that being able to buy an AR-15 will protect you from the US Army.