question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

client.validate_template return error while aws cloudformation validate-template doesn't

See original GitHub issue

python --version Python 2.7.11 :: Anaconda 2.5.0 (64-bit)

print boto3.version 1.3.0

I’m seeing a problem where boto3 client.validate_template returns ValidationError consistently on any template I tried on; however, aws cloudformation validate-template reads the same templates ok.

In my test.py:

        cfclient = boto3.client('cloudformation')
       response = cfclient.validate_template(TemplateBody='file://mytemplate.json')

and this would return: Traceback (most recent call last): File “./test.py”, line 207, in <module> response = cfclient.validate_template(TemplateBody=‘file:/mytemplate.json’) File “/root/anaconda2/lib/python2.7/site-packages/botocore/client.py”, line 228, in _api_call return self._make_api_call(operation_name, kwargs) File “/root/anaconda2/lib/python2.7/site-packages/botocore/client.py”, line 492, in _make_api_call raise ClientError(parsed_response, operation_name) botocore.exceptions.ClientError: An error occurred (ValidationError) when calling the ValidateTemplate operation: Template format error: JSON not well-formed. (line 1, column 5) NOTE: the error always points to line 1, column 5.

When I run "aws cloudformation validate-template --template-body file://mytemplate.json. It appears to be okay. { “Description”: “Template to launch an instance”, “Parameters”: [] }

I have tried giving the TemplateBody absolute path (file:///root/development/mytemplate.json) or relative path (file://mytemplate.json) and the problem still persists. This lead me to believe that the cause is something else.

My ultimate goal being able to create a stack with the following code with Boto3: response = cfclient.create_stack(StackName='my-stack', TemplateBody=tdmctemplate)

Currently, this statement will fail on the same error.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
kyleknapcommented, Apr 11, 2016

I think this a similar solution. You cannot provide the filename, you need to open the file and pass its contents to the parameter. So something like:

cf = boto3.client('cloudformation')
with open('mytemplate.json', 'r') as f:
    resp = cf.create_stack(StackName=stack_name, TemplateBody=f.read())
6reactions
kyleknapcommented, Apr 11, 2016

So you cannot use the file:// from the CLI. We only have this in the CLI because there is not a good way of getting the contents of the file from the command line. For boto3, you will have to open a file yourself and read its contents. So something like this:

cfclient = boto3.client('cloudformation')
with open('mytemplate.json', 'r') as f:
    response = cfclient.validate_template(TemplateBody=f.read())

Let us know if that helps.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolve template validation or template format errors in ... - AWS
Validate your JSON or YAML templates with the AWS CloudFormation linter ... These templates return the following error: "Unresolved resource ...
Read more >
How do I resolve template validation or template format errors ...
How do I resolve template validation or template format errors in AWS CloudFormation ? 5K views 1 year ago. Amazon ...
Read more >
Aws cloudformation validate-template keeps giving error ...
The template you posted is valid but there's a syntax error in the command and it's not communicating what the problem is. Try...
Read more >
Validating a template - Amazon CloudFormation - 亚马逊云科技
Learn about validating an Amazon CloudFormation template. ... produces a validation error when running the aws cloudformation validate-template command.
Read more >
How to validate CloudFormation template with AWS CLI
aws cloudformation validate-template --template-body file://stack.json ... A client error (ValidationError) occurred when calling the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found