【问题标题】:Converting Yaml cloudformation template to Json将 Yaml cloudformation 模板转换为 Json
【发布时间】:2019-08-13 08:21:45
【问题描述】:

我不知道如何将 yaml cloudformation 模板的以下部分转换为 json 模板。任何人都可以在这里指导我。

Tags: 
- Key: Name 
  Value: !Join 
        - '' 
        - - !FindInMap  
              - BusinessUnitCode 
              - !Ref BusinessUnit 
              - Code 
          - A 
          - !FindInMap  
              - AWSRegionCode 
              - !Ref 'AWS::Region' 
              - Code 
          - '-' 
          - !FindInMap  
              - AccountsCode 
              - !Ref Account 
              - Code 
          - '-' 
          - !FindInMap  
              - ProdLevelCode 
              - !Ref ProductionLevel 
              - Code 
          - '-' 
          - !FindInMap  
              - ServerFunctionCode 
              - !Ref ServerFunction 
              - Code 
          - '-' 
          - !FindInMap  
              - SGFunctionCode 
              - !Ref SecurityGroupFunction 
              - Code 
          - '-'                   
          - !Ref IncrementCode

虽然我知道没有必要进行这种转换,因为 json 和 yaml 在 cloudformation 中的工作方式相同,但必须按照规定进行。

【问题讨论】:

    标签: amazon-cloudformation


    【解决方案1】:

    AWS 控制台可以帮助您。 在template designer 中打开您的 CloudFormation 模板。

    CloudFormation > 选择您的堆栈 > 模板 > 在设计器中查看

    然后,您可以通过将模板语言选择为 Json 来轻松转换模板。

    【讨论】:

    【解决方案2】:

    如果您想要一个命令行解决方案来从 JSON 转换为 YAML,反之亦然,请考虑 aws-cfn-template-flip 项目。

    安装库:

    ▶ pip install cfn_flip
    

    用法:

    ▶ cfn-flip 
    Usage: cfn-flip [OPTIONS] [INPUT] [OUTPUT]
    
      AWS CloudFormation Template Flip is a tool that converts AWS
      CloudFormation templates between JSON and YAML formats, making use of the
      YAML format's short function syntax where possible.
    
    Options:
      -i, --input [json|yaml]   Specify the input format. Overrides -j and -y
                                flags.
      -o, --output [json|yaml]  Specify the output format. Overrides -j, -y, and
                                -n flags.
      -j, --json                Convert to JSON. Assume the input is YAML.
      -y, --yaml                Convert to YAML. Assume the input is JSON.
      -c, --clean               Performs some opinionated cleanup on your
                                template.
      -l, --long                Use long-form syntax for functions when converting
                                to YAML.
      -n, --no-flip             Perform other operations but do not flip the
                                output format.
      --version                 Show the version and exit.
      --help                    Show this message and exit.
    

    如果您在 YAML 中有 CloudFormation 模板,则可以使用以下方法将其翻转为 JSON:

    ▶ cfn-flip -j cloudformation.yml | jq . > cloudformation.json
    

    JQ 的使用是可选的,只是为了清理格式。

    【讨论】:

      【解决方案3】:

      我使用它,它就像一个魅力:https://cfnflip.com/

      您可以将 JSON 转换为 YAML 并将 YAML 转换为 JSON。

      无论是复制/粘贴,都会检测并翻转。如果您也想将其设置为您自己的内部,则可以使用开源和源代码。 (不,这不是我的,也不是赞助的)。

      这不仅仅是一个普通的翻转工具,它特别适用于 CloudFormation。

      【讨论】:

      • 虽然链接中的内容可能会回答问题,但请从链接中获取相关信息并将其包含在您的答案中。
      • 希望更好。
      猜你喜欢
      • 1970-01-01
      • 2019-09-23
      • 2021-10-30
      • 2018-04-14
      • 1970-01-01
      • 2020-12-16
      • 1970-01-01
      • 1970-01-01
      • 2016-02-16
      相关资源
      最近更新 更多