Madrid and Bayern have met 22 times previously in the competition - more than any other clubs - and the Spanish giants destroyed the Bundesliga leaders 5-0 on aggregate in the semi-finals three years ago.
Bayern dominated possession in the first leg of that clash but Karim Benzema's goal earned Madrid a 1-0 lead at home, which they compounded with a thrilling display at the Allianz Arena, and Bayern captain Lahm said his side have to learn lessons about Madrid's danger on the break.
"The Champions League is such a tough competition," Lahm told Kicker. "We have to avoid being counter-attacked like we were the last time we played against them.
"This team has had to get used to not having the ball sometimes and knowing that it is okay for the opposition to have it.
"If the defence each maintains our position, then we have the possibility to avoid being countered."
Lahm is due to retire at the end of the season, so the 2014 World Cup winner is playing in the Champions League for the last time, with the 33-year-old noting Bayern will have to be at the top of their game to progress against holders Madrid.
"Everyone in our team has to be at their best because we are playing against an elite team with world-class players in each position," Lahm added.
"That's why we must work hard as a team and to do that we don't only need the 11 starters, but everyone in the squad."