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

时间:2018-01-17 12:12:57

标签: amazon-web-services amazon-cloudformation init.d

我使用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

2 个答案:

答案 0 :(得分:6)

如果Ubuntu是您的目标部署,那么您似乎链接了错误的cfn-hup init脚本。如果你在符号链接的cfn-hup文件中查找LSB标题,你会注意到它们不存在。

它隐藏在这里的cfn-hup文档中:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-helper-scripts-reference.html,但是对于Ubuntu,您需要符号链接AWS提供的不同init脚本(而不是代码中的/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之前。

答案 1 :(得分:1)

我得到了我的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