The gunman who opened fire at the U.S. embassy near Beirut was charged with affiliation to the terrorist Islamic State group, security and judicial officials said.