You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Ubuntu 22.04 EC2实例中CloudFormation配置的cfn-hup无法自动触发的问题排查求助

Ubuntu 22.04 EC2实例中CloudFormation配置的cfn-hup无法自动触发的问题排查求助

大家好,我最近在Ubuntu 22.04的EC2实例上配置CloudFormation的cfn-hup服务,想要实现栈更新时自动触发cfn-init,但遇到了棘手的问题,想请教下各位大佬。

先说说我的配置背景:我参考了AWS官方文档里的cfn-hup示例,结合CloudWatch Agent的配置案例,写了如下的CloudFormation资源配置:

01_setupCfnHup:
  files:
    '/etc/cfn/cfn-hup.conf':
      content: !Sub |
        [main]
        stack=${AWS::StackId}
        region=${AWS::Region}
        interval=1
      mode: '000400'
      owner: root
      group: root
    '/etc/cfn/hooks.d/amazon-cloudwatch-agent-auto-reloader.conf':
      content: !Sub |
        [cfn-auto-reloader-hook]
        triggers=post.update
        path=Resources.EC2Instance.Metadata.AWS::CloudFormation::Init.02_config-amazon-cloudwatch-agent
        action=/opt/aws/bin/cfn-init -v --stack ${AWS::StackId} --resource EC2Instance --region ${AWS::Region} --configsets UpdateEnvironment
        runas=root
      mode: '000400'
      owner: root
      group: root
    "/lib/systemd/system/cfn-hup.service":
      content: !Sub |
        [Unit]
        Description=cfn-hup daemon
        [Service]
        Type=simple
        ExecStart=/opt/aws/bin/cfn-hup
        Restart=always
        [Install]
        WantedBy=multi-user.target
  commands:
    01enable_cfn_hup:
      command: !Sub |
        systemctl enable cfn-hup.service
    02start_cfn_hup:
      command: !Sub |
        systemctl start cfn-hup.service

同时,我在UserData脚本里也执行了pip3 install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz来安装必要的依赖。

现在的问题是:当我更新CloudFormation栈的时候,cfn-hup完全没有触发cfn-init。查看/var/log/cfn-hup.log日志,只有一些看起来正常的日志条目:

2023-07-08 09:49:56,112 [DEBUG] CloudFormation client initialized with endpoint https://cloudformation.eu-west-2.amazonaws.com
2023-07-08 09:49:56,115 [INFO] No umask value specified in config file. Using the default one: 0o22

从日志看服务似乎在正常运行,但就是检测不到栈更新。奇怪的是,如果我手动从shell执行cfn-hup --no-daemon,更新就能被正确检测到,cfn-init也会按预期触发。

有没有大佬遇到过类似的问题?或者能帮我排查下哪里可能出问题了吗?

备注:内容来源于stack exchange,提问作者Nye

火山引擎 最新活动