我正在创建AWS IAM托管策略的列表。当我尝试给出detach命令时,出现错误消息:
Invalid length for parameter PolicyArn, value: 1, valid range: 20-inf
我认为我收到此错误的原因是引号干扰了detach命令。
这是我要提供给命令的列表:
['arn:aws:iam::aws:policy/AmazonAppStreamFullAccess', 'arn:aws:iam::aws:policy/AmazonAPIGatewayAdministrator']
我正在尝试使用以下命令从列表中去除括号和单引号:
managed_policies_list = str(managed_policies_list).replace('[','').replace(']','').replace('\'','')
我在使用remove policy命令时遇到了麻烦,因为引号没有从列表中删除。这是strip命令后的managed_policies_list外观:
'arn:aws:iam::aws:policy/AmazonAppStreamFullAccess, arn:aws:iam::aws:policy/AmazonAPIGatewayAdministrator'
当我运行以下代码尝试从用户中删除策略时:
managed_user_policies = (iam_client.list_attached_user_policies(UserName=user_name))
tree = objectpath.Tree(managed_user_policies)
managed_policies_list = set(tree.execute('$..AttachedPolicies[\'PolicyArn\']'))
managed_policies_list = list(managed_policies_list)
managed_policies_list = str(managed_policies_list).replace('[','').replace(']','').replace('\'','')
for policy_arn in managed_policies_list:
print(f"Removing: {policy_arn} from User: {user_name}.")
detach_user_policy_response = (iam_client.detach_user_policy(UserName=user_name,PolicyArn=\'policy_arn\'))
出现此错误是因为引号没有被删除
'arn:aws:iam::aws:policy/AlexaForBusinessReadOnlyAccess'
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
File "C:\Users\tdunphy\AppData\Local\Programs\Python\Python37-32\lib\site-packages\botocore\client.py", line 357, in _api_call
return self._make_api_call(operation_name, kwargs)
File "C:\Users\tdunphy\AppData\Local\Programs\Python\Python37-32\lib\site-packages\botocore\client.py", line 634, in _make_api_call
api_params, operation_model, context=request_context)
File "C:\Users\tdunphy\AppData\Local\Programs\Python\Python37-32\lib\site-packages\botocore\client.py", line 682, in _convert_to_request_dict
api_params, operation_model)
File "C:\Users\tdunphy\AppData\Local\Programs\Python\Python37-32\lib\site-packages\botocore\validate.py", line 297, in serialize_to_request
raise ParamValidationError(report=report.generate_report())
botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid length for parameter PolicyArn, value: 1, valid range: 20-inf
如何正确执行此操作?
答案 0 :(得分:1)
据我所知,命令中没有括号,也没有单/双引号。 您看到的内容不属于命令(字符串)的一部分:
[ ]
)表示它是一个列表/数组。'
"
)表示它是一个字符串。这些是Python解释器的常规指示符,可帮助您查看所使用的数据类型(字符串,整数,字典等)。
请参阅:
riverman@ubuntu:~$ python3
Python 3.7.3 (default, Apr 3 2019, 05:39:12)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
aws_iam_mp_list = ['arn:aws:iam::aws:policy/AmazonAppStreamFullAccess', 'arn:aws:iam::aws:policy/AmazonAPIGatewayAdministrator']
aws_iam_mp_list # Typing a variable's name into the interpreter prints their content
['arn:aws:iam::aws:policy/AmazonAppStreamFullAccess', 'arn:aws:iam::aws:policy/AmazonAPIGatewayAdministrator']
for managed_policy in aws_iam_mp_list:
print(managed_policy)
arn:aws:iam::aws:policy/AmazonAppStreamFullAccess
arn:aws:iam::aws:policy/AmazonAPIGatewayAdministrator
以编程方式使用结果时,这些指示符将不会成为代码的一部分。
我对你的理解正确吗?