CentOS下CloudFormation执行cfn-init报错:pystache>=0.4.0未找到
Fixing
pkg_resources.DistributionNotFound: pystache>=0.4.0 Error with cfn-init on CentOS Let's break down what's causing this error and how to fix it:
What's Going Wrong
Your error happens because cfn-init requires the pystache Python library (version 0.4.0 or higher), but your UserData steps don't explicitly install it. On top of that, you're installing aws-cfn-bootstrap twice—once via easy_install from the tarball, then again via yum—which creates conflicting environments that can break dependency resolution.
The Fix
Adjust your UserData to:
- Install the missing
pystachedependency first - Remove the redundant
yum install -y aws-cfn-bootstrapstep (since you already install it viaeasy_install) - Ensure the installation flow is clean and ordered
Here's the corrected UserData shell script section:
#!/bin/bash # If UserData hasn't yet been installed then install everything if [[ -z "${USERDATA_INSTALLED}" ]]; then /bin/echo "Entering if statement" >> /tmp/testfile.txt yum update -y yum install -y python-setuptools wget mkdir -p /opt/aws/bin # Install the required pystache dependency first python -m easy_install pystache>=0.4.0 wget https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz python -m easy_install --script-dir /opt/aws/bin aws-cfn-bootstrap-latest.tar.gz /bin/echo "Successfully installed aws-cfn-bootstrap" >> /tmp/testfile.txt # Removed the duplicate yum install of aws-cfn-bootstrap here /opt/aws/bin/cfn-init -v -s ${AWS::StackName} --resource EC2 /bin/echo "Exiting if statement" >> /tmp/testfile.txt fi
Full Corrected CloudFormation Template Snippet
Here's how the updated UserData block looks in your template:
"UserData": { "Fn::Base64": { "Fn::Join": ["\n", [ "Content-Type: multipart/mixed; boundary=\"//\"", "MIME-Version: 1.0", "", "--//", "Content-Type: text/cloud-config; charset=\"us-ascii\"", "MIME-Version: 1.0", "Content-Transfer-Encoding: 7bit", "Content-Disposition: attachment; filename=\"cloud-config.txt\"", "", "#cloud-config", "cloud_final_modules:", "- [scripts-user, always]", "", "--//", "Content-Type: text/x-shellscript; charset=\"us-ascii\"", "MIME-Version: 1.0", "Content-Transfer-Encoding: 7bit", "Content-Disposition: attachment; filename=\"userdata.txt\"", "", "#!/bin/bash", "# If UserData hasn't yet been installed then install everything", "if [[ -z \"${USERDATA_INSTALLED}\" ]]; then", "/bin/echo \"Entering if statement\" >> /tmp/testfile.txt", "yum update -y", "yum install -y python-setuptools wget", "mkdir -p /opt/aws/bin", "python -m easy_install pystache>=0.4.0", "wget https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz", "python -m easy_install --script-dir /opt/aws/bin aws-cfn-bootstrap-latest.tar.gz", "/bin/echo \"Successfully installed aws-cfn-bootstrap\" >> /tmp/testfile.txt", { "Fn::Join" : ["",[ "/opt/aws/bin/cfn-init -v -s ", { "Ref" : "AWS::StackName" }, " --resource EC2" ]]}, "/bin/echo \"Exiting if statement\" >> /tmp/testfile.txt", "fi" ]] } }
Why This Works
- Installing pystache: The
aws-cfn-bootstrappackage depends onpystachefor template rendering, so we explicitly install it before setting upcfn-init. - Removing duplicate installation: Installing via both
easy_installandyumcan overwrite files or create conflicting Python package paths, which often leads to dependency errors like the one you saw. - Cleaner flow: By streamlining the installation steps, we ensure the environment is consistent and all required dependencies are in place before running
cfn-init.
内容的提问来源于stack exchange,提问作者Mark
相关产品推荐
相关产品推荐

