Hashicorp 加壳器:如何在第一个 运行 上初始化实例?

Hashicorp packer: How to init instance on first run?

我使用 Hashicorp Packer 创建 AWS ubuntu 图像。我需要在第一个 运行 上执行一些实例初始化。我知道我可以创建一个 运行 一次的脚本。但我想知道是否有开箱即用的解决方案,因为我在文档中找不到任何相关内容。

Packer 只关心构建基础 AMI,并不关心之后会发生什么。最好的选择是 cloud-init 脚本,因为这就是它们的用途。正如他们网站上提到的:

Cloud-init is the industry standard multi-distribution method for cross-platform cloud instance initialization. It is supported across all major public cloud providers, provisioning systems for private cloud infrastructure, and bare-metal installations.

可在此处找到亚马逊关于设置用户数据脚本的文档: Run commands on your Linux instance at launch.

它们包含一个使用 cloud-init 指令的示例:

#cloud-config
repo_update: true
repo_upgrade: all

packages:
 - httpd
 - mariadb-server

runcmd:
 - [ sh, -c, "amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2" ]
 - systemctl start httpd
 - sudo systemctl enable httpd
 - [ sh, -c, "usermod -a -G apache ec2-user" ]
 - [ sh, -c, "chown -R ec2-user:apache /var/www" ]
 - chmod 2775 /var/www
 - [ find, /var/www, -type, d, -exec, chmod, 2775, {}, \; ]
 - [ find, /var/www, -type, f, -exec, chmod, 0664, {}, \; ]
 - [ sh, -c, 'echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php' ]

除此之外,另一个解决方案是使用配置即代码工具,例如 Puppet、Chef、Ansible 或 Salt。