First Minister Carwyn Jones described it as a "major coup for Wales". The company looked at 100 sites in the UK.
It is understood the Welsh Government offered a £3m grant to secure the jobs.
The Celtic Business Park site, bordering the south Wales main railway line to London, was chosen from one of four potential shortlisted sites.
Media caption'Vote of confidence' in Welsh manufacturing
CAF, which has a turnover of more than £1bn and employs about 7,000 people worldwide, built trains for the Heathrow Express in London.
CAF, whose headquarters are in Spain's Basque Country, is in the bidding process for new London Underground trains.
It has confirmed an interest to tender for the HS2 high-speed line that, in the first phase, will link London and Birmingham, and has also supplied trams to Edinburgh and Birmingham
"Today's announcement is a major coup for Wales and a big vote of confidence in our manufacturing industry," said Mr Jones.
"We are, once again, competing and beating others on the world stage to secure significant investment to Wales.
"This £30 million investment is a major economic boost that we hope will kick-start the growth of our rail sector and create hundreds of highly skilled, very well paid jobs."
The jobs boost for south east Wales comes weeks after the Welsh Government rejected a £210m plea to help build the Circuit of Wales in Ebbw Vale, Blaenau Gwent.
Economy Secretary Ken Skates said: "As well as creating 300 high calibre jobs, it also coincides with our major Welsh Government Metro rail investment in south east Wales, details of which will be unveiled over the coming weeks."
The project will be the first centre of its type in Wales and the Welsh Government said the site would allow for significant future expansion, providing potential for further investment and employment.
The new facility will enable the company to assemble, test and commission new vehicles and have capacity to undertake future manufacturing projects as well as maintenance and servicing.
Richard Garner, CAF's UK director, said: "We now have solid economic basis for further increasing our footprint in the UK.
"The establishment of this facility will serve CAF's long-term aspirations to dominate the UK market for many years to come."