Urinary tract infection during pregnancy

Katey
I'm pregnant on my first baby (5 weeks) first trimester. I really had a hard time going frequently to bathroom after 2 days i felt some burning sensations while urination and sometimes i cant hold to pee. Really painful..
So i immediately go to my obgyne urinalysis and urine culture done results that i have bacterial infection (E.coli). My doctor prescribe me medication
Ampicilin 500 mg for every 6hours for 4 days only. Then plenty of water and hydrate
Is this medication is safe for first trimester? 
I hope theres no harm on my baby, really worried to take it. 
Hope to give me feedback, thanks