在Ubuntu实例中通过CloudFormation(YAML)启动cfn-init的问题
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.logLook 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:DescribeStacksandcloudformation:GetTemplatepermissions attached.Confirm the resource name is correct: The
--resource xxxflag must match the exact logical name of the resource in your CloudFormation template. A tiny typo here will causecfn-initto 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-regionThis will show you immediately if the command works when variables are replaced.
Check cfn-hup (if using it): If you're using
cfn-hupto monitor for configuration changes, ensure it's running properly:sudo systemctl status cfn-hupFor older Ubuntu versions, use
sudo service cfn-hup statusinstead.Validate your template's Metadata section: YAML indentation is strict—double-check that the
AWS::CloudFormation::Initmetadata under your resource is properly indented and formatted. Even a single extra space can break the configuration.
内容的提问来源于stack exchange,提问作者DenCowboy




