自动化 AWS AMI 更新和推出过程

Automate AWS AMI update and Rollout process

我已经设置了一个过程,用于在 ubuntu 基础映像上安装和设置我的应用程序堆栈,然后从机器创建 AMI。所有这些都是使用 packer 自动完成的。现在,一旦我有了 ami-id,一旦打包程序完成,我就会手动创建一个新的启动配置并更新我的 ASG 配置,然后安排扩大和缩小操作以摆脱旧实例。

所以我要找的是:

  1. 每当有任何软件版本的新更新(例如 ngnix ruby 等)时,请建议一种更新我的应用程序堆栈的更好方法
  2. 我怎样才能使推出过程自动化,以便选择新的 ami 而旧的实例应该降级。

Faster Auto Scaling in AWS CloudFormation Stacks with Lambda-backed Custom Resources

中有一个很好的策略

To orchestrate this process, you bootstrap a reference instance with a user data script, use wait conditions to trigger an AMI capture, and finally create an Auto Scaling group launch configuration that references the newly created AMI. The reference instance that is used to capture the AMI can then be terminated, or it can be repurposed for administrative access or for performing scheduled tasks.

该过程不使用 Packer,也不需要专用服务器来创建 AMI,而是使用 Lambda 支持的自定义资源。

第二个选项

由于您已经在 Packer 中创建了 AMI,因此您应该考虑使用 Lambda 将现有的启动配置与更新后的 AMI 一起复制。您可以从 Patch an AMI and Update an Auto Scaling Group:

中看到一个很好的方法

The following example builds on the Simplify AMI Patching Using Automation, Lambda, and Parameter Store example by adding a step that updates an Auto Scaling group with the newly-patched AMI. This approach ensures that new images are automatically made available to different computing environments that use Auto Scaling groups.

The final step of the Automation workflow in this example uses an AWS Lambda function to copy an existing launch configuration and set the AMI ID to the newly-patched AMI. The Auto Scaling group is then updated with the new launch configuration. In this type of Auto Scaling scenario, users could terminate existing instances in the Auto Scaling group to force a new instance to launch that uses the new image. Or, users could wait and allow scale-in or scale-out events to naturally launch newer instances.