AWS:如何确保在启动ec2服务器时初始化并传播实例配置文件

时间:2016-11-08 14:00:22

标签: amazon-web-services amazon-ec2

我正在使用带有实例配置文件的ec2实例配置文件启动服务器。

问题是简介有时不是"那里"在创建它之后,即使我等了10秒钟:

  # Create new Instance Profile
  instanceProfile = self.iam.create_instance_profile(InstanceProfileName=instProfName)
  instanceProfile.add_role(RoleName="...")

  time.sleep(10)

  # Create the Instance
  instances = self.ec2.create_instances(
     # ...
     IamInstanceProfile={
        "Name":instanceProfile.instance_profile_name
     }
  )

有没有办法等待它传播?

我的第一次尝试是:

  error = 30
  dryRun = True

  while error > 0:
     try:
        # Create the Instance
        instances = self.ec2.create_instances(
           DryRun=dryRun
           # ...               
           IamInstanceProfile={
              "Name":instanceProfile.instance_profile_name
           }
        )
        if not dryRun:
           break;

        dryRun = False

     except botocore.exceptions.ClientError as e:
        error = error - 1;

但是如何才能获得IAM配置文件错误?

1 个答案:

答案 0 :(得分:0)

大部分AWS使用"最终的一致性"。这意味着在进行更改后,需要一些时间才能在系统中传播。

创建实例配置文件后:

  1. 延迟5或10秒(或其他时间你感到舒服),
  2. 使用您的实例个人资料名称致电iam.get_instance_profile
  3. 重复延迟并检查,直到get_instance_profile返回您的信息。
  4. 你可以做的另一件事是抓住"实例个人资料未找到错误"在ec2.create_instances通话期间,如果您收到错误,请延迟并重复此操作。