【问题标题】:How to install aws-cfn-bootstrap/cfn-init package in Redhat using CloudFormation?如何使用 CloudFormation 在 Redhat 中安装 aws-cfn-bootstrap/cfn-init 包?
【发布时间】:2016-02-14 21:21:12
【问题描述】:

我正在尝试使用 CloudFormation 模板启动一个实例。实例已启动,但 UserData 部分未完全执行,因为 cfn-init/aws-cfn-bootstrap 软件包未安装在 Redhat 7 AMI 中。我尝试手动安装aws-cfn-bootstrap 包,但由于与python版本冲突而无法安装。

这里是 CloudFormation 模板的 UserData 部分

"UserData": {
                "Fn::Base64": {
                    "Fn::Join": [
                        "\n",
                        [
                            "#!/bin/bash",
                            "set -x",
                            "",
                            "INSTANCE_ID=`/opt/aws/bin/ec2-metadata --instance-id | cut -f2 -d' '`",
                            "REGION=`/opt/aws/bin/ec2-metadata --availability-zone| cut -f2 -d' ' | sed '$s/.$//'`",
                            {
                                "Fn::Join": [
                                    "",
                                    [
                                        "AID='",
                                        {
                                            "Fn::GetAtt": [
                                                "eip",
                                                "AllocationId"
                                            ]
                                        },
                                        "'"
                                    ]
                                ]
                            },
                            "aws ec2 associate-address --region $REGION --instance-id $INSTANCE_ID --allocation-id $AID"
                        ]
                    ]
                }
            }

cloud-init.log

