[英]Difference between UserData and cfn-helper with CloudFormation AWS
I am starting to use CloudFormation for orchestration/provisioning and I see there are two ways to install packages: 我开始将CloudFormation用于业务流程/资源调配,我发现有两种安装软件包的方法:
First way is with a bash script in userdata section, example: 第一种方法是在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",
Another way is to use cfn-init: 另一种方法是使用cfn-init:
"UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
"yum update -y aws-cfn-bootstrap\n",
"# Install the files and packages from the metadata\n",
"/opt/aws/bin/cfn-init -v ",
" --stack ", { "Ref" : "AWS::StackName" },
" --resource WebServerInstance ",
" --configsets Install ",
" --region ", { "Ref" : "AWS::Region" }, "\n"
]]}}
Is there any reasons to use cfn-init and not bash in UserData ? 有什么理由在UserData中使用cfn-init而不使用bash吗?
Actually it makes no difference if you use bash in UserData or cfn-init. 实际上,如果在UserData或cfn-init中使用bash,则没有任何区别。 But to use cnf-init you need to install the package aws-cfn-bootstrap as you have already done and also need to define 但是要使用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 is like a clean way to get things done instead of you needing to do yum install in bash of UserData though you will get the same outcome from both the methods. cfn-init就像完成任务的一种干净方法,而不是需要在UserData的bash中进行yum安装,尽管这两种方法都可以得到相同的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.