The first amplitude analysis of the $B^pm to pi^pm K^+ K^-$ decay is reported based on a data sample corresponding to an integrated luminosity of 3.0 fb$^{-1}$ of $pp$ collisions recorded in 2011 and 2012 with the LHCb detector. The data is found to be best described by a coherent sum of five resonant structures plus a nonresonant component and a contribution from $pipi leftrightarrow KK$ $S$-wave rescattering. The dominant contributions in the $pi^pm K^mp$ and $K^{+}K^{-}$ systems are the nonresonant and the $B^pm to rho(1450)^{0}pi^{pm}$ amplitudes, respectively, with fit fractions around $30%$. For the rescattering contribution, a sizeable fit fraction is observed. This component has the largest $CP$ asymmetry reported to date for a single amplitude of $(-66 pm 4 pm 2)%$, where the first uncertainty is statistical and the second systematic. No significant $CP$ violation is observed in the other contributions.