amazon-web-services - 当名称有效时,为什么 AWS boto3 调用 associate_iam_instance_profile 会导致 InvalidParameterValue 无效名称?
问题描述
我有调用 AWS boto3 API 的代码。这段代码使用 iam 客户端和 ec2 客户端做了几件事:
iam_client = boto3.client('iam')
ec2_client = boto3.client('ec2')
对于所有这些调用,角色名称和配置文件名称都是相同的:MyExampleName
- 调用
iam_client.create_instance_profile
以创建新的实例配置文件 - 调用
iam_client.create_role
以创建新角色 - 调用
iam_client.attach_role_policy
以将 AWS 托管策略附加到新角色 - 调用
iam_client.add_role_to_instance_profile
以将新角色添加到实例配置文件 - 调用
ec2_client.associate_iam_instance_profile
以将配置文件关联到实例
但是最后一次调用失败并显示如下消息:
信息“失败:调用 AssociateIamInstanceProfile 操作时发生错误 (InvalidParameterValue):参数 iamInstanceProfile.name 的值 (MyExampleName) 无效。IAM 实例配置文件名称无效,账户 ID:XXXXXXXXXXXXX,资源:EC2 实例:i-78sd976sd6912”
我刚刚创建了配置文件,那么对 associate_iam_instance_profile 的调用怎么会说它无效?
解决方案
使用 IAM 客户端和 EC2 客户端都存在一些时间问题。通过 . 创建的配置文件iam_client
可能无法立即由ec2_client
. 通过一些重试逻辑,该名称似乎最终会起作用——它并不是真的无效,只是还没有找到:
import time
logger.info('Attaching profile: MyExampleName'))
counter = 0
while counter < 60:
try:
ec2_client.associate_iam_instance_profile(IamInstanceProfile={'Name': 'MyExampleName'},
InstanceId='i-78sd976sd6912')
break
except ClientError as err:
if err.response['Error']['Code'] == 'InvalidParameterValue':
counter = counter + 1
logger.info('The ec2 client did not find the profile yet; wait 1 second and then try again')
time.sleep(1)
logger.info('Finally worked!')
以下日志显示了一个真实的日志条目示例,显示 EC2 客户端最终与 IAM 客户端一致:
INFO "Attaching profile: MyExampleName"
INFO "The profile is not found yet; wait 1 second and then try again"
INFO "The profile is not found yet; wait 1 second and then try again"
INFO "The profile is not found yet; wait 1 second and then try again"
INFO "The profile is not found yet; wait 1 second and then try again"
INFO "The profile is not found yet; wait 1 second and then try again"
INFO "The profile is not found yet; wait 1 second and then try again"
INFO "The profile is not found yet; wait 1 second and then try again"
INFO "The profile is not found yet; wait 1 second and then try again"
INFO "The profile is not found yet; wait 1 second and then try again"
INFO "Finally worked!"
推荐阅读
- c# - 将记录添加到多个 SQL 表的 ASP.NET C# 循环意外停止而没有错误
- c# - Wix 工具集引导程序显示选项以选择/选择要安装的 msi 包
- slider - 猫头鹰轮播有免费模式选项吗?像刷卡器
- regex - 使用 REEXTRACT 从 Google 表格中提取 £ 或 % 值
- arrays - 矩阵数组到多维 RGB 图像数组并使用 imresize 重塑图像
- python - 将带有 websockets 的 Quart 项目从 asyncio 迁移到 trio
- javascript - XHR 请求和下载禁止的解决方法
- reactjs - 剪切和不剪切 PropTypes 形状
- ios - 旋转设备时约束未更新且视图为信箱
- databricks - Azure Databricks 机密范围:Azure Key Vault 支持或 Databricks 支持