Is it possible to swear in front of a notary in the US that the contents of a document are true and create an affidavit even if that affidavit won't be used in court. If so, are their laws that punish breaking such an affidavit?
Yes anyone can create an affidavit by swearing to the truth of some document or statement before a notary, or some other authorized person such as a magistrate. However, this is normally done only when there is an intention to submit such a document as part of some legal proceeding.
Falsely making an affidavit can be a crime, but only if:
The document is submitted to a court or to some official who requires that it be under oath, or as part of an attempt to induce some official to make some decision or take some action;
The falsehood is materiel. For example if a person started an affidavit with "I was born on April 2, 1961" when the correct year was 1962, and the person's exact age was not important for the case at hand, that would not be a punishable false statement.
The false statement must have been made knowingly, not by mistake or misunderstanding.
For US Federal law, 18 U.S. Code § 1621 provides::
(1) having taken an oath before a competent tribunal, officer, or person, in any case in which a law of the United States authorizes an oath to be administered, that he will testify, declare, depose, or certify truly, or that any written testimony, declaration, deposition, or certificate by him subscribed, is true, willfully and contrary to such oath states or subscribes any material matter which he does not believe to be true; or
(2) in any declaration, certificate, verification, or statement under penalty of perjury as permitted under section 1746 of title 28, United States Code, willfully subscribes as true any material matter which he does not believe to be true;
is guilty of perjury and shall, except as otherwise expressly provided by law, be fined under this title or imprisoned not more than five years, or both. This section is applicable whether the statement or subscription is made within or without the United States.
Laws of the various US states and of other countries will differ in detail, but the general elements are similar in most cases.