我读了一些资料,并了解到如果您希望在 Ubuntu 中使用 launchConfig,您将需要自己安装 cfn-init 文件,我已经完成了:
"Properties" : {
"KeyName" : { "Ref" : "KeyName" },
"SpotPrice" : "0.05",
"ImageId" : { "Fn::FindInMap" : [ "AWSRegionArch2AMI", { "Ref" : "AWS::Region" },
{ "Fn::FindInMap" : [ "AWSInstanceType2Arch", { "Ref" : "InstanceType" },
"Arch" ] } ] },
"SecurityGroups" : [ { "Ref" : "InstanceSecurityGroup" } ],
"InstanceType" : { "Ref" : "InstanceType" },
"UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
"#!/bin/bash\n",
"apt-get -y install python-setuptools\n",
"easy_install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-1.0-6.tar.gz\n",
"cfn-init ",
" --stack ", { "Ref" : "AWS::StackName" },
" --resource LaunchConfig ",
" --configset ALL",
" --access-key ", { "Ref" : "WorkerKeys" },
" --secret-key ", {"Fn::GetAtt": ["WorkerKeys", "SecretAccessKey"]},
" --region ", { "Ref" : "AWS::Region" }, " || error_exit 'Failed to run cfn-init'\n"
]]}}
但是这个设置有问题,我似乎无法得到一个合适的答案。我在日志中不断收到此错误:
Jun 15 12:02:34 ip-0 [CLOUDINIT] __init__.py[DEBUG]: config-scripts-per-once already ran once
Jun 15 12:02:34 ip-0 [CLOUDINIT] __init__.py[DEBUG]: handling scripts-per-boot with freq=None and args=[]
Jun 15 12:02:34 ip-0 [CLOUDINIT] __init__.py[DEBUG]: handling scripts-per-instance with freq=None and args=[]
Jun 15 12:02:34 ip-0 [CLOUDINIT] __init__.py[DEBUG]: handling scripts-user with freq=None and args=[]
Jun 15 12:02:34 ip-0 [CLOUDINIT] cc_scripts_user.py[WARNING]: failed to run-parts in /var/lib/cloud/instance/scripts
Jun 15 12:02:34 ip-0 [CLOUDINIT] __init__.py[WARNING]: Traceback (most recent call last):#012 File "/usr/lib/python2.7/dist-packages/cloudinit/CloudConfig/__init__.py", line 117, in run_cc_modules#012 cc.handle(name, run_args, freq=freq)#012 File "/usr/lib/python2.7/dist-packages/cloudinit/CloudConfig/__init__.py", line 78, in handle#012 [name, self.cfg, self.cloud, cloudinit.log, args])#012 File "/usr/lib/python2.7/dist-packages/cloudinit/__init__.py", line 326, in sem_and_run#012 func(*args)#012 File "/usr/lib/python2.7/dist-packages/cloudinit/CloudConfig/cc_scripts_user.py", line 31, in handle#012 util.runparts(runparts_path)#012 File "/usr/lib/python2.7/dist-packages/cloudinit/util.py", line 223, in runparts#012 raise RuntimeError('runparts: %i failures' % failed)#012RuntimeError: runparts: 1 failures
Jun 15 12:02:34 ip-0 [CLOUDINIT] __init__.py[ERROR]: config handling of scripts-user, None, [] failed
Jun 15 12:02:34 ip-0 [CLOUDINIT] __init__.py[DEBUG]: handling keys-to-console with freq=None and args=[]
Jun 15 12:02:34 ip-0 [CLOUDINIT] __init__.py[DEBUG]: handling phone-home with freq=None and args=[]
Jun 15 12:02:34 ip-0 [CLOUDINIT] __init__.py[DEBUG]: handling final-message with freq=None and args=[]
Jun 15 12:02:34 ip-0 [CLOUDINIT] cloud-init-cfg[ERROR]: errors running cloud_config [final]: ['scripts-user']
我完全不知道 scripts-user 是什么意思,Google 也帮不上什么忙。当我通过 ssh 进入服务器时,我可以看到它运行了 userdata 脚本,因为我可以以命令的形式访问 cfn-init,而在实例所基于的原始 AMI 中却无法访问。
但是我有一个 launchConfig:
"Comment" : "Install a simple PHP application",
"AWS::CloudFormation::Init" : {
"configSets" : {
"ALL" : ["WorkerRole"]
},
"WorkerRole" : {
"files" : {
"/etc/cron.d/worker.cron" : {
"content" : "*/1 * * * * ubuntu /home/ubuntu/worker_cron.php &> /home/ubuntu/worker.log\n",
"mode" : "000644",
"owner" : "root",
"group" : "root"
},
"/home/ubuntu/worker_cron.php" : {
"content" : { "Fn::Join" : ["", [
"#!/usr/bin/env php",
"<?php",
"define('ROOT', dirname(__FILE__));",
"const AWS_KEY = \"", { "Ref" : "WorkerKeys" }, "\";",
"const AWS_SECRET = \"", { "Fn::GetAtt": ["WorkerKeys", "SecretAccessKey"]}, "\";",
"const QUEUE = \"", { "Ref" : "InputQueue" }, "\";",
"exec('git clone x '.ROOT.'/worker');",
"if(!file_exists(ROOT.'/worker/worker_despatcher.php')){",
"echo 'git not downloaded right';",
"exit();",
"}",
"echo 'git downloaded';",
"include_once ROOT.'/worker/worker_despatcher.php';"
]]},
"mode" : "000755",
"owner" : "ubuntu",
"group" : "ubuntu"
}
}
}
}
似乎根本无法运行。我检查了主目录中是否存在该文件,但该文件不存在。我检查了 cronjob 条目,但它也不存在。阅读完文档后,我似乎无法看出我的代码可能存在什么问题。
您对为什么这不起作用有什么想法吗?我是否忽略了一些明显的东西?
答案1
好的,解决了。
我随机决定,为了更好地调试,我将最小实例数设为 1。这引发了有关我的 launchConfig 的错误。经过一番搜索,我发现我的模板犯了一个非常大的错误。所以我将 launchConfig 更改为:
"LaunchConfig" : {
"Type" : "AWS::AutoScaling::LaunchConfiguration",
"Metadata" : {
"Comment" : "Install a simple PHP application",
"AWS::CloudFormation::Init" : {
"config" : {
"files" : {
"/etc/cron.d/worker" : {
"content" : "*/1 * * * * ubuntu /home/ubuntu/worker_cronjob &> /home/ubuntu/worker.log\n",
"mode" : "000644",
"owner" : "root",
"group" : "root"
},
"/home/ubuntu/worker_cronjob" : {
"content" : { "Fn::Join" : ["", [
"#!/usr/bin/env php\n",
"<?php\n",
"define('ROOT', dirname(__FILE__));",
"const AWS_KEY = \"", { "Ref" : "WorkerKeys" }, "\";",
"const AWS_SECRET = \"", { "Fn::GetAtt": ["WorkerKeys", "SecretAccessKey"]}, "\";",
"const QUEUE = \"", { "Ref" : "InputQueue" }, "\";",
"exec('git clone x '.ROOT.'/worker');",
"if(!file_exists(ROOT.'/worker/worker_despatcher.php')){",
"echo 'git not downloaded right';",
"exit();",
"}",
"echo 'git downloaded';",
"include_once ROOT.'/worker/worker_despatcher.php';"
]]},
"mode" : "000755",
"owner" : "ubuntu",
"group" : "ubuntu"
}
}
}
}
},
并将我的用户数据改为:
"UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
"#!/bin/bash\n",
"apt-get -y install python-setuptools\n",
"easy_install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz\n",
"cfn-init -v ",
" -s ", { "Ref" : "AWS::StackName" },
" -r LaunchConfig ",
" --access-key ", { "Ref" : "WorkerKeys" },
" --secret-key ", {"Fn::GetAtt": ["WorkerKeys", "SecretAccessKey"]},
" --region ", { "Ref" : "AWS::Region" }, "\n"
]]}
现在一切运行顺利。
因此,如果您收到用户脚本错误,则意味着您的启动配置错误,您应该再次查看。
谢谢您的帮助 :)