Bash 使用CloudFormation AWS时UserData和cfn helper之间的差异

Bash 使用CloudFormation AWS时UserData和cfn helper之间的差异,bash,cloud,server,amazon,provisioning,Bash,Cloud,Server,Amazon,Provisioning,我开始使用CloudFormation进行编排/资源调配,我发现有两种安装软件包的方法: 第一种方法是使用userdata部分中的bash脚本,例如: "UserData": { "Fn::Base64": { "Fn::Join": [ "\n", [ "#!/bin/bash", "apt-get update",

我开始使用CloudFormation进行编排/资源调配,我发现有两种安装软件包的方法:

第一种方法是使用userdata部分中的bash脚本,例如:

"UserData": {
          "Fn::Base64": {
            "Fn::Join": [
              "\n",
              [
                "#!/bin/bash",
                "apt-get update",
                "apt-get upgrade -y",
                "apt-get install apache2 -y",
                "echo \"<html><body><h1>Welcome</h1>\" > /var/www/index.html",

是否有任何理由使用cfn init而不在UserData中使用bash?

实际上,在UserData或cfn init中使用bash没有什么区别。 但是要使用cnf init,您需要安装aws cfn bootstrap包,正如您已经完成的那样,并且还需要定义

"Resources": {
  "MyInstance": {
    "Type": "AWS::EC2::Instance",
    "Metadata" : {
      "AWS::CloudFormation::Init" : {
        "config" : {
          "packages" : {
            :
          },
          "groups" : {
            :
          },
          "users" : {
            :
          },
          "sources" : {
            :
          },
          "files" : {
            :
          },
          "commands" : {
            :
          },
          "services" : {
            :
          }
        }
      }
    },
    "Properties": {
      :
    }
  }
}
cfn init类似于一种干净的方式来完成任务,而不需要在UserData的bash中执行yum安装,尽管这两种方法的结果相同

"Resources": {
  "MyInstance": {
    "Type": "AWS::EC2::Instance",
    "Metadata" : {
      "AWS::CloudFormation::Init" : {
        "config" : {
          "packages" : {
            :
          },
          "groups" : {
            :
          },
          "users" : {
            :
          },
          "sources" : {
            :
          },
          "files" : {
            :
          },
          "commands" : {
            :
          },
          "services" : {
            :
          }
        }
      }
    },
    "Properties": {
      :
    }
  }
}