How do I turn what was in a bash script file into a base64 encoded string that will work with Amazon's Java API? This code expresses what I'm trying to do but results in an exception:
...Caused by: com.amazonaws.AmazonServiceException: Invalid BASE64 encoding of user data (Service: AmazonEC2; Status Code: 400; Error Code: InvalidParameterValue...
String startupUserData = "#!/bin/bash cd /home/ubuntu/myTestDir; mvn test -PmyBuild";
startupUserData = org.apache.commons.codec.binary.Base64.encodeBase64String(startupUserData.getBytes());
runRqst.withImageId(_computerAmi)
.withInstanceType(instanceSize)
.withMinCount(hwRequest.numHwComputers)
// .withMaxCount(Utils.MAX_EC2_INSTANCES_AT_A_TIME) // NOT THIS
.withMaxCount(hwRequest.numHwComputers)
.withKeyName(_keyName)
.withSecurityGroups(_securityGroup)
.withUserData(startupUserData);
ec2.runInstances(runRqst);
Edit: com.amazonaws.util.Base64.encodeAsString() quells the exception but the script still doesn't execute. How should the string be formatted? Should there be carriage returns?