God is passing the sentence of death on a culture or people, the Amalekites, for their personal and collective “sin,” just as he told Adam and Eve when they sinned they would die. Moral consequences of their own behavior.
Thankfully, beyond the grave, God is able to absolve or recompense the “innocent” children and infants among the Amalekites, something that will no doubt NOT compute in your mentality — but nevertheless true.
IOW, there is no ultimate justice this side of eternity, only human approximations of it, based on God’s unchanging moral standards.
That’s the same sort of “justification” Muslims give, too.
It completely contradicts the essence of the Golden Rule.