【问题标题】:Invalid parameter value error of YAML fileYAML 文件参数值无效错误
【发布时间】:2018-10-29 22:21:16
【问题描述】:

我在 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'>

【问题讨论】:

  • 看起来好像您没有根据要求将 YAML 文件的内容传递给 client.create_policy。从错误消息的最后一部分猜测,它需要一个字符串而不是字典。我建议您查看该函数的文档。
  • 我检查了很多次但无法识别错误..你能帮忙吗?我真的很感激
  • client.create_policy 的文档对Content 参数有什么看法?除非你告诉我们这个函数的确切来源,否则我们无法知道问题出在哪里。
  • 根据 aws boto3 doc : response = client.create_policy( Content='string', Description='string', Name='string', Type='SERVICE_CONTROL_POLICY' )
  • Content (string) -- [必需] 添加到新策略的策略内容。例如,如果您创建服务控制策略 (SCP),则此字符串必须是 JSON 文本,用于指定附加帐户中的管理员可以委派给其用户、组和角色的权限。 .

标签: python aws-sdk-js


【解决方案1】:

根据您显示的create_policy 的文档,

Content (string) -- [REQUIRED] 添加到新的策略内容 政策。例如,如果您创建服务控制策略 (SCP), 此字符串必须是 JSON 文本,用于指定权限 附加帐户中的管理员可以委托给他们的用户、组和 角色。

您需要将字典 policy['content'](您已从 YAML 文档中解码)编码回 JSON 字符串。

您可以使用json.dumps

import json

...

client.create_policy(
  ...
  Content=json.dumps(policy['content']),
  ...
)

【讨论】:

    【解决方案2】:

    答案:

    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'
        )

    【讨论】:

      猜你喜欢
      • 2015-09-21
      • 2014-11-23
      • 1970-01-01
      • 1970-01-01
      • 2021-01-30
      • 2019-12-23
      • 2016-08-17
      • 2016-01-05
      • 2020-10-16
      相关资源
      最近更新 更多