The piezoelectric properties of Sc- or Er-doped wurtzite AlN at different dopant concentrations were investigated using the first-principles calculations by analyzing the lattice distortion and the bonding type. It is found that when x = 0.5, Sc- or Er-doped AlN presents the highest piezoelectric constants in several dopant concentrations, namely, 25.50 pC/N for Sc0.5Al0.5N and 9.34 pC/N for Er0.5Al0.5N. By calculating the coordinate difference between positive and negative charge centers, it is confirmed that the enhancement of piezoelectric constant for M-doped AlN (M = Sc, Er) is related to the long polarization length. Meanwhile, Mulliken population analysis proves that the improved ionicity of AlN bond after doping is another reason for the enhancement of piezoelectric constant. This work provides a theoretical basis for further improving the piezoelectric properties of AlN and its application in the high-frequency devices.