AWS EC2用户数据无法使用Java API

时间:2013-03-11 17:37:12

标签: amazon-ec2

我有以下代码来创建包含用户数据的EC2实例...

但是在创建EC2实例后它不会创建日志文件。

如果我做错了什么,请帮助我。

代码如下: -

class EC2Demo{
<br>public static void main(String str[]){

{  //AWS Credencial lines......

        RunInstancesRequest request = new RunInstancesRequest();
        request.setInstanceType(InstanceType.T1Micro);
        request.setMinCount(1); 
        request.setMaxCount(1);
        request.setImageId("ami-ab844dc2");
        request.setKeyName("VIIT1");
        ArrayList<String> securityGroups = new ArrayList<String>();
        securityGroups.add("VIITgroup");
        request.setSecurityGroups(securityGroups);
        Placement placement=new Placement();
        placement.setAvailabilityZone("us-east-1d");
        request.setPlacement(placement);

        request.setUserData(getUserDataScript());
        System.out.println("Shell Script");
        System.out.println("-------->>>>"+ec2.runInstances(request));
        //}
        System.out.println("End");

    }
    private static String getUserDataScript(){
        ArrayList<String> lines = new ArrayList<String>();
        lines.add("#! /bin/bash");
        lines.add("cd /home");
        lines.add("echo `ls` >> /home/test.log");
        //lines.add("shutdown -h 0");
        String str = new String(Base64.encodeBase64(join(lines, "\n").getBytes()));

        return str;
    }

    static String join(Collection<String> s, String delimiter) {
        StringBuilder builder = new StringBuilder();
        Iterator<String> iter = s.iterator();
        while (iter.hasNext()) {
            builder.append(iter.next());
            if (!iter.hasNext()) {
                break;
            }
            builder.append(delimiter);
        }
        return builder.toString();
    }

}

0 个答案:

没有答案