We'd have to defend against a nuclear Iran: Defense secretary

US Defense Sec. meets with tech titans
Defense Sec. Carter: Goal to prevent nuclear Iran   

Secretary of Defense Ashton Carter said Thursday that the United States would have to defend itself against a nuclear Iran, but he said that a good deal with the country on its nuclear program would ensure stability.

Earlier in the day, Secretary of State John Kerry said that major progress has been made in talks with Iran. However, he noted that some tough issues remained unresolved.

"One of the purposes of Secretary Kerry's talks is to stop Iran from having a nuclear weapon. Those talks are ongoing now. I don't know how they'll turn out," Carter said in an interview that aired on CNBC's "Closing Bell."

"I think the president has made it clear that he will only agree to a good deal and a good deal would stop Iran from getting a nuclear weapon, which would make it much easier for us to ensure stability and protect ourselves."

Read MoreKerry: Real progress being made in Iran nuclear talks

The negotiations are about restricting Iran's nuclear program to nonmilitary usage in exchange for sanctions relief.

Carter, who spoke to media moguls and technology leaders at Allen & Co.'s annual summit in Sun Valley, Idaho, on Thursday, also addressed the need for cooperation between Silicon Valley and the Pentagon.

He called it a national security issue.

"If you are thinking about the long term … security and vitality of the Department of Defense, I need to be concerned about things, and I am, like science, technology engineering, mathematics, education, schools and all the other things that go into making what has been for all of our lives and our generation now the most innovative country in the world."

—CNBC's Julia Boorstin and Reem Nasr contributed to this report.