我在AWS组织上工作:目前正在AWS组织下创建scp策略,如下所示:
Python文件:
policies = config['policies']
for policy in policies:
try:
OUPolicy = client.create_policy(
Description=policy['description'],
Name= policy['Name'],
Content=policy['content'],
Type='SERVICE_CONTROL_POLICY'
)
YAML文件:
policies:
- Name: xyz
description: Service Control Policies for xyz
content:
Version: 2012-10-17
Statement:
- Effect: Allow
Resource: "*"
Action: "*"
- Effect: Deny
Resource: "*"
Action: "*
我验证了YAML模板,它的格式正确,但仍然出现如下错误:
Parameter validation failed:
Invalid type for parameter Content, value: {'Version': datetime.date(2012, 10, 17), 'Statement': [{'Effect': 'Allow', 'Resource': '*', 'Action': '*'}, {'Effect': 'Deny', 'Resource': '*', 'Action': '*'}]}, type: <class 'dict'>, valid types: <class 'str'>
答案 0 :(得分:0)
根据您显示的create_policy
的文档,
内容(字符串)-[必需]要添加到新的策略内容 政策。例如,如果您创建服务控制策略(SCP), 此字符串必须是JSON文本,用于指定 附加帐户中的管理员可以委派给其用户,组和 角色。
您需要将字典policy['content']
(已从YAML文档中解码)编码回JSON字符串。
您可以使用json.dumps:
import json
...
client.create_policy(
...
Content=json.dumps(policy['content']),
...
)
答案 1 :(得分:0)
答案:
policies = config['policies']
for policy in policies:
try:
OUPolicy = client.create_policy(
Description=policy['description'],
Name= policy['Name'],
Content=json.dumps(policy['content']),
Type='SERVICE_CONTROL_POLICY'
)