cloudformation windows userdata cfn-init.exe 行尾问题

cloudformation windows userdata cfn-init.exe end of line issues

正在通过添加一些 cfn-init 来从 S3 存储桶请求数据。 我相信 powershell 的 cfn-init.exe 调用存在语法问题,但似乎找不到位置。此结构取自 Bootstrapping AWS CloudFormation Windows Stacks AWS Example. I've also tried adapting from the bash structure from AWS cfn-init documentation 但没有成功。

              "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"
                        }
                    }
                }
            }

cfn-init.exe 正在 运行 但由于参数传递到新行而出错:

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

这是因为您在顶部使用\n 加入了。如果您在同一行键入一些参数,连接函数的每个参数都将由换行事件分隔! 因此,您的命令 cfn-init 已被解释为:

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

由于断线,命令无法正确获取 运行。 因此,您可以通过 space 字符加入。您可以尝试用这个替换上面的内容:

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