AWS Cloudformation cfn-hup 在默认启动级别上失败

Posted

技术标签:

【中文标题】AWS Cloudformation cfn-hup 在默认启动级别上失败【英文标题】:AWS Cloudformation cfn-hup fails on default start levels 【发布时间】:2018-06-26 07:42:57 【问题描述】:

我使用 Ubuntu 16.04 编写了一个 cloudformation 脚本,并实现了 userdata/cloud init 脚本。但是,当它尝试启动 cfn-hup 时,它会根据 init.d 脚本失败。脚本如下。我到处搜索以了解默认开始级别,我可以手动编辑它们,但我在任何其他模板中都找不到这是必要的,所以我一定做错了什么?!脚本如下。

如果有人有想法请分享,非常感谢!

2018-01-17 11:58:36,562 P7799 [INFO] Command 01_enable_cfn_hup
2018-01-17 11:58:36,665 P7799 [INFO] -----------------------Command Output-----------------------
2018-01-17 11:58:36,665 P7799 [INFO]    Synchronizing state of cfn-hup.service with SysV init with /lib/systemd/systemd-sysv-install...
2018-01-17 11:58:36,665 P7799 [INFO]    Executing /lib/systemd/systemd-sysv-install enable cfn-hup
2018-01-17 11:58:36,665 P7799 [INFO]    insserv: warning: script 'cfn-hup' missing LSB tags and overrides
2018-01-17 11:58:36,665 P7799 [INFO]    update-rc.d: error: cfn-hup Default-Start contains no runlevels, aborting.
2018-01-17 11:58:36,665 P7799 [INFO] ------------------------------------------------------------
2018-01-17 11:58:36,665 P7799 [ERROR] Exited with error code 1

脚本:

  AppServerInstanceLaunchConfig:
    Type: 'AWS::AutoScaling::LaunchConfiguration'
    DependsOn: VPCGatewayAttachment
    Properties:
      AssociatePublicIpAddress: true
      KeyName: !Ref KeyPairName
      ImageId: !FindInMap 
        - AWSRegionArch2AMI
        - !Ref 'AWS::Region'
        - !FindInMap 
          - AWSInstanceType2Arch
          - !Ref InstanceTypeParam
          - Arch
      InstanceType: !Ref InstanceTypeParam
      SecurityGroups:
        - !Ref PubSubnetSecurityGroup
      UserData: !Base64 
        'Fn::Join':
          - ''
          - - |
              #!/bin/bash -xe
            - |
              # Install AWS cfn-bootstrap utilities
              apt-get update
              apt-get -y install python-pip
            - >
              pip install
              https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz
            - |
              ln -s /usr/local/bin/cfn-hup /etc/init.d/
            - |
              chmod 775 /etc/init.d/cfn-hup
            - |
              chown root:root /etc/init.d/cfn-hup
            - /usr/local/bin/cfn-init
            - ' --stack '
            - !Ref 'AWS::StackName'
            - ' --resource AppServerInstanceLaunchConfig'
            - ' --configsets SetupAppServer'
            - ' --region '
            - !Ref 'AWS::Region'
            - |+

            - /usr/local/bin/cfn-signal -e $? --stack
            - !Ref 'AWS::StackName'
            - ' --resource AppServerInstanceGroup'
            - ' --region '
            - !Ref 'AWS::Region'
    Metadata:
      'AWS::CloudFormation::Designer':
        id: 7f848ae7-0378-4ac3-800c-1f4c1ad4de4c
      'AWS::CloudFormation::Init':
        configSets:
          SetupAppServer:
            - config1
        config1:
          packages:
            apt:
              git: []
              php: []
              apache2: []
              apt-transport-https: []
              ca-certificates: []
              curl: []
              software-properties-common: []
          commands:
            01_enable_cfn_hup:
              command: systemctl enable cfn-hup.service
            02_start_cfn_hup:
              command: systemctl start cfn-hup.service
          files:
            /etc/cfn/cfn-hup.conf:
              content: !Join 
                - ''
                - - |-
                    [main]
                    stack=
                  - !Ref 'AWS::StackName'
                  - |-

                    region=
                  - !Ref 'AWS::Region'
              mode: '000400'
              owner: root
              group: root
            /etc/cfn/hooks.d/cfn-auto-reloader.conf:
              content: !Join 
                - ''
                - - |
                    [cfn-auto-reloader-hook]
                  - |
                    triggers=post.update
                  - >
                    path=Resources.LaunchConfig.Metadata.AWS::CloudFormation::Init
                  - 'action=/usr/local/bin/cfn-init -v --stack '
                  - !Ref 'AWS::StackName'
                  - ' --resource AppServerInstanceLaunchConfig --region '
                  - !Ref 'AWS::Region'
                  - ''
                  - |
                    runas=root
            /lib/systemd/system/cfn-hup.service:
              content: !Join 
                - ''
                - - |
                    [Unit]
                  - |+
                    Description=cfn-hup daemon

                  - |
                    [Service]
                  - |
                    Type=simple
                  - |
                    ExecStart=/usr/local/bin/cfn-hup
                  - |+
                    Restart=always

                  - |
                    [Install]
                  - WantedBy=multi-user.target
          services:
            sysvinit:
              apache2:
                enabled: true
                ensureRunning: true
              cfn-hup:
                enabled: true
                ensureRunning: true
                files:
                  - /etc/cfn/cfn-hup.conf
                  - /etc/cfn/hooks.d/cfn-auto-reloader.conf

