EC2 Java SDK - 用户数据脚本

发布于 2024-09-08 02:26:32 字数 2049 浏览 5 评论 0原文

我正在寻找一种将用户数据脚本附加到 Java SDK 中的 EC2 RunRequest 的方法(相当于 ec2-run-instances ami-1234567 -fstartup-script.zip(对于命令行工具)。

几个 things 我读过表明任何带有“#!”的用户数据字符串都会执行,但情况似乎并非如此。

这可能吗?

仅供参考:这是我的测试类:

public class AWSTest {

    public static void main(String[] args) {

        AWSCredentials credentials = new BasicAWSCredentials("access-key","secret-access-key");
        AmazonEC2Client ec2 = new AmazonEC2Client(credentials);
        RunInstancesRequest request = new RunInstancesRequest();
        request.setInstanceType(InstanceType.M1Small.toString());
        request.setMinCount(1);
        request.setMaxCount(1);
        request.setImageId("ami-84db39ed");
        request.setKeyName("linux-keypair");
        request.setUserData(getUserDataScript());
        ec2.runInstances(request);    
    }

    private static String getUserDataScript(){
        ArrayList<String> lines = new ArrayList<String>();
        lines.add("#! /bin/bash");
        lines.add("curl http://www.google.com > google.html");
        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();
    }

}

不幸的是,在我运行这个之后,我能够通过 SSH 进入盒子,并确认它

  • 没有关闭并且
  • 没有下载文件。

非常感谢任何帮助。

最好的,

扎克

I'm looking for a way to attach a user data script to an EC2 RunRequest in the Java SDK (the equivalent of ec2-run-instances ami-1234567 -f startup-script.zip for the command line tool).

Several things I've read indicate that anything user data string with "#! " will execute, but this doesn't seem to be the case.

Is this even possible?

FYI: here's my test class:

public class AWSTest {

    public static void main(String[] args) {

        AWSCredentials credentials = new BasicAWSCredentials("access-key","secret-access-key");
        AmazonEC2Client ec2 = new AmazonEC2Client(credentials);
        RunInstancesRequest request = new RunInstancesRequest();
        request.setInstanceType(InstanceType.M1Small.toString());
        request.setMinCount(1);
        request.setMaxCount(1);
        request.setImageId("ami-84db39ed");
        request.setKeyName("linux-keypair");
        request.setUserData(getUserDataScript());
        ec2.runInstances(request);    
    }

    private static String getUserDataScript(){
        ArrayList<String> lines = new ArrayList<String>();
        lines.add("#! /bin/bash");
        lines.add("curl http://www.google.com > google.html");
        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();
    }

}

Unfortunately, after I run this, I'm able to SSH into the box, and confirm that

  • It hasn't shut down and
  • It didn't download the file

Any assistance is greatly appreciated.

Best,

Zach

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还在原地等你 2024-09-15 02:26:32

这可以在实例运行请求中插入用户数据,在本例中专门用于加入 ECS 集群:

private static String getECSuserData(String clusterName) {
    String userData = "";
    userData = userData + "#!/bin/bash" + "\n";
    userData = userData + "echo ECS_CLUSTER=" + clusterName + " ";
    userData = userData + ">> /etc/ecs/ecs.config";
    String base64UserData = null;
    try {
        base64UserData = new String( Base64.encodeBase64( userData.getBytes( "UTF-8" )), "UTF-8" );
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return base64UserData;
}

This works to insert user data in an instance run request, in this case specifically to join an ECS cluster:

private static String getECSuserData(String clusterName) {
    String userData = "";
    userData = userData + "#!/bin/bash" + "\n";
    userData = userData + "echo ECS_CLUSTER=" + clusterName + " ";
    userData = userData + ">> /etc/ecs/ecs.config";
    String base64UserData = null;
    try {
        base64UserData = new String( Base64.encodeBase64( userData.getBytes( "UTF-8" )), "UTF-8" );
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return base64UserData;
}
白云悠悠 2024-09-15 02:26:32

您使用的 AMI 可能不支持用户数据脚本?
请使用 www.alestic.com 上的 AMI。

也是一个很好的参考 http://alestic.com/2009/06/ec2-用户数据脚本

It could be possible that the AMI your using does not support user-data script?
Please use the AMI's found at www.alestic.com.

A good reference also http://alestic.com/2009/06/ec2-user-data-scripts

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文