The primary objective of this study was to identify associations between teat apex microbiome and Staphylococcus aureus intramammary infection (IMI) risk in primiparous cows during the first 5 weeks after calving. We performed a case-control study using shotgun metagenomics of the teat apex and culture-based milk data collected longitudinally from 710 primiparous cows on 5 organic dairy farms. We observed a strong association between S. aureus DNA in the metagenomic teat apex data prior to parturition and the odds of S. aureus IMI after parturition (OR = 38.9, 95% CI: 14.84-102.21). Differential abundance analysis confirmed this association, with cases having a 23.8 higher log fold change (LFC) in abundance of S. aureus in their samples compared to controls. Of the most prevalent microorganisms in controls, those associated with a lower risk of post-calving S. aureus IMI included Microbacterium phage Min 1 (OR = 0.37, 95% CI: 0.25-0.53), Corynebacterium efficiens (OR = 0.53, 95% CI: 0.30-0.94), Kocuria polaris (OR = 0.54, 95% CI: 0.35-0.82), Micrococcus terreus (OR = 0.64, 95% CI: 0.44-0.93) and Dietzia alimentaria (OR = 0.45, 95% CI: 0.26-0.75). Microcin B17 was the most prevalent antibacterial peptide on the teat apex of cases and controls (99.7% in both groups). The predicted abundance of Microcin B17 was also higher in cases compared to controls (LFC 0.26). Cow and farm random effects often explained a large proportion of the observed variability in the teat apex microbiome, suggesting that our results need to be interpreted within the context of the random effects.