AWS CloudFormation - Ubuntu Nginx Metadata



因此,我是云形式的新手,我正在尝试使用nginx创建一个index.html页面,该页面显示了一系列文本,服务器的公共DNS名称和AWS区域。我正在用YAML进行堆栈代码。这是在用户数据下的代码的一部分:

sudo apt-get update
sudo apt-get install -y nginx
sudo service nginx start
cd /var/www/html
echo "<title>CloudFormation</title><h1>Name</h1><p>This page created entirely by CloudFormation</p>" > index.html

我不知道如何使用回声命令将元数据传递到index.html。我尝试了!Sub {EC2Instance.PublicDnsName},但不起作用。显然,方法是使用Amazon Metadata Web服务并使用某些命令获取值,然后将它们传递到index.html中,但是由于我在堆栈中编码并且不使用控制台,所以我不知道该语法。任何人都有指示吗?

对不起,我的代码太长了,所以我无法发表评论。我不使用YAML,但是我可以在JSON中执行您的预期代码。我使用Fn::Base64Fn::join语法。您可以将其引用并迁移到yaml。

"UserData": {
  "Fn::Base64": {
    "Fn::Join": [
      "",
      [
        "#!/bin/bash",
        "n",
        "sudo apt-get updaten",
        "sudo apt-get install -y nginxn",
        "sudo service nginx startn",
        "n",
        "cat > /var/www/html/index.html << "EOF"n",
        "<title>CloudFormation</title>n",
        "<h1>Name</h1><p>This page created entirely by CloudFormation. Author: ",
        {
          "Ref": "Author_name_in_parameter"
        },
        "</p>n",
        "EOFn",
        "n"
      ]
    ]
  }
}

最新更新