0
votes

Working through adding some cfn-init to request data from an S3 bucket. I believe I've got a syntax problem with the cfn-init.exe call from powershell but cannot seem to find where. This structure was taken from the Bootstrapping AWS CloudFormation Windows Stacks AWS Example. I've also tried adapting from the bash structure from AWS cfn-init documentation with no success.

              "UserData": {"Fn::Base64": {"Fn::Join": ["\n", [
                "<powershell>",
                ...
                "cfn-init.exe -v -s", { "Ref" : "AWS::StackName" },
                " -r EC2Instance",
                "</powershell>"

        "Metadata" : {
            "AWS::CloudFormation::Init" : {
                "config": {
                    "files" : {
                        "C:\\chef\\validator.pem" : {
                            "source" : "https://s3.amazonaws.com/dtcfstorage/validator.pem",
                            "authentication" : "s3creds"
                        }
                    }
                },
                    "AWS::CloudFormation::Authentication" : {
                        "s3creds" : {
                            "type" : "S3",
                            "roleName" : "awss3chefkeyaccess"
                        }
                    }
                }
            }

The cfn-init.exe is being run but errors out as the arguments are passing to new lines:

2018/05/21 15:35:08Z: Message: The errors from user scripts: Usage: cfn-init.exe [options] or: cfn-init.exe [options] or: cat | cfn-init.exe [options] -

cfn-init.exe: error: -s option requires an argument cloudinittest : The term 'cloudinittest' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\Windows\TEMP\UserScript.ps1:30 char:1 + cloudinittest + ~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (cloudinittest:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

-r : The term '-r' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\Windows\TEMP\UserScript.ps1:31 char:2 + -r EC2Instance + ~~ + CategoryInfo : ObjectNotFound: (-r:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

1
Should also add this is on Windows Server 2016 base AMI from AWS. I've also tried the same code on 2012 R2 AMI from AWS with like results. - d_turner

1 Answers

0
votes

It's because you have joined using \n at the top. Every arg to the join function will separate by a newline event if you type some on the same line! Therefore, your command cfn-init has been interpreted as:

cfn-init.exe -v -s
stack-name
 -r EC2Instance
...

Since the line is broken, the command doesn't get run properly. As such, you can join by a space character. You can try replacing the above by this:

{"Fn::Join": [" ", ["cfn-init.exe -v -s", {"Ref":"AWS::StackName"},
              "-r EC2Instance"]}