如何通过环境变量编程时,从图像开始一个新的Amazon EC2?环境变量、图像、Amazon

2023-09-11 08:34:45 作者:神经病中”』

我使用AWS的Java API RunInstance()从我的自定义AMI映像启动一个新的EC2实例。我如何通过环境变量设置为新的EC2实例,如数据库URL,AWS凭据等?

I am using AWS Java API RunInstance() to start a new EC2 instance from my custom AMI image. How do I pass environment variables to the new EC2 INSTANCE such as database url, AWS credentials etc. ?

推荐答案

http://alestic.com/2009/06/ec2-user-data-scripts解释如何使用用户数据执行此操作。有关使用Java的陷阱看到 AmazonEC2推出与用户数据。

http://alestic.com/2009/06/ec2-user-data-scripts explains how to do this with user-data. for gotchas about using Java see AmazonEC2 launch with userdata.

请注意,我已经看到提到,这并不与Windows的工作,只有Unix的。

note that I've seen mention that this doesn't work with Windows, only Unix.

[更新]就在这里设置环境变量的详细数据:https://forums.aws.amazon.com/message.jspa?messageID=139744

[update] more data on setting environment variables here: https://forums.aws.amazon.com/message.jspa?messageID=139744

[多后测试]对我来说,呼应环境变量到的/ etc /环境中效果最好,是这样的:

[after much testing] for me, echoing the environment variables into /etc/environment works best, like this:

 reservation = connection.run_instances(image_id = image_id,
  key_name = keypair,
  instance_type = 'm1.small',
  security_groups = ['default'],
  user_data = '''#!/bin/sh\necho export foozle=barzle >> /etc/environment\n''')

然后在登录:

then upon login:

ubuntu@ip-10-190-81-29:~$ echo $foozle
barzle