gpt4 book ai didi

amazon-web-services - 使用 cloudformation 模板创建时,用户数据脚本未在 ubuntu 服务器中运行

转载 作者:行者123 更新时间:2023-12-03 07:28:16 25 4
gpt4 key购买 nike

很抱歉问了重复的老问题。我的代码不运行 userdata shell 脚本。我的 userdata 属性是创建堆栈时给出的参数。我的代码是

{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "AWS CloudFormation Sample Template",
"Parameters": {
"UserData": {
"Description": "User data",
"Type": "String"
}
},
"Resources": {
"EC2Instance": {
"Type": "AWS::EC2::Instance",
"Properties": {
"KeyName": {
"Ref": "KeyName"
},
"InstanceType": {
"Ref": "InstanceType"
},
"ImageId": {
"Ref": "ImageId"
},
"SecurityGroups": [
{
"Ref": "EC2SecurityGroup"
}
],
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"#!/bin/bash",
"\n",
{
"Ref": "UserData"
}
]
]
}
}
}
}
}

}

我在 userdata 参数中给出了以下 shell 命令

"apt-get update","\n","apt-get install -y apache2","\n","apt-get install -y php","\n"

我不知道我的用户数据脚本是否正确,创建堆栈时没有显示任何错误,它成功创建了实例。创建实例后,我检查了实例中未安装 apache2 和 php 。谁能发现我的代码有什么问题吗?

最佳答案

尝试这个 UserData block 。 UserData 被重命名为 UData 以排除命名冲突。在这种情况下,UData 应该是“apt-get update; apt-get install -y apache2 php;”

"AWSTemplateFormatVersion": "2010-09-09",
"Description": "AWS CloudFormation Sample Template",
"Parameters": {
"UData": {
"Description": "User data",
"Type": "String"
}
},
"Resources": {
"EC2Instance": {
"Type": "AWS::EC2::Instance",
"Properties": {
"KeyName": {
"Ref": "KeyName"
},
"InstanceType": {
"Ref": "InstanceType"
},
"ImageId": {
"Ref": "ImageId"
},
"SecurityGroups": [
{
"Ref": "EC2SecurityGroup"
}
],
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"#!/bin/bash\n",
"echo \">>>>>>>>>>>>> UPDATE <<<<<<<<<<<<<\"\n",
"export USR_DATA='", { "Ref": "UData" }, "'\n",
"echo $USR_DATA\n",
"echo $USR_DATA > user_data.sh\n",
"chmod +x user_data.sh\n",
"source user_data.sh\n",
"echo \">>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<\"\n"
]
]
}
}
}
}
}

用户数据也记录在/var/log/cloud-init-output.log 中该日志的输出确实有助于调试。

关于amazon-web-services - 使用 cloudformation 模板创建时,用户数据脚本未在 ubuntu 服务器中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48416070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com