Insect metamorphosis is triggered by the production, secretion and degradation of 20-hydroxyecdysone (ecdysone). In addition to its role in developmental regulation, increasing evidence suggests that ecdysone is involved in innate immunity processes, such as phagocytosis and the induction of antimicrobial peptide (AMP) production. AMP regulation includes systemic responses as well as local responses at surface epithelia that contact with the external environment. At pupariation, Drosophila melanogaster increases dramatically the expression of three AMP genes, drosomycin (drs), drosomycin-like 2 (drsl2) and drosomycin-like 5 (drsl5). We show that the systemic action of drs at pupariation is dependent on ecdysone signalling in the fat body and operates via the ecdysone downstream target, Broad. In parallel, ecdysone also regulates local responses, specifically through the activation of drsl2 expression in the gut. Finally, we confirm the relevance of this ecdysone dependent AMP expression for the control of bacterial load by showing that flies lacking drs expression in the fat body have higher bacterial persistence over metamorphosis. In contrast, local responses may be redundant with the systemic effect of drs since reduction of ecdysone signalling or of drsl2 expression has no measurable negative effect on bacterial load control in the pupa. Together, our data emphasize the importance of the association between ecdysone signalling and immunity using in vivo studies and establish a new role for ecdysone at pupariation, which impacts developmental success by regulating the immune system in a stage-dependent manner. We speculate that this co-option of immune effectors by the hormonal system may constitute an anticipatory mechanism to control bacterial numbers in the pupa, at the core of metamorphosis evolution.