Natural populations of most organisms harbor substantial genetic variation for resistance to infection. The continued existence of such variation is unexpected under simple evolutionary models that either posit direct and continuous natural selection on the immune system or an evolved life history "balance" between immunity and other fitness traits in a constant environment. However, both local adaptation to heterogeneous environments and genotype-by-environment interactions can maintain genetic variation in a species. In this study, we test Drosophila melanogaster genotypes sampled from tropical Africa, temperate northeastern North America, and semi-tropical southeastern North America for resistance to bacterial infection and fecundity at three different environmental temperatures. Environmental temperature had absolute effects on all traits, but there were also marked genotype-by-environment interactions that may limit the global efficiency of natural selection on both traits. African flies performed more poorly than North American flies in both immunity and fecundity at the lowest temperature, but not at the higher temperatures, suggesting that the African population is maladapted to low temperature. In contrast, there was no evidence for clinal variation driven by thermal adaptation within North America for either trait. Resistance to infection and reproductive success were generally uncorrelated across genotypes, so this study finds no evidence for a fitness tradeoff between immunity and fecundity under the conditions tested. Both local adaptation to geographically heterogeneous environments and genotype-by-environment interactions may explain the persistence of genetic variation for resistance to infection in natural populations.