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

在Ubuntu实例中通过CloudFormation(YAML)启动cfn-init的问题

Debugging cfn-init After Successful Installation on Your EC2 Instance

Hey there, let's walk through what to check now that your cfn-init installation has completed successfully and you can access your instance. First, here's the setup script you're using for reference:

Fn::Base64: !Sub | 
#!/bin/bash 
sudo apt-get -y install python-setuptools 
mkdir aws-cfn-bootstrap-latest 
curl https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz | tar xz -C aws-cfn-bootstrap-latest --strip-components 1 
sudo easy_install aws-cfn-bootstrap-latest 
sudo /usr/local/bin/cfn-init --stack !Ref 'AWS::StackName' --resource xxx --region !Ref 'AWS::Region'

Since you've confirmed the install steps worked but likely are seeing issues with cfn-init not applying your desired configurations, here are the most common debugging steps I recommend:

  • Check the cfn-init log file: This is the first stop for any issues. The log is located at /var/log/cfn-init.log—run this command to see recent activity:

    sudo tail -n 50 /var/log/cfn-init.log
    

    Look for errors like missing resource references, IAM permission denied messages, or invalid YAML in your template's metadata.

  • Verify IAM role permissions: Your EC2 instance's IAM role needs permissions to fetch the CloudFormation stack details. Make sure it has at least cloudformation:DescribeStacks and cloudformation:GetTemplate permissions attached.

  • Confirm the resource name is correct: The --resource xxx flag must match the exact logical name of the resource in your CloudFormation template. A tiny typo here will cause cfn-init to not find the configuration.

  • Test the cfn-init command manually: Try running the command with hardcoded values (replace the placeholders with your actual stack name, resource name, and region) to rule out issues with the CloudFormation references:

    sudo /usr/local/bin/cfn-init --stack your-actual-stack-name --resource your-resource-logical-name --region your-region
    

    This will show you immediately if the command works when variables are replaced.

  • Check cfn-hup (if using it): If you're using cfn-hup to monitor for configuration changes, ensure it's running properly:

    sudo systemctl status cfn-hup
    

    For older Ubuntu versions, use sudo service cfn-hup status instead.

  • Validate your template's Metadata section: YAML indentation is strict—double-check that the AWS::CloudFormation::Init metadata under your resource is properly indented and formatted. Even a single extra space can break the configuration.

内容的提问来源于stack exchange,提问作者DenCowboy

火山引擎 最新活动