传递一个脚本,而在亚马逊EC2启动实例亚马逊、而在、脚本、实例

2023-09-11 09:04:20 作者:孤久必疏

是否有可能通过一个脚本,而我开始一个实例?这样的实例将执行该脚本一旦其初始化?

Is it possible to pass a script while I'm starting an instance? So that the instance will execute that script once its initialized?

public static void startInstance(final String instanceId) {
    StartInstancesRequest startRequest = new StartInstancesRequest().withInstanceIds(instanceId);
    logger.info(String.format("Starting instance '%s':...", instanceId));
    ec2.startInstances(startRequest);       
}

你知道吗?脚本将是一种启动脚本,我需要尽快执行的实例已准备就绪。先谢谢了。

Any idea? Script will be kind of startup script which I need to execute as soon as the instance is ready. Thanks in advance.

推荐答案

我公司推出的概念的在社区的Ubuntu AMI的2009年(的一些想法来自RightScale的简化版本)用户数据的脚本的:

I introduced the concept of user-data scripts in the community Ubuntu AMIs in 2009 (simplified version of some ideas from RightScale):

http://alestic.com/2009/06/ec2-user-data-scripts

它归结为:

如果实例的用户数据开始的两个字符#!则实例运行它作为第一个启动的root用户。

If the instance user-data starts with the two characters #! then the instance runs it as the root user on the first boot.

此功能后来被包含在Ubuntu官方的AMI在CloudInit包:

This feature was later included in the official Ubuntu AMIs in the CloudInit package:

https://help.ubuntu.com/community/CloudInit

亚马逊现在包括CloudInit包在自己亚马逊的Linux AMI的:

Amazon now includes the CloudInit package in their own Amazon Linux AMIs:

http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/AmazonLinuxAMIBasics.html

其他的发行版,欢迎(可能的话)在其AMI的这一功能。

Other distros are welcome to (and may already) include this feature in their AMIs.