AWS Cloudformation模板:哪个部分?

时间:2018-11-13 18:37:20

标签: amazon-web-services amazon-cloudformation

如果我编写了一个cloudFormation模板来创建一个EC2实例,并且我需要安装软件包并在计算机启动时进行一些配置更改。为了达到此目的,我应该编辑模板的哪一部分?

是属性,参数,输出还是映射?

2 个答案:

答案 0 :(得分:1)

您可以通过将脚本放在UserData下的Properties中来安装软件。该脚本将在服务器部署后运行

下面是安装Apache的示例:

    "UserData": {
      "Fn::Base64": {
        "Fn::Join": [
          "\n",
          [
            "#!/bin/bash -xe",
            "sudo yum update -y",
            "sudo yum install httpd -y",
            "sudo /etc/init.d/httpd start",
            "echo \"<html><body>Installed httpd successfully\" > /var/www/html/index.html",
            "echo \"</body></html>\" >> /var/www/html/index.html"
          ]
        ]
      }
    }

Metadata可以做很多事情。查看参考资料以获取更多详细信息

参考

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/deploying.applications.html

答案 1 :(得分:0)

UserData 中,您必须提及要用来安装软件包的所有bash脚本

所以您的Cloudformation看起来像

{
   "AWSTemplateFormatVersion" : "2010-09-09",
   "Parameters" : {some paramters...}
   "Mappings" : {some mappings...}
   "Resources" : {
      "EC2Instance" : {
        "Type" : "AWS::EC2::Instance", 
        "Properties" : {
        "KeyName" : { "Ref" : "KeyName" },
        "UserData" : {here you have to add all your script to deploy while boot up Ec2 }
   }
}