07.28.09

Amazon EC2 Java Library: Base64 encoding of RunInstancesRequest UserData

Posted in Uncategorized at 3:39 pm by Administrator

Amazon EC2 has impressed me and so has the simplicity of the Java programming API for Amazon EC2. However, today I hit a road bump while making steady progress using the amazon-ec2-2009-04-04-java-library. It seems the UserData of RunInstancesRequest should be Base64 encoded to retrieve it correctly on the instance after it is running. I did not find any easy to access documentation that describes this “gotcha” in otherwise easy to use library. Here is a small Java code snippet that worked for me.


RunInstancesRequest request = new RunInstancesRequest();
String myUserData = "blah-blah-blah";
byte[] charArray = myUserData.getBytes();
byte[] encodedData = org.apache.commons.codec.binary.Base64.encodeBase64(charArray, false);
request.setUserData(new String(encodedData));
service.runInstances(request);