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

AWS Cloudformation cfn-hup fails on default start levels

我使用 Ubuntu 16.04 编写了一个 cloudformation 脚本并实现了 userdata/cloud 初始化脚本。但是,当它尝试启动 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

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

它隐藏在此处的 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

我的 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