【问题标题】:How do I deserialize a cloudformation template yaml into a C# Dictionary of Strings?如何将 cloudformation 模板 yaml 反序列化为 C# 字符串字典?
【发布时间】:2020-04-29 20:21:45
【问题描述】:

我需要我的 csharp 应用程序来读取 cloudformation 模板 yaml 文件(带有 !Sub 和其他功能),以便将资源按原样复制到另一个具有不同资源名称的 cloudformation 模板 yaml 中。 YamlDotNet 在遇到 !Sub 和其他函数时无法反序列化。我应该使用另一个反序列化器吗?我可以将其软类型反序列化为字典字典,如字典 > 吗?然后我可以将整个小节作为一个字符串读取并复制,而不必解析它。

示例 cf.yaml:

Description: my cloudformation example

Parameters:
  MyParameter:
    Description: What do you want?
    Type: String
    Default: 'hello'
  Environment:
    Description: Where do you want?
    Type: String
    Default: 'world'

Conditions:
  # Enable Permission Boundary
  IsBoundaryEnabled: !Equals [!Ref MyParameter, 'hello']

Resources:

  MyPolicies:
    Type: AWS::IAM::Policy
    Properties:
      PolicyName: My-Policies
      PolicyDocument:
        Statement:
        -
          Effect: Allow
          Action:
          - ssm:GetParameter
          Resource:
          - !Sub 'arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/${Environment}.connectionString'
      Roles:
        - !Ref MyRole

  MyRole:
    Type: AWS::IAM::Role
    Properties:
      PermissionsBoundary: !If [ IsBoundaryEnabled, !Sub 'arn:aws:iam::${AWS::AccountId}:policy/My-Permission-Boundary', !Ref "AWS::NoValue"]
      AssumeRolePolicyDocument:
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action:
              - sts:AssumeRole
      Path: /

所以我只想将 mycf["Resources"]["MyPolicies"] 复制到另一个文件中

【问题讨论】:

  • 如果您提供更多背景信息,例如您尝试加载的文档示例,我或许可以为您提供帮助。
  • 添加了一个例子,谢谢!
  • @Chip 你找到解决方案了吗?我也有同样的问题...
  • 我最终改用 python 和 cfn_flip。如果您需要坚持使用 c#,也许您可​​以使用 cfn_flip 将 yaml 转换为 json,然后加载 json。这不是一个友好的设置,因为你会失去 cmets 等等。

标签: c# amazon-cloudformation yamldotnet


【解决方案1】:

阅读 CloudFormation 并不容易!有numerous difficulties involved

我最近发布了一个nuget package 来读写 CloudFormation,上面的链接是其文档的一部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-25
    • 2012-05-10
    • 2019-10-05
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    相关资源
    最近更新 更多