rubyamazon-web-servicesaws-cloudformationaws-sdk-ruby

AWS Ruby SDK Cloudformation will not validate a template


running into a ruby Cloudformation problem I have a super simple cloudformation template is will validate and create_stack with AWS CLI, but failing when I run either of the same commands with the the 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>

It does not matter if I use file:// or not in the template_body field. I can make the Ruby work with the same template if I upload it to S3, but I do not want to have to do that. Any one hit this problem before?


Solution

  • As per the documentation, the value for template_body should be a string

    template_body (String) — Structure containing the template body with a minimum length of 1 byte and a maximum length of 51,200 bytes

    so you just have to read the file and pass it as a string to the option.

    
    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>