【发布时间】:2018-11-13 18:37:20
【问题描述】:
如果我编写了一个创建了一个 EC2 实例的 cloudFormation 模板,并且我需要在机器启动时安装软件包并进行一些配置更改。为了实现这一点,我应该编辑模板的哪个部分?
是属性、参数、输出还是映射?
【问题讨论】:
标签: amazon-web-services amazon-cloudformation
如果我编写了一个创建了一个 EC2 实例的 cloudFormation 模板,并且我需要在机器启动时安装软件包并进行一些配置更改。为了实现这一点,我应该编辑模板的哪个部分?
是属性、参数、输出还是映射?
【问题讨论】:
标签: amazon-web-services amazon-cloudformation
您可以通过将脚本放入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
【讨论】:
在 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 }
}
}
【讨论】: