简体   繁体   English

如何使用 Packer 将 AMI 复制到另一个帐户?

[英]How can I copy an AMI to another account using Packer?

I have two AWS Accounts:我有两个 AWS 账户:

  1. Test Account测试账户
  2. Prod Account产品帐户

I am creating an AMI using Packer in the Test Account and want to copy the AMI to the Prod Account after that.我正在测试帐户中使用 Packer 创建一个 AMI,然后想将 AMI 复制到 Prod 帐户。

How can I use Packer to do that and also remove the actual AMI after the job is done?我如何使用 Packer 来做到这一点,并在工作完成后删除实际的 AMI?

I already checked following questions but they didn't resolve my query:我已经检查了以下问题,但他们没有解决我的查询:

  1. How do I bulk copy AMI AWS account number permissions from one AMI image to another? 如何将 AMI AWS 帐号权限从一个 AMI 映像批量复制到另一个?
  2. how to copy AMI from one aws account to other aws account? 如何将 AMI 从一个 aws 帐户复制到另一个 aws 帐户?

You can accomplish this behavior by using the ami_users directive in packer.您可以通过在 packer 中使用ami_users指令来完成此行为。 This will allow the specified accounts to access the created AMIs from the source account.这将允许指定账户从源账户访问创建的 AMI。

If you are looking to have a deep copy of the AMIs in each account (distinct IDs) then you will have to re-run packer build with credentials into the other account.如果您希望在每个账户中拥有 AMI 的深层副本(不同的 ID),那么您将必须使用凭据重新运行packer build到另一个账户中。

As answered above use ami_users .如上所述,使用ami_users

The way we use this in production is, we usually have vars file for each environment in the "vars" folder.我们在生产中使用它的方式是,我们通常在“vars”文件夹中为每个环境都有 vars 文件。 One of the value in the vars JSON file is "nonprod_account_id":"1234567890" . vars JSON 文件中的值之一是"nonprod_account_id":"1234567890" Then in the packer.json, use ami_users as below.然后在 packer.json 中,使用ami_users如下。

"ami_users": ["{{user `nonprod_account_id`}}"]

I'm unclear on why you would want to remove the AMI from the account where it was built after copying it to another account rather than just building it in the "destination" account, unless maybe there are stronger access restrictions or something in Prod, but in that case I would question copying in an AMI built where things are "loose".我不清楚为什么您要在将 AMI 复制到另一个帐户后从其构建的帐户中删除它,而不是仅仅在“目标”帐户中构建它,除非可能有更强的访问限制或 Prod 中的某些内容,但在那种情况下,我会质疑在事情“松散”的情况下构建的 AMI 中的复制。

To specifically do the copying you may want this plugin.要专门进行复制,您可能需要此插件。

https://github.com/martinbaillie/packer-post-processor-ami-copy https://github.com/martinbaillie/packer-post-processor-ami-copy

The removal from the source account might need to be "manual" or could be automated by a cleanup process that removes AMIs older than a certain period of time.从源账户中删除可能需要“手动”进行,或者可以通过删除超过特定时间段的 AMI 的清理过程自动进行。 As of May 2019 it is possible to create in one account and share access for both unencrypted AND encrypted AMIs (the ability to copy/utilize encrypted AMIs is the new bit compared to the other answers).截至 2019 年 5 月,可以在一个帐户中创建并共享未加密和加密 AMI 的访问权限(与其他答案相比,复制/利用加密 AMI 的能力是新的一点)。

A couple Amazon posts on the new capabilities.亚马逊发布了一些关于新功能的帖子。

https://aws.amazon.com/about-aws/whats-new/2019/05/share-encrypted-amis-across-accounts-to-launch-instances-in-a-single-step/ https://aws.amazon.com/about-aws/whats-new/2019/05/share-encrypted-amis-across-accounts-to-launch-instances-in-a-single-step/

https://aws.amazon.com/blogs/security/how-to-share-encrypted-amis-across-accounts-to-launch-encrypted-ec2-instances/ https://aws.amazon.com/blogs/security/how-to-share-encrypted-amis-across-accounts-to-launch-encrypted-ec2-instances/

This article outlines a process of using Packer to copy an AMI between accounts rather than just referencing a source in another account, you can probably extend it to perform the cleanup.本文概述了使用 Packer 在账户之间复制 AMI 的过程,而不仅仅是引用另一个账户中的源,您可能可以扩展它来执行清理。

https://www.helecloud.com/single-post/2019/03/21/How-to-overcome-AWS-Copy-AMI-boundaries-by-using-Hashicorp%E2%80%99s-Packer https://www.helecloud.com/single-post/2019/03/21/How-to-overcome-AWS-Copy-AMI-boundaries-by-using-Hashicorp%E2%80%99s-Packer

This one shows an updated process from above that uses the ability to grant access across accounts to avoid creating multiple copies of the AMI, one for each account/environment where you want to utilize it.这显示了上面的更新过程,该过程使用跨账户授予访问权限的能力,以避免创建 AMI 的多个副本,每个副本用于您想要使用它的每个账户/环境。

https://www.helecloud.com/single-post/2019/11/06/Overcome-AWS-Copy-AMI-boundaries-%E2%80%93-share-encrypted-AMIs-with-Packer-%E2%80%93-follow-up https://www.helecloud.com/single-post/2019/11/06/Overcome-AWS-Copy-AMI-boundaries-%E2%80%93-share-encrypted-AMIs-with-Packer-%E2% 80%93-跟进

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM