AWS Cloudformation CLI uses shorthand syntax when passing parameters as json file

demonicdaron

I am trying to run the following command:

aws cloudformation update-stack --stack-name $stack.Trim('"') --use-previous-template --parameters file://./parameters.json

This is the content of parameters.json (you can copy and paste here to check it is a valid JSON string):

[{"ParameterKey":"directoryName","UsePreviousValue":true},{"ParameterKey":"CodeDeployServiceRole","UsePreviousValue":true},{"ParameterKey":"ApplicationType","UsePreviousValue":true},{"ParameterKey":"EnvironmentCode","UsePreviousValue":true},{"ParameterKey":"InstanceRoleInstanceProfile","UsePreviousValue":true},{"ParameterKey":"VPCID","UsePreviousValue":true},{"ParameterKey":"SecurityGroupList","UsePreviousValue":true},{"ParameterKey":"KeyName","UsePreviousValue":true},{"ParameterKey":"Subnets","UsePreviousValue":true},{"ParameterKey":"dnsIpAddresses","UsePreviousValue":true},{"ParameterKey":"directoryId","UsePreviousValue":true},{"ParameterKey":"directoryBIOS","UsePreviousValue":true},{"ParameterKey":"ScalingTermination","UsePreviousValue":true},{"ParameterKey":"EC2ScaleUpCooldown","UsePreviousValue":true},{"ParameterKey":"AttributeDelayTimeout","UsePreviousValue":true},{"ParameterKey":"CwLowOperator","UsePreviousValue":true},{"ParameterKey":"EBSOptimized","UsePreviousValue":true},{"ParameterKey":"PatchingGroupTag","UsePreviousValue":true},{"ParameterKey":"CwHighOperator","UsePreviousValue":true},{"ParameterKey":"SSMInventoryTag","UsePreviousValue":true},{"ParameterKey":"CwHighEvaluations","UsePreviousValue":true},{"ParameterKey":"EC2ScaleUpAdjustment","UsePreviousValue":true},{"ParameterKey":"CwScalingMetric","UsePreviousValue":true},{"ParameterKey":"HealthyThresholdCount","UsePreviousValue":true},{"ParameterKey":"ImageId","ParameterValue":"ami-0b2e03736f1241bbb"},{"ParameterKey":"CodeDeployServiceRole","UsePreviousValue":true},{"ParameterKey":"directoryName","UsePreviousValue":true},{"ParameterKey":"HealthCheckIntervalSeconds","UsePreviousValue":true},{"ParameterKey":"ReleaseId","UsePreviousValue":true},{"ParameterKey":"UnhealthyThresholdCount","UsePreviousValue":true},{"ParameterKey":"EC2ScaleDownAdjustment","UsePreviousValue":true},{"ParameterKey":"ApplicationType","UsePreviousValue":true},{"ParameterKey":"EnvironmentCode","UsePreviousValue":true},{"ParameterKey":"Environment","UsePreviousValue":true},{"ParameterKey":"EbsVolumeType","UsePreviousValue":true},{"ParameterKey":"BackupsEnabled","UsePreviousValue":true},{"ParameterKey":"CwLowPeriod","UsePreviousValue":true},{"ParameterKey":"HealthCheckType","UsePreviousValue":true},{"ParameterKey":"InstanceRoleInstanceProfile","UsePreviousValue":true},{"ParameterKey":"ServiceLevel","UsePreviousValue":true},{"ParameterKey":"TerminatedInstances","UsePreviousValue":true},{"ParameterKey":"VPCID","UsePreviousValue":true},{"ParameterKey":"HealthCheckPath","UsePreviousValue":true},{"ParameterKey":"CwHighPeriod","UsePreviousValue":true},{"ParameterKey":"HealthCheckGracePeriod","UsePreviousValue":true},{"ParameterKey":"HealthCheckTimeoutSeconds","UsePreviousValue":true},{"ParameterKey":"CustomCWConfig","UsePreviousValue":true},{"ParameterKey":"ScalingMin","UsePreviousValue":true},{"ParameterKey":"SecurityGroupList","UsePreviousValue":true},{"ParameterKey":"CwLowEvaluations","UsePreviousValue":true},{"ParameterKey":"directoryBIOS","UsePreviousValue":true},{"ParameterKey":"CustomSSMStepCount","UsePreviousValue":true},{"ParameterKey":"ScalingMax","UsePreviousValue":true},{"ParameterKey":"InstanceType","UsePreviousValue":true},{"ParameterKey":"EncryptEBSVolume","UsePreviousValue":true},{"ParameterKey":"SSMRefreshFrequency","UsePreviousValue":true},{"ParameterKey":"CwHighThreshold","UsePreviousValue":true},{"ParameterKey":"CustomSSMSteps","UsePreviousValue":true},{"ParameterKey":"MinInstancesInService","UsePreviousValue":true},{"ParameterKey":"MatcherCode","UsePreviousValue":true},{"ParameterKey":"EbsVolumeSize","UsePreviousValue":true},{"ParameterKey":"EC2ScaleDownCooldown","UsePreviousValue":true},{"ParameterKey":"Iops","UsePreviousValue":true},{"ParameterKey":"KeyName","UsePreviousValue":true},{"ParameterKey":"Subnets","UsePreviousValue":true},{"ParameterKey":"dnsIpAddresses","UsePreviousValue":true},{"ParameterKey":"CWLogRetention","UsePreviousValue":true},{"ParameterKey":"EnableRackspaceTicket","UsePreviousValue":true},{"ParameterKey":"CwLowThreshold","UsePreviousValue":true},{"ParameterKey":"directoryId","UsePreviousValue":true},{"ParameterKey":"ScalingNotificationTopic","UsePreviousValue":true},{"ParameterKey":"LoadBalancerFullName","UsePreviousValue":true}]

