Herbivores are often implicated in the generation of the extraordinarily diverse tropical flora. One hypothesis linking enemies to plant diversification posits that the evolution of novel defenses allows plants to escape their enemies and expand their ranges. When range expansion involves entering a new habitat type, this could accelerate defense evolution if habitats contain different assemblages of herbivores and/or divergent resource availabilities that affect plant defense allocation. We evaluated this hypothesis by investigating two sister habitat specialist ecotypes of Protium subserratum (Burseraceae), a common Amazonian tree that occurs in white-sand and terra firme forests. We collected insect herbivores feeding on the plants, assessed whether growth differences between habitats were genetically based using a reciprocal transplant experiment, and sampled multiple populations of both lineages for defense chemistry. Protium subserratum plants were attacked mainly by chrysomelid beetles and cicadellid hemipterans. Assemblages of insect herbivores were dissimilar between populations of ecotypes from different habitats, as well as from the same habitat 100 km distant. Populations from terra firme habitats grew significantly faster than white-sand populations; they were taller, produced more leaf area, and had more chlorophyll. White-sand populations expressed more dry mass of secondary compounds and accumulated more flavone glycosides and oxidized terpenes, whereas terra firme populations produced a coumaroylquinic acid that was absent from white-sand populations. We interpret these results as strong evidence that herbivores and resource availability select for divergent types and amounts of defense investment in white-sand and terra firme lineages of Protium subserratum, which may contribute to habitat-mediated speciation in these trees.