The impact of the three-dimensional (3D) tokamak geometry from external magnetic perturbations (MPs) on the local edge stability has been examined in high confinement mode (H-mode) plasmas with edge localised modes (ELMs) in ASDEX Upgrade. The 3D geometry has been probed using rigidly rotating MP fields. The measured distortions of the plasma boundary are compared to single-fluid ideal magnetohydrodynamic (MHD) equilibria using VMEC and MARS-F applying resistive as well as ideal MHD. Both codes reproduce well the measured radial displacements of the edge density and temperature profiles in amplitude, toroidal phase and their dependence on the applied poloidal spectrum. The induced 3D geometry distorts the local magnetic shear, which reduces locally the stabilising effect from field-line bending at certain most unstable field lines. Around these field lines, we observe additional stable ideal MHD modes with clear ballooning structure in-between ELMs. After their immediate appearance, they saturate and then grow on timescales of the pedestal pressure recovery. The subsequent ELMs show strongly localised magnetic perturbations of the initial crash and accompanied energetic electrons around the same most unstable field lines. These are strong signatures that filaments at the ELM onset preferentially erupt on these most unstable (’bad’) field lines with their unfavourably 3D geometry where preceding ballooning modes are observed.