【问题讨论】:

【参考方案1】:

如果 Ubuntu 是您的目标部署,那么看起来您链接了错误的 cfn-hup 初始化脚本。如果您在符号链接的 cfn-hup 文件中查找 LSB 标头,您会发现它们不存在。

它隐藏在此处的 cfn-hup 文档中:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-helper-scripts-reference.html,但对于 Ubuntu,您需要符号链接 AWS 提供的不同初始化脚本(而不是代码中的 /usr/local/bin/cfn-hup):

ln -s /root/aws-cfn-bootstrap-latest/init/ubuntu/cfn-hup /etc/init.d/cfn-hup

但是,根据您安装aws-cfn-boostrap-latest.tar.gz 的方式,该文件可能位于不同的位置,对我来说它位于此处:

/usr/local/lib/python2.7/dist-packages/aws_cfn_bootstrap-1.4-py2.7.egg/init/ubuntu/cfn-hup

您还需要运行:

update-rc.d cfn-hup defaults

就在开始之前cfn-init

【讨论】:

我做了所有这些并运行了update-rc.d cfn-hup defaults。我收到script cfn-hup is not an executable regular file, skipped! 亚马逊是否试图阻止人们使用 Amazon Linux 以外的发行版?我花了太多时间试图让它工作 @lfk 您需要设置正确的权限。 chmod 700 /etc/init.d/cfn-hup 为我解决了这个问题。【参考方案2】:

按照此处的建议,我让我的 ubuntu 16.04 EC2 成功地初始化了 cfn-hup。 看起来你可能忘记了:

chmod +x /usr/local/lib/python2.7/dist-packages/aws_cfn_bootstrap-1.4-py2.7.egg/init/ubuntu/cfn-hup

【讨论】:

以上是关于AWS Cloudformation cfn-hup 在默认启动级别上失败的主要内容,如果未能解决你的问题,请参考以下文章

将现有 AWS 资源整合到 CloudFormation 堆栈中

AWS Cloudformation的相关概念

CloudFormation - 将标签应用于其他 AWS 资源

AWS CloudFormation:Cognito LambdaTrigger CustomEmailSender - 属性“AWS CloudFormation 目前不支持。”和 CDK 的使用

AWS学习笔记--利用CloudFormation管理AWS资源

AWS — AWS CloudFormation