You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 pystache dependency first
  • Remove the redundant yum install -y aws-cfn-bootstrap step (since you already install it via easy_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

  1. Installing pystache: The aws-cfn-bootstrap package depends on pystache for template rendering, so we explicitly install it before setting up cfn-init.
  2. Removing duplicate installation: Installing via both easy_install and yum can overwrite files or create conflicting Python package paths, which often leads to dependency errors like the one you saw.
  3. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:50:18