Packer私钥存储在哪里?

9

我在Ubuntu shell中运行了以下命令,以与AWS平台通信,自定义Amazon AMI(ami-9abea4fb):

$ packer build -debug template.packer 
Debug mode enabled. Builds will not be parallelized.
amazon-ebs output will be in this color.

==> amazon-ebs: Prevalidating AMI Name...
==> amazon-ebs: Pausing after run of step 'StepPreValidate'. Press enter to continue. 
==> amazon-ebs: Inspecting the source AMI...
==> amazon-ebs: Pausing after run of step 'StepSourceAMIInfo'. Press enter to continue. 
==> amazon-ebs: Creating temporary keypair: packer 5dfe9f3b-9cc2-cbfa-7349-5c8ef50c64d5
    amazon-ebs: Saving key for debug purposes: ec2_amazon-ebs.pem
==> amazon-ebs: Pausing after run of step 'StepKeyPair'. Press enter to continue. 

template.packer 是什么:

{
    "builders": [
        {
            "type": "amazon-ebs",
            "region": "us-west-2",
            "source_ami": "ami-9abea4fb",
            "instance_type": "t2.micro",
            "ssh_username": "ubuntu",
            "ami_name": "MiddleTier-{{isotime | clean_ami_name}}",
            "ami_description": "Amazon AMI customised",
            "tags": {
                "role": "MiddleTier"
            },
            "run_tags":{
                "role": "buildSystem"
            }
        }
    ],
    "provisioners": [

    ],
    "post-processors":[

    ]
}

我的理解是,AWS已经为Packer创建了一个私钥(ec2_amazon-ebs.pem),以无密码方式与EC2实例通信,如上述步骤所述。
但我没有看到Packer将私钥(ec2_amazon-ebs.pem)复制到我的笔记本电脑中(作为~/.ssh/ec2_amazon-ebs.pem)。
那么Packer如何在不将私钥复制为~/.ssh/ec2_amazon-ebs.pem的情况下与EC2进行通信呢?
2个回答

13

如果Packer没有被提供私有SSH密钥(ssh_private_key_file),Packer会创建一个临时的,只在Packer运行时保存在内存中的密钥。

当你使用-debug标志运行时,这个临时密钥会保存在当前工作目录中。这是为了让你能够通过手动SSH到实例来解决构建问题。


1
我在同一目录中没有看到临时密钥与模板文件。我已经验证过了。 - overexchange
抱歉,它在当前工作目录中。请参阅https://www.packer.io/docs/builders/amazon-ebs.html#accessing-the-instance-to-debug。 - Rickard von Essen
我的当前工作目录与模板文件所在的目录相同.. 让我再次验证一下。 - overexchange
1
这很方便知道。我对调试的唯一问题是,我希望它能自动继续执行,直到出现错误,因为使用yum update构建需要一段时间才能到达失败点,那时我想探测实例。这些方法也可以实现吗? - openCivilisation

0

我认为连接实例的最佳方式是设置以下两个属性。

ssh_keypair_name      # provide the aws keypair name which already exists. 
ssh_private_key_file  # provide the path to private key associated with above keypair. ~ resolves to current user's home directory. 

如果实例未分配公共 IP,则还需设置 associate_public_ip_address(否则您将不得不使用堡垒主机来 ssh 进入该机器)。

这应该允许您按以下方式进行 ssh:

ssh -i ~/.ssh/private_key.pem ec2-user@<InstancePublicIP>

根据实际私钥路径更改文件名。还要根据操作系统更改用户名(例如,对于基于Ubuntu的机器,请使用ubuntu而不是ec2-user)

您可以使用断点配置程序暂停packer执行,如下所示:

provisioner "breakpoint" {
    disable = true
    note    = "this is a breakpoint"
}

或者你可以在出错时暂停打包执行,类似这样。

packer build -on-error=ask .

这将允许您在Packer暂停输入时通过ssh进入实例以验证脚本。

调试标志如下

packer build -debug .

这也将允许您暂停打包程序的构建。

一旦构建被暂停,请使用上述SSH命令连接到实例。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接