EC2 Java SDK - 用户数据脚本
我正在寻找一种将用户数据脚本附加到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可以在实例运行请求中插入用户数据,在本例中专门用于加入 ECS 集群:
This works to insert user data in an instance run request, in this case specifically to join an ECS cluster:
您使用的 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