We generalize the well-known Bonnor-Melvin solution of the Einstein-Maxwell equations to the case of a nonvanishing cosmological constant. The spacetime is again cylindrically symmetric and static but, unlike the original solution, it truly represents a homogeneous magnetic field.