In many instances of staphylococcal infections, such as bacteremia, cefazolin is an alternative to penicillin in patients who are allergic to penicillin. However, there is still potential for a reaction to occur with cefazolin and other cephalosporins in patients allergic to penicillin. Resistance to cefazolin is seen in several species of bacteria, such as Mycoplasma and Chlamydia, in which case different generations of cephalosporins may be more effective. Cefazolin does not fight against Enterococcus, anaerobic bacteria or atypical bacteria among others. Bacterial susceptibility
As a first-generation cephalosporin antibiotic, cefazolin and other first-generation antibiotics are very active against gram-positive bacteria and some gram-negative bacteria. Their broad spectrum of activity can be attributed to their improved stability to many bacterial beta-lactamases compared to penicillins. Spectrum of activity
Gram-positive aerobes:
Staphylococcus aureus (including beta-lactamase producing strains)
Staphylococcus epidermidis
Streptococcus pyogenes, Streptococcus agalactiae, Streptococcus pneumoniae and  other strains of streptococciGram-Negative Aerobes:

Escherichia coli
Proteus mirabilis

Non susceptible
The following are not susceptible:
Methicillin-resistant staphylococcus aureus
Enterococcus
most strains of indole positive Proteus (Proteus vulgaris)
Enterobacter spp. Morganella morganii
Providencia rettgeri
Serratia spp. Pseudomonas spp. Listeria

Special populations
Pregnancy
Cefazolin is pregnancy category B, indicating general safety for use in pregnancy. Caution should be used in breastfeeding as a small amount of cefazolin enters the breast milk. Cefazolin can be used prophylactically against perinatal Group B streptococcal infection (GBS). Although penicillin and ampicillin are the standard of care for GBS prophylaxis, penicillin-allergic women with no history of anaphylaxis can be given cefazolin instead. These patients should be closely monitored as there is a small chance of an allergic reaction due to the similar structure of the antibiotics.
U.S. National Library of Medicine.