The Swiss Confederation was founded in 1291 as a defensive alliance among three cantons .