1

I need my csharp app to read a cloudformation template yaml file (with !Sub and other functions) in order to copy the resource, as-is, into another cloudformation template yaml with a different resource name. The YamlDotNet fails to deserialize when it hits the !Sub and other functions. Is there another deserializer I should use? Can I soft-type deserialize it into a Dictionary of Dictionaries like Dictionary <String, Dictionary<String, String>>? Then I could just read the whole subsection as a String to copy over and not have to parse it.

Example 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: /

So I just want to copy mycf["Resources"]["MyPolicies"] to another file

Chip
  • 1,439
  • 3
  • 15
  • 29
  • If you provide more context, such as an example of the document that you are trying to load, I may be able to help you. – Antoine Aubry May 28 '20 at 20:17
  • Added an example, thanks! – Chip Jun 23 '20 at 01:51
  • @Chip Were you able to find a solution? I have the same problem... – Maordav Apr 01 '21 at 10:03
  • I ended up switching to using python and cfn_flip instead. Maybe you could use cfn_flip to convert the yaml to json then load json if you need to stick with c#. It's not a friendly setup as you'd lose comments among other things. – Chip Apr 03 '21 at 19:12

1 Answers1

1

Reading CloudFormation is not easy! There are numerous difficulties involved.

I have recently released a nuget package to read and write CloudFormation of which the above link is part of the documentation.