Packer 出现问题:amazon-ebs:等待 SSH 超时

Alf*_*hee 4 ssh amazon-ec2 packer

我是 Packer 新手,尝试使用 VPC 的专用网络创建映像,但不断出现错误*amazon-ebs: Timeout waiting for SSH.*

使用的Packer版本是1.3.4,私有子网可以通过公有子网和路由表访问NAT网关。但由于问题可能无法到达实例,所以我还尝试了其他参数,例如:ssh_interface 的值为private_dnsassociate_public_ip_address。但即使是更改我也会遇到同样的错误。

我正在使用的模板有以下内容

"builders": [
{
  "type": "amazon-ebs",
  "access_key": "{{user `aws_access_key`}}",
  "secret_key": "{{user `aws_secret_key`}}",
  "region": "{{user `region`}}",
  "source_ami": "{{user `source_ami`}}",
  "instance_type": "{{user `instance_type`}}",
  "iam_instance_profile": "{{user `role`}}",
  "ssh_username": "{{user `ssh_username`}}",
  "ssh_timeout": "15m",
  "vpc_id": "{{user `vpc_id`}}",
  "subnet_id": "{{user `subnet_id`}}",
  "associate_public_ip_address": true,
  "ami_name": "{{user `name`}}.{{isotime \"2006-01-02T150405Z\"}}",
  "ami_description": "based on {{user `source_ami`}}",
  "tags": {
    "Name": "{{user `name`}}"
  }]
Run Code Online (Sandbox Code Playgroud)

在模板中我没有定义安全组,但在 Packer 的日志中我看到它能够创建一个临时安全组,那么对端口 22 的访问也应该可用

==> amazon-ebs: Pausing after run of step 'StepKeyPair'. Press enter to continue. 
==> amazon-ebs: Creating temporary security group for this instance: packer_5
c6b3667-c41f-92bc-aa89-efc5f3a2d8a8
==> amazon-ebs: Authorizing access to port 22 from 0.0.0.0/0 in the temporary security group...
==> amazon-ebs: Pausing after run of step 'StepSecurityGroup'. Press enter to continue. 
==> amazon-ebs: Pausing after run of step 'StepCleanupVolumes'. Press enter to continue. 
==> amazon-ebs: Launching a source AWS instance...
Run Code Online (Sandbox Code Playgroud)

但问题仍然存在。模板中是否缺少某些内容?或者我应该做些什么不同的事情来生成 AMI?

小智 6

您无法通过 NAT 网关访问 ec2。AWS 中的 NAT 网关用于提供从 VPC 到 VPC 的 Internet 访问。

您有多种选择:

  1. Make packer 在具有公共 IP 的公共子网中启动 ec2。在 VPC 和路由表中正确配置 IGW
  2. 在AWS中部署一个安全堡垒主机,并使用它从带有加壳器的工作站跳转到ec2。您需要使用自定义通信器在 packer.json 中配置一些内容。这里的文档https://www.packer.io/docs/templates/communicator.html#ssh

问候