Fixed #1698 and put unit tests on it
Merge pull request #2396 from phspagiari/userdata_encode_fix

EC2 UserData encoding fix (Full version of #1698). Fixes #2396.