0
votes

I cannot get any CloudFormation template to deploy or validate when using PowerShell, but using the exact template I have no issues doing it using AWS CLI or using the AWS Console.

Let's take a basic CloudFormation template, lets call it Test.template.

{
  "AWSTemplateFormatVersion" : "2010-09-09",
  "Description" : "Simple template.",
  "Parameters" : {    
    "KeyName" : {
      "Type" : "String", 
      "Description" : "Name of an existing EC2 KeyPair to enable SSH access to the web server"
    }
  }, 

  "Resources" : {
    "Ec2Instance" : {
      "Type" : "AWS::EC2::Instance", 
      "Properties" : {        
        "KeyName" : "test",        
      }
    }
  }
}

Nothing special, very basic. Ignoring that this would fail when you were to create the EC2 resource, this is a properly formatted JSON CloudFormation template.

Now, if I run this command using the AWS CLI, it comes back successful and outputs the paramaters:

aws cloudformation validate-template --template-body file://c:/temp/Test.template

Using the exact same file, if I run this in PowerShell I get an error:

Test-CFNTemplate -TemplateBody file://c:/temp/Test.template

Test-CFNTemplate : Template format error: unsupported structure. At line:1 char:1 + Test-CFNTemplate -TemplateBody file://c:/temp/Test.template + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (Amazon.PowerShe...NTemplateCmdlet:TestCFNTemplateCmdlet) [Test-CFNTemplate], InvalidOperationException + FullyQualifiedErrorId : Amazon.CloudFormation.AmazonCloudFormationException,Amazon.PowerShell.Cmdlets.CFN.TestCFNTemplateCmdlet

I also have no issues deploying this template using either the AWS Console or using aws cloudformation create-stack I just cannot figure out why I can't do it using PowerShell.

New-CFNStack is returning the same error above as well:

Template format error: unsupported structure.

I installed the latest version of the AWS PowerShell module and I'm running Powershell 5.1.14409.1012

Everything else I found regarding this error were from folks who had issues because they weren't using file:// in the TemplateBody but that doesn't appear to be the case here.

2

2 Answers

3
votes

Apparently -TemplateBody does not support local file URI. You first need to read the template into a variable, and use that like below.

$content = [IO.File]::ReadAllText("c:\test.template")
Test-CFNTemplate -TemplateBody $content

After doing that, it will now give you the expected output.

Test-CFNTemplate -TemplateBody $content

Capabilities       : {}
CapabilitiesReason : 
DeclaredTransforms : {}
Description        : Simple template.
Parameters         : {KeyName}
0
votes

It is too bad that -TemplateBody doesn't support local file URI. Luckily you can still specific they file inline.

Test-CFNTemplate -TemplateBody (get-content c:\test.template -Raw)

The command should return the expected result.

Test-CFNTemplate -TemplateBody (get-content c:\test.template -Raw)

Capabilities       : {}
CapabilitiesReason : 
DeclaredTransforms : {}
Description        : Simple template.
Parameters         : {KeyName}

I can't claim this is my solution as I did read it from this blog: https://matthewdavis111.com/aws/test-cloudformation-powershell/