I get this when I run the command:

aws : 
At line:1 char:1
+ aws cloudformation update-stack --stack-name $stack.Trim('"') --templ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Error parsing parameter '--parameters': Expected: '=', received: 'ÿ' for input:
ÿþ[ { " P a r a m e t e r K e y " : " d i r e c t o r y N a m e " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " C o d e D e p l o y S e r v i c e R o l e " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r
 a m e t e r K e y " : " A p p l i c a t i o n T y p e " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " E n v i r o n m e n t C o d e " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " 
: " I n s t a n c e R o l e I n s t a n c e P r o f i l e " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " V P C I D " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " S e c u r i t
 y G r o u p L i s t " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " K e y N a m e " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " S u b n e t s " , " U s e P r e v i o u s V a 
l u e " : t r u e } , { " P a r a m e t e r K e y " : " d n s I p A d d r e s s e s " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " d i r e c t o r y I d " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r
 a m e t e r K e y " : " d i r e c t o r y B I O S " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " S c a l i n g T e r m i n a t i o n " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y 
" : " E C 2 S c a l e U p C o o l d o w n " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " A t t r i b u t e D e l a y T i m e o u t " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " :
 " C w L o w O p e r a t o r " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " E B S O p t i m i z e d " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " P a t c h i n g G r o u p T 
a g " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " C w H i g h O p e r a t o r " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " S S M I n v e n t o r y T a g " , " U s e P r e v
 i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " C w H i g h E v a l u a t i o n s " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " E C 2 S c a l e U p A d j u s t m e n t " , " U s e P r e v i o u 
s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " C w S c a l i n g M e t r i c " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " H e a l t h y T h r e s h o l d C o u n t " , " U s e P r e v i o u s V a l u
 e " : t r u e } , { " P a r a m e t e r K e y " : " I m a g e I d " , " P a r a m e t e r V a l u e " : " a m i - 0 b 2 e 0 3 7 3 6 f 1 2 4 1 b b b " } , { " P a r a m e t e r K e y " : " C o d e D e p l o y S e r v i c e R o l e " , " U s e P r e v i o u 
s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " d i r e c t o r y N a m e " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " H e a l t h C h e c k I n t e r v a l S e c o n d s " , " U s e P r e v i o u s V
 a l u e " : t r u e } , { " P a r a m e t e r K e y " : " R e l e a s e I d " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " U n h e a l t h y T h r e s h o l d C o u n t " , " U s e P r e v i o u s V a l u e " : t r 
u e } , { " P a r a m e t e r K e y " : " E C 2 S c a l e D o w n A d j u s t m e n t " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " A p p l i c a t i o n T y p e " , " U s e P r e v i o u s V a l u e " : t r u e } ,
 { " P a r a m e t e r K e y " : " E n v i r o n m e n t C o d e " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " E n v i
 r o n m e n t " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " E b s V o l u m e T y p e " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " B a c k u p s E n a b l e d " , " U s e 
P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " C w L o w P e r i o d " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " H e a l t h C h e c k T y p e " , " U s e P r e v i o u s V a l u e " :
 t r u e } , { " P a r a m e t e r K e y " : " I n s t a n c e R o l e I n s t a n c e P r o f i l e " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " S e r v i c e L e v e l " , " U s e P r e v i o u s V a l u e " : t 
r u e } , { " P a r a m e t e r K e y " : " T e r m i n a t e d I n s t a n c e s " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " V P C I D " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K
 e y " : " H e a l t h C h e c k P a t h " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " C w H i g h P e r i o d " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " H e a l t h C h 
e c k G r a c e P e r i o d " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " H e a l t h C h e c k T i m e o u t S e c o n d s " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " C u
 s t o m C W C o n f i g " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " S c a l i n g M i n " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " S e c u r i t y G r o u p L i s t " 
, " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " C w L o w E v a l u a t i o n s " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " d i r e c t o r y B I O S " , " U s e P r e v i o u
 s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " C u s t o m S S M S t e p C o u n t " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " S c a l i n g M a x " , " U s e P r e v i o u s V a l u e " : t r u e 
} , { " P a r a m e t e r K e y " : " I n s t a n c e T y p e " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " E n c r y p t E B S V o l u m e " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r
 K e y " : " S S M R e f r e s h F r e q u e n c y " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " C w H i g h T h r e s h o l d " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " 
C u s t o m S S M S t e p s " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " M i n I n s t a n c e s I n S e r v i c e " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " M a t c h e
 r C o d e " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " E b s V o l u m e S i z e " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " E C 2 S c a l e D o w n C o o l d o w n " , 
" U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " I o p s " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " K e y N a m e " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r
 a m e t e r K e y " : " S u b n e t s " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " d n s I p A d d r e s s e s " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " C W L o g R e 
t e n t i o n " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " E n a b l e R a c k s p a c e T i c k e t " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " C w L o w
 T h r e s h o l d " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " d i r e c t o r y I d " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " S c a l i n g N o t i f i c a t i o n T 
o p i c " , " U s e P r e v i o u s V a l u e " : t r u e } , { " P a r a m e t e r K e y " : " L o a d B a l a n c e r F u l l N a m e " , " U s e P r e v i o u s V a l u e " : t r u e } ] 
^

I have run the command using --debug and noticed that it tries to parse the parameters using shorthand syntax:

2019-04-15 14:11:36,069 - MainThread - botocore.hooks - DEBUG - Event process-cli-arg.cloudformation.update-stack: calling handler <awscli.argprocess.ParamShorthandParser object at 0x0000020212160F28>
2019-04-15 14:11:36,070 - MainThread - awscli.argprocess - DEBUG - Parsing param --parameters as shorthand

I cannot find any way to force the CLI to parse the parameters using JSON syntax rather than shorthand. From the docs, it looks like it should do that automatically.

Any ideas why it tries to use the shorthand syntax?

demonicdaron

It looks like the issue was the encoding for parameters.json. I think the CLI could not parse parameters.json due to the encoding of the file, and was therefore trying to parse the parameters using the shorthand syntax rather than the JSON syntax.

I created the file using the following code:

$parameters = "{""ParameterKey"":""directoryName"",""UsePreviousValue"":true},{""ParameterKey"":""CodeDeployServiceRole"",""UsePreviousValue"":true},{""ParameterKey"":""ApplicationType"",""UsePreviousValue"":true},{""ParameterKey"":""EnvironmentCode"",""UsePreviousValue"":true},{""ParameterKey"":""InstanceRoleInstanceProfile"",""UsePreviousValue"":true},{""ParameterKey"":""VPCID"",""UsePreviousValue"":true},{""ParameterKey"":""SecurityGroupList"",""UsePreviousValue"":true},{""ParameterKey"":""KeyName"",""UsePreviousValue"":true},{""ParameterKey"":""Subnets"",""UsePreviousValue"":true},{""ParameterKey"":""dnsIpAddresses"",""UsePreviousValue"":true},{""ParameterKey"":""directoryId"",""UsePreviousValue"":true},{""ParameterKey"":""directoryBIOS"",""UsePreviousValue"":true},"

    # generate json file with parameters for stack
    foreach ($parameter in $stackJson.Stacks.Parameters){
        $parameterKey = $parameter.ParameterKey
        if ($parameterKey -eq "ImageId"){
            $parameters += "{""ParameterKey"":""$parameterKey"",""ParameterValue"":""$latestAmiId""},"
        }
        else {
            $parameters += "{""ParameterKey"":""$parameterKey"",""UsePreviousValue"":true},"            
        }        
    }

    $parameters = $parameters.TrimEnd(',')
    $parameters = "[" + $parameters + "]"
    #$parameters = ConvertFrom-Json $parameters
    $parameters | Out-File .\parameters.json

Adding -Encoding ascii to the Out-File command solved the issue.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Optional parameters when using AWS CLI to launch CloudFormation template

Passing a JSON object to ECS instance in AWS CloudFormation

Aws cli cloudformation error seen on passing parameter value of type CommaDelimitedList

AWS CLI: aws events put-targets --cli-input-json file://target.json giving JSON syntax error

BadRequestException in AWS CLI when trying to import a swagger.json file

Shorthand syntax for message-attributes in the send-message command in aws-cli for sqs

Passing a JSON file as environment variable to aws lambda update-function-configuration cli

AWS Cloudformation Metric With Parameters

Get JSON template of a cloudformation stack via AWS CLI

AWS CloudFormation fails to create template when it is deployed via CLI

CloudFormation passing parameters from CodePipeline

cloudformation passing parameters to ssm ducument

Can I use "Fn::Join" in "Parameters" of AWS Cloudformation json template

What should be inside "Parameters" and what in "Resoures" in AWS Cloudformation json template?

Passing parameters of type List<AWS::EC2::Subnet::Id> to nested CloudFormation template

Create CloudFormation stack from AWS Lambda function, passing API Gateway parameters

Passing option value to AWS CLI command from variable or file

AWS cloudformation spot instance parameters

Azure DevOps - AWS CloudFormation and parameters

Unable to pass AWS CLI commands in double quotes in json syntax

json syntax error when creating an AWS role

Passing multiple parameters from external file to cloudformation template and using values with ref

Typescript constructor shorthand when parameters are passed as object

Roll back error when updating cloudformation stack in the error posted occurs when using AWS CLI

Cloudformation CLI Parameters Using Deploy Command

Put Items using Json File in AWS DynamoDB using AWS CLI

passing parameter with space into AWS cli

override parameters in parameter file for cloudformation

AWS CloudFormation: Passing Values between Nested Stacks