【问题标题】:Attaching role while creating boto3 ec2 Instance Error在创建 boto3 ec2 实例错误时附加角色
【发布时间】:2018-07-31 08:26:07
【问题描述】:

我刚开始阅读 AWS 并尝试创建一个 ec2 实例并为其附加一个角色。据我所知,我需要创建一个实例配置文件,为其附加一个角色,然后将其附加到 EC2 实例,但由于下面描述的错误,我无法这样做。 我没有足够的时间,所以任何帮助将不胜感激。

这是我的代码:

创建角色:

role = iam.create_role(
                Path='/',
                RoleName=self.roleName,
                AssumeRolePolicyDocument= str1,
                Description="Allow EC2 instances to call AWS services"
            )
        #roleArn=role["Role"]["Arn"]
        response = iam.attach_role_policy(
            RoleName=self.roleName, PolicyArn='arn:aws:iam::aws:policy/AdministratorAccess')

        response = iam.attach_role_policy(
            RoleName=self.roleName,
            PolicyArn='arn:aws:iam::aws:policy/AmazonS3FullAccess'
        )

创建实例配置文件并附加角色:

instance_profile = iam.create_instance_profile(InstanceProfileName=self.instanceProfile,Path='/')
iam.add_role_to_instance_profile(InstanceProfileName=self.instanceProfile, RoleName=self.roleName)

创建 EC2 实例:

instance = ec2.create_instances(
                ImageId=imageId,
                MinCount=1,
                MaxCount=1,
                KeyName=keyName,
                InstanceType=instanceType,
                IamInstanceProfile={
                        'Name': instanceProfile
                 }) code here
            )

运行上述代码时出现以下错误:

调用时发生错误(InvalidParameterValue) RunInstances 操作:参数的值 (mnbvinst) iamInstanceProfile.name 无效。 IAM 实例配置文件名称无效

一旦我重新运行应用程序并尝试为实例提供相同的名称,我就会收到以下错误。

调用时发生错误 (LimitExceeded) AddRoleToInstanceProfile 操作:不能超过配额 InstanceSessionsPerInstanceProfile: 1

我尝试使用 run_instances 方法创建 EC2 实例,但同样的错误仍然存​​在

变量:

iam = boto3.client('iam')
ec2 = boto3.resource('ec2')
client = boto.client('ec2')

【问题讨论】:

  • 您没有传递正确的“instanceProfile”名称。我想应该是 InstanceProfileName
  • @SudharsanSivasankaran instanceProfile 变量实际上包含实例配置文件的名称。
  • 也许这不是一个虚名。如果创建了实例配置文件,您可以签入 UI 吗?

标签: amazon-web-services amazon-s3 amazon-ec2 boto3


【解决方案1】:

您的代码有:

IamInstanceProfile={
    'Name': instanceProfile
}

但是,没有名为 instanceProfile 的变量。

你似乎也在混合instanceProfileinstance_profile

instance_profile = iam.create_instance_profile(InstanceProfileName=self.instanceProfile,Path='/')
iam.add_role_to_instance_profile(InstanceProfileName=self.instanceProfile, RoleName=self.roleName)

第一行创建instance_profile,但第二行引用self.instanceProfile

你有一些清理工作要做。

【讨论】:

  • 您好,self.instanceProfile 的值传递给了变量 instanceProfile。抱歉,它不是完整的代码,而只是与问题相关的部分。同样 create_instance_profile() 和 add_role_to_instance_profile() 接受实例配置文件的名称作为第一个参数或 add_role_to_instance_profile 接受实例配置文件的对象?
【解决方案2】:

此实例配置文件是否正在多个资源中使用? IAM 实例配置文件限制同时凭证会话的数量。

您可能有太多的 EC2 资源提供相同的实例配置文件。

您可以创建实例配置文件并使用该配置文件,也可以在此处请求 AWS 增加限制: https://console.aws.amazon.com/support/home#/case/create?issueType=service-limit-increase&limitType=service-code-iam-groups-and-users

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-20
    • 2019-08-23
    • 2018-10-26
    • 2014-08-15
    • 2020-09-02
    • 1970-01-01
    • 2015-12-28
    • 2019-11-21
    相关资源
    最近更新 更多