【问题标题】:How to debug cloudformation template? Syntax errors in policy. YAML file如何调试cloudformation模板?策略中的语法错误。 YAML 文件
【发布时间】:2019-01-29 15:55:00
【问题描述】:

部署堆栈时出现的错误:

Syntax errors in policy. (Service: AmazonIdentityManagement; Status Code: 400; Error Code: MalformedPolicyDocument; Request ID: 

这是我的角色政策导致错误:

  roleEc2:
Type: AWS::IAM::Role
Properties:
  AssumeRolePolicyDocument:
    Version: "2012-10-17"
    Statement:
      -
        Effect: Allow
        Principal:
          Service:
            - 'ec2.amazonaws.com'
        Action:
            - 'sts:AssumeRole'
  Path: '/'
  Policies:
    -
      PolicyName: 'bucket-access'
      PolicyDocument:
        Version: '2012-10-17'
        Id: 'BucketPolicy'
        Statement:
        - Effect: Allow
          Action:
          - s3:ListBucket
          - s3:GetObject
          - s3:GetBucketLocation
          Resource:
          - arn:aws:s3:::code-dir
          - arn:aws:s3:::code-dir/*
          Principal: !Ref BucketPrincipal

而且我不知道怎么调试,不知道怎么理解哪里出错了,什么行号。

【问题讨论】:

  • 文件的根级别是一个映射。其中第一个键缩进两个空格,第二个键缩进零个空格。那是无效的,它们都必须缩进相同的数量。如果删除 roleEc2 之前的空格,并假设 !Ref 的构造函数可用,则这是有效的 YAML。

标签: amazon-web-services configuration amazon-cloudformation amazon-iam


【解决方案1】:

(我知道这是一年后,但仍然......)我喜欢使用的一个好工具是 cfn-lint:https://github.com/aws-cloudformation/cfn-python-lint 对您的模板运行它,它会告诉您您做错了什么,在什么方面行:

代码中的问题:没有前四行,IAM Policy 中不能有委托人(最后一行)。

---
AWSTemplateFormatVersion: '2010-09-09'
Description: AoD CloudFormation Template Detective Controls
Resources:
  roleEc2:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          -
            Effect: Allow
            Principal:
              Service:
                - 'ec2.amazonaws.com'
            Action:
                - 'sts:AssumeRole'
      Path: '/'
      Policies:
        -
          PolicyName: 'bucket-access'
          PolicyDocument:
            Version: '2012-10-17'
            Id: 'BucketPolicy'
            Statement:
            - Effect: Allow
              Action:
              - s3:ListBucket
              - s3:GetObject
              - s3:GetBucketLocation
              Resource:
              - arn:aws:s3:::code-dir
              - arn:aws:s3:::code-dir/*

【讨论】:

    猜你喜欢
    • 2018-09-16
    • 2021-07-31
    • 2020-04-24
    • 2021-01-01
    • 2017-04-05
    • 2020-03-24
    • 2017-11-26
    • 2023-02-04
    • 2020-09-13
    相关资源
    最近更新 更多