0

遇到 ruby​​ Cloudformation 问题我有一个超级简单的 cloudformation 模板,它将使用 AWS CLI 验证和 create_stack,但是当我使用 Ruby SDK 运行任何一个相同的命令时失败。

$ cat net.yml
AWSTemplateFormatVersion: '2010-09-09'

Resources:
  Vpc:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/16

$ aws cloudformation validate-template --region us-east-1 --template-body file://./net.yml
{
    "Parameters": []
}
$ irb
irb(main):001:0> require 'aws-sdk-cloudformation'
=> true
irb(main):002:1* cfn = Aws::CloudFormation::Client.new(
irb(main):003:1*   region: 'us-east-1'
irb(main):004:0> )
irb(main):005:2* cfn.validate_template({
irb(main):006:2*     template_body: 'file://./net.yml'
irb(main):007:0> })
Traceback (most recent call last):
       13: from /usr/bin/irb:23:in `<main>'
       12: from /usr/bin/irb:23:in `load'
       11: from /usr/lib/ruby/gems/2.7.0/gems/irb-1.2.1/exe/irb:11:in `<top (required)>'
       10: from (irb):5
        9: from /var/lib/gems/2.7.0/gems/aws-sdk-cloudformation-1.47.0/lib/aws-sdk-cloudformation/client.rb:5361:in `validate_template'
        8: from /var/lib/gems/2.7.0/gems/aws-sdk-core-3.112.0/lib/seahorse/client/request.rb:72:in `send_request'
        7: from /var/lib/gems/2.7.0/gems/aws-sdk-core-3.112.0/lib/seahorse/client/plugins/response_target.rb:24:in `call'
        6: from /var/lib/gems/2.7.0/gems/aws-sdk-core-3.112.0/lib/aws-sdk-core/plugins/response_paging.rb:12:in `call'
        5: from /var/lib/gems/2.7.0/gems/aws-sdk-core-3.112.0/lib/seahorse/client/plugins/request_callback.rb:71:in `call'
        4: from /var/lib/gems/2.7.0/gems/aws-sdk-core-3.112.0/lib/aws-sdk-core/plugins/param_converter.rb:26:in `call'
        3: from /var/lib/gems/2.7.0/gems/aws-sdk-core-3.112.0/lib/aws-sdk-core/plugins/idempotency_token.rb:19:in `call'
        2: from /var/lib/gems/2.7.0/gems/aws-sdk-core-3.112.0/lib/aws-sdk-core/plugins/jsonvalue_converter.rb:22:in `call'
        1: from /var/lib/gems/2.7.0/gems/aws-sdk-core-3.112.0/lib/seahorse/client/plugins/raise_response_errors.rb:17:in `call'
Aws::CloudFormation::Errors::ValidationError (Template format error: unsupported structure.)
irb(main):008:0>

我是否file://template_body现场使用并不重要。如果我将它上传到 S3,我可以让 Ruby 使用相同的模板,但我不想这样做。以前有人遇到过这个问题吗?

4

1 回答 1

1

根据文档,值template_body应该是一个字符串

template_body (String) — 包含模板主体的结构,最小长度为 1 字节,最大长度为 51,200 字节

所以你只需要读取文件并将其作为字符串传递给选项。


irb(main):001:0> File.read('x.yml')
=> "Resources:\n        myvpc:\n                Type: AWS::EC2::VPC\n                
Properties: \n                        CidrBlock: String\n"
irb(main):002:0>


irb(main):038:0> cfn.validate_template({template_body: File.read('x.yml').to_s})
=> #<struct Aws::CloudFormation::Types::ValidateTemplateOutput parameters=[],
 description=nil, capabilities=[], capabilities_reason=nil, declared_transforms=[]>
irb(main):039:0>
于 2021-02-13T22:19:05.897 回答