【问题标题】:Userdata script not running in ubuntu server while creating with cloudformation template使用 cloudformation 模板创建时,用户数据脚本未在 ubuntu 服务器中运行
【发布时间】:2018-07-03 02:52:45
【问题描述】:

很抱歉提出重复的老问题。我的代码没有运行用户数据 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 参数不是有效的脚本,这不会被拆分为数组,而是被视为字符串,尝试将 apt-get update\n update-get install -y apache2..... 作为 UserData CloudFormation 参数传递
  • 这个用户数据参数apt-get update\n apt-get install -y apache2\n apt-get install -y php不起作用@toske我的JSON代码有什么变化
  • 系统日志显示什么?还要从 aws 控制台检查实例上的用户数据。格式是否正确?
  • 还要注意 CFN 中的“/n”字符。这似乎是无效的。
  • 我将apt-get update\n apt-get install -y apache2\n apt-get install -y php 作为用户数据参数。 aws 控制台中的用户数据看起来像 #/bin/bash -xe apt-get update\n apt-get install -y apache2\n apt-get install -y php 并且还将我的 json 代码 "/n" 更新为 "\n" 那么我的问题是什么? @RodrigoM

标签: amazon-web-services amazon-ec2 ubuntu-16.04 amazon-cloudformation user-data


【解决方案1】:

试试这个 UserData 块。 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 该日志的输出确实有助于调试。

【讨论】:

  • 谢谢,它工作得很好。但是我需要通过 userdata 参数来实现,我的 json 代码或 userdata 参数有什么问题。否则请提供一些想法,了解在 cloudformation 模板中为用户数据提供参数@Mareks Pikalovs
  • @ArunKumar 您可能需要考虑使用更高级别的工具构建您的 cloudformation,例如 cfndsl(用于 cloudformation 的 ruby​​ dsl)。这样您就可以使用 ruby​​ 变量来定义用户数据,但在编译的模板中进行硬编码。
  • 我们可以给用户数据,比如apt-get update;apt-get install -y apache2;apt-get install -y php; 它的工作。谢谢大家
【解决方案2】:

它接受以下用户数据,并安装以下脚本中给出的所有软件。

apt-get update;apt-get install -y apache2;apt-get install -y php;

【讨论】:

    猜你喜欢
    • 2019-06-24
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 2015-12-20
    相关资源
    最近更新 更多