0
votes

I've been banging my head all morning on trying to create a powershell script that will ultimately update an AWS stack. Everything is great right up to the point where I have to pass parameters to the cloudformation template.

One of the parameter values (ParameterKey=ZipFilePath) contains a /. But the script fails complaining that it was expecting a = but found a /. I've tried escaping the slash but then the API complains that it found the backslash instead of an equals. Where am I going wrong?

... <snip creating a zip file> ...

 $filename = ("TotalCommApi-" + $DateTime + ".zip")

aws s3 cp $filename ("s3://S3BucketName/TotalCommApi/" + $filename)

aws cloudformation update-stack --stack-name TotalCommApi-Dev --template-url https://s3-region.amazonaws.com/S3bucketName/TotalCommApi/TotalCommApiCFTemplate.json --parameters ParameterKey=S3BucketName,ParameterValue=S3BucketNameValue,UsePreviousValue=false ParameterKey=ZipFilePath,ParameterValue=("TotalCommApi/" + $filename) ,UsePreviousValue=false

cd C:\Projects\TotalCommApi\TotalComm_API

And here is the pertinent section from the CloudFormation Template:

    "Description": "An AWS Serverless Application that uses the ASP.NET Core framework running in Amazon Lambda.",
"Parameters": {
        "ZipFilePath": {
            "Type": "String",
            "Description": "Path to the zip file containing the Lambda Functions code to be published."
        },
        "S3BucketName": {
            "Type": "String",
            "Description": "Name of the S3 bucket where the ZipFile resides." 
        } 
},
"AWSTemplateFormatVersion": "2010-09-09",
"Outputs": {},
"Conditions": {},
"Resources": {
    "ProxyFunction": {
        "Type": "AWS::Lambda::Function",
        "Properties": {
            "Code": {
                "S3Bucket": {"Ref": "S3BucketName" },
                "S3Key": { "Ref": "ZipFilePath" }
            },

And this is the error message generated by PowerShell ISE

[image removed]

Update: I am using Windows 7 which comes with Powershell 2. I updgraded to Powershell 4. Then my script yielded this error:

AWS CLI Error

On recommendation from a consulting firm, I uninstalled the CLI that I installed via msi, then I upgraded Python to 3.6.2 and then re-installed the CLI via pip. I still get the same error. I "echo"d the command to the screen and this is what I see:

upload: .\TotalCommApi-201806110722.zip to s3://S3bucketName/TotalCommApi/TotalCommApi-201806110722.zip
aws
cloudformation
update-stack
--stack-name
TotalCommApi-Dev
--template-url
https://s3-us-west-2.amazonaws.com/s3BucketName/TotalCommApi/TotalCommApiCFTemplate.json
--parameters
ParameterKey=S3BucketName
UsePreviousValue=true
ParameterKey=ZipFilePath
ParameterValue=TotalCommApi/TotalCommApi-201806110722.zip
1
This is really hard to debug without an example of the template. Can you share? - John Nicely
@JohnNicely I added the pertinent part from the CloudFormation template above. Note that the template works as expected when I use the console to update the stack. I'm able to supply the same values in the console as I do in the script and the console updates the stack successfully. Thanks for responding. - Dar
@JohnNicely I have discovered that you have to enclose the parameterlist with double quotes ore PowerShell will strip out the commas. I thought that would solve my problem but not it complains that I'm not providing a value to the ZipFileName parameter. I can't seem to win. I'll update the original post accordingly. - Dar
Solution: stop using Windows! /s - John Nicely
All joking aside, you might want to give the Bash subshell a shot and see if you can get that to work - Bash is what I was using when I tried to reproduce your issue and I didn’t have any issues (aside from the one I noted in my answer). - John Nicely

1 Answers

0
votes

Sorry for the delay getting back to you on this - the good news is that I might have a hint about what your issue is.

ParameterKey=ZipFilePath,ParameterValue=("TotalCommApi/" + $filename) ,UsePreviousValue=false

I was driving myself mad trying to reproduce this issue. Why? Because I assumed that the space after ("TotalCommApi/" + $filename) was an artifact from copying, not the actual value that you were using. When I added the space in:

aws cloudformation update-stack --stack-name test --template-url https://s3.amazonaws.com/test-bucket-06-09/test.template --parameters ParameterKey=S3BucketName,ParameterValue=$bucketname,UsePreviousValue=false ParameterKey=ZipFilePath,ParameterValue=testfolder/$filename ,UsePreviousValue=false
Error parsing parameter '--parameters': Expected: '=', received: ','

This isn't exactly your error message (, instead of /), but I think it's probably a similar issue in your case - check to make sure the values that are being used in your command don't have extra spaces somewhere.