Nov 12 03:55:27 localhost cloud-init: Cloud-init v. 0.7.6 running 'modules:config' at Thu, 12 Nov 2015 08:55:27 +0000. Up 19.01 seconds. 
Nov 12 03:55:28 localhost cloud-init: Cloud-init v. 0.7.6 running 'modules:final' at Thu, 12 Nov 2015 08:55:27 +0000. Up 19.67 seconds. 
Nov 12 03:55:28 localhost cloud-init: ++ /opt/aws/bin/ec2-metadata --instance-id 
Nov 12 03:55:28 localhost cloud-init: /var/lib/cloud/instance/scripts/part-001: line 4: /opt/aws/bin/ec2-metadata: No such file or directory 
Nov 12 03:55:28 localhost cloud-init: ++ cut -f2 '-d ' 
Nov 12 03:55:28 localhost cloud-init: + INSTANCE_ID= 
Nov 12 03:55:28 localhost cloud-init: ++ cut -f2 '-d ' 
Nov 12 03:55:28 localhost cloud-init: ++ sed '$s/.$//' 
Nov 12 03:55:28 localhost cloud-init: ++ /opt/aws/bin/ec2-metadata --availability-zone 
Nov 12 03:55:28 localhost cloud-init: /var/lib/cloud/instance/scripts/part-001: line 5: /opt/aws/bin/ec2-metadata: No such file or directory 
Nov 12 03:55:28 localhost cloud-init: + REGION= 
Nov 12 03:55:28 localhost cloud-init: + AID=eipalloc-XXXXXX 
Nov 12 03:55:28 localhost cloud-init: + aws ec2 associate-address --region --instance-id --allocation-id eipalloc-XXXXXX 
Nov 12 03:55:28 localhost cloud-init: /var/lib/cloud/instance/scripts/part-001: line 7: aws: command not found 
Nov 12 03:55:28 localhost cloud-init: 2015-11-12 03:55:28,078 - util.py[WARNING]: Failed running /var/lib/cloud/instance/scripts/part-001 [127] 
Nov 12 03:55:28 localhost cloud-init: 2015-11-12 03:55:28,089 - cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts) 
Nov 12 03:55:28 localhost cloud-init: 2015-11-12 03:55:28,089 - util.py[WARNING]: Running module scripts-user (<module 'cloudinit.config.cc_scripts_user' from '/usr/lib/pyt
hon2.7/site-packages/cloudinit/config/cc_scripts_user.pyc'>) failed 

【问题讨论】:

  • 能否详细描述一下与python版本的冲突?
  • 您的 cloud-init.log 显示没有安装 AWS CLI。

标签: amazon-web-services redhat amazon-cloudformation cloud-init


【解决方案1】:

这对我有用,我没有完全相信这一点,因为我在其中一个 aws 论坛上找到了它,但现在我找不到源链接...无论如何我不得不对其进行一些修改以在 redhat 6 上为我工作,所以希望它也可以帮助其他人。

这是模板中用于安装 cfn-init 脚本的 Userdata 部分:

"UserData"       : { "Fn::Base64" : { "Fn::Join" : ["", [
      "#!/bin/bash -xe\n",

      "### This is redhat 6. It supports cloud-init but Cfn packages need to be installed unlike AWS Linux. And they are installed in a different location\n",
      "# First enable EPEL\n",
      "rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm", "\n",
      "# Now install Python Setuptools(easy_install) and Pip", "\n",
      "yum -y install python-pip", "\n",
      "# Now install cfn scripts", "\n",
      "/usr/bin/easy_install --script-dir /opt/aws/bin https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz", "\n",
      "# Now fix the cfn-hup script and copy to init.d location as AWS does not do it for you", "\n",
      "cp -f `pip show aws-cfn-bootstrap 2> /dev/null | egrep  \"^Location\" | awk -F \":\" ' { print $2 }'`/init/redhat/cfn-hup /etc/init.d/", "\n",
      "chmod 755 /etc/init.d/cfn-hup", "\n",
      "chkconfig --add cfn-hup", "\n",

      "/opt/aws/bin/cfn-init -v ",
      " --stack ", { "Ref" : "AWS::StackId" },
      " --resource AppServer ",
      " --configsets Install ",
      " --region ", { "Ref" : "AWS::Region" }, "\n"
    ]]}}

【讨论】:

  • 这在亚太地区的 RHEL 7.5 HVM 上对我有用。谢谢。
  • 您能否详细说明您的评论“修复 cfn-hup 脚本”。您必须在 cfn-hup 脚本中修复什么?
【解决方案2】:

你可以通过这种方式安装它,只使用pip(不需要使用easy_install):

Python 2

pip install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz

Python 3

pip3 install  https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz

CloudFormation LaunchConfiguration 的完整示例:

LaunchConfig:
  Type: AWS::AutoScaling::LaunchConfiguration
  Properties:
    UserData:
      Fn::Base64:
        !Sub |
          #!/bin/bash

          set -e # Exit on error
          set -u # Treat unset variable as error
          set -x # Trace commands
          set -o pipefail

          # Install latest security updates
          yum update -y

          # Install pip
          yum -y install python3-pip

          # Install AWS CLI and CFN Bootstrap
          pip3 install awscli
          pip3 install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz

【讨论】:

    【解决方案3】:

    对于 RHEL-7.x,你必须添加 pypa-repo,下面是对我有用的东西。

      "UserData": {
          "Fn::Base64": {
            "Fn::Join": [
              "",
              [
                "#!/bin/bash\n",
                "## Install EPEL\n",
                "rpm -Uvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm\n",
                "# Installing CFN-init \n",
                "yum -y install python-pip",
                "\n",
                "cd /use/bin", "\n",
                "/usr/bin/easy_install --script-dir /opt/aws/bin https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz",
                "\n",
                "cp -f `pip show aws-cfn-bootstrap 2> /dev/null | grep -i \"^Location\" | awk   '{ print $2\"/init/redhat/cfn-hup\"}'` /etc/init.d/",
                "\n",
                "chmod 755 /etc/init.d/cfn-hup",
                "\n",
                "chkconfig --add cfn-hup",
                "\n",
                "## DONE \n",
                "# Install the files and packages from the metadata\n",
                "/opt/aws/bin/cfn-init -v ",
                "         --stack ",
                {
                  "Ref": "AWS::StackName"
                },
                "         --resource appInstance1 ",
                "         --region ",
                {
                  "Ref": "AWS::Region"
                },
                "\n",
                "# Start up the cfn-hup daemon to listen for changes to the Web Server metadata\n",
                "/opt/aws/bin/cfn-hup || error_exit 'Failed to start cfn-hup'\n",
                "# Signal the status from cfn-init\n",
                "/opt/aws/bin/cfn-signal -e $? ",
                "         --stack ",
                {
                  "Ref": "AWS::StackName"
                },
                "         --resource appInstance1 ",
                "         --region ",
                {
                  "Ref": "AWS::Region"
                },
                "\n"
              ]
            ]
          }
        }
    

    【讨论】:

    【解决方案4】:

    RedHat 8 示例:

      UserData: 
        Fn::Base64: !Sub |
              #!/bin/bash -x
    
              yum update -y
    
              yum -y install python2-pip
    
              pip2 install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz
    
              python2 /usr/bin/cfn-signal -e $? \
                     --stack ${AWS::StackName} \
                     --resource MyInstance \
                     --region ${AWS::Region}  
    

    MyInstance 是您的 CFN 中实例资源的名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多