我正在使用对流层生成CloudFormation模板。我正在尝试创建运行LAMP Stack的EC2实例的自动缩放组。
我需要在EC2实例上安装软件包并创建配置文件。
现在,我正在使用Join方法内联指定配置文件内容。
LaunchConfig = t.add_resource(LaunchConfiguration(
"LaunchConfiguration",
Metadata=autoscaling.Metadata(
cloudformation.Init(
cloudformation.InitConfigSets(
install=['install']
),
"install": cloudformation.InitConfig(
"packages" : {
"apt-get" : {
"curl": [],
"zip": [],
"unzip": [],
"git": [],
"supervisor": [],
"sqlite3": [],
"nginx": [],
"php7.2-fpm": [],
"php7.2-cli": [],
"php7.2-pgsql": [],
"php7.2-sqlite3": [],
"php7.2-gd": [],
"php7.2-curl": [],
"php7.2-memcached": [],
"php7.2-imap": [],
"php7.2-mysql": [],
"php7.2-mbstring": [],
"php7.2-xml": [],
"php7.2-zip": [],
"php7.2-bcmath": [],
"php7.2-soap": [],
"php7.2-intl": [],
"php7.2-readline": [],
"php-msgpack": [],
"php-igbinary": []
}
),
{
"config": cloudformation.InitConfig(
files=cloudformation.InitFiles({
"/etc/nginx/sites-available/default": cloudformation.InitFile(
source=Join('', [
"server {",
" listen 80 default_server;",
" root /var/www/html/public;",
" index index.html index.htm index.php;",
" server_name _;",
" charset utf-8;",
" location = /favicon.ico { log_not_found off; access_log off; }",
" location = /robots.txt { log_not_found off; access_log off; }",
" location / {",
" try_files $uri $uri/ /index.php$is_args$args;",
" }",
" location ~ \.php$ {",
" include snippets/fastcgi-php.conf;",
" fastcgi_pass unix:/run/php/php7.2-fpm.sock;",
" }",
" error_page 404 /index.php;",
"}"
])
),
"/etc/supervisor/conf.d/supervisord.conf": cloudformation.InitFile(
source=Join('',[
"[supervisord]",
"nodaemon=true",
"[program:nginx]",
"command=nginx",
"stdout_logfile=/dev/stdout",
"stdout_logfile_maxbytes=0",
"stderr_logfile=/dev/stderr",
"stderr_logfile_maxbytes=0",
"[program:php-fpm]",
"command=php-fpm7.2",
"stdout_logfile=/dev/stdout",
"stdout_logfile_maxbytes=0",
"stderr_logfile=/dev/stderr",
"stderr_logfile_maxbytes=0",
"[program:horizon]",
"process_name=%(program_name)s",
"command=php /var/www/html/artisan horizon",
"autostart=true",
"autorestart=true",
"user=root",
"redirect_stderr=true",
"stdout_logfile=/var/www/html/storage/logs/horizon.log",
])
),
}),
services={
"sysvinit": cloudformation.InitServices({
"nginx": cloudformation.InitService(
enabled=True,
ensureRunning=True
)
})
}
)
}),
),
UserData=Base64(Join('', [
"#!/bin/bash\n",
"cfn-signal -e 0",
" --resource AutoscalingGroup",
" --stack ", Ref("AWS::StackName"),
" --region ", Ref("AWS::Region"), "\n"
])),
ImageId=Ref(AmiId),
KeyName=Ref(KeyName),
BlockDeviceMappings=[
ec2.BlockDeviceMapping(
DeviceName="/dev/sda1",
Ebs=ec2.EBSBlockDevice(
VolumeSize="8"
)
),
],
SecurityGroups=[Ref(SecurityGroup)],
InstanceType="t2.medium",
))
有没有一种方法可以单独保存这些配置文件,并仅使用对流层将其加载?
答案 0 :(得分:0)
这是我的想法,我想介绍一个称为罐头模板的概念。基本上,can只是一组键值配置,然后使用可调用方法在运行时生成模板。请查看此项目https://github.com/MacHu-GWU/configirl-project中的README,它是一种配置管理工具,允许cloudformation,shell脚本,python,terraform和无服务器对话。
我的想法只是将ConfigClass
的子类化并实现def create_template(self)
方法,然后将对流层代码放入该方法中。 这可以解决您的问题。我在很多cloudformation项目中都一直在使用它。
由于这是一个常见用例,我认为值得在库中实现Canned Template
的想法。它正在进行中,它将很快在此troposphere_mate
项目https://github.com/MacHu-GWU/troposphere_mate-project