使用Packer.io创建基本的AWS AMI时遇到问题。SSH超时。

15
我将尝试按照这些说明使用Packer.io构建基本的AWS镜像。但是它对我不起作用。
以下是我的模板文件:
{
  "variables": {
    "aws_access_key": "",
    "aws_secret_key": ""
  },
  "builders": [{
    "type": "amazon-ebs",
    "access_key": "{{user `aws_access_key`}}",
    "secret_key": "{{user `aws_secret_key`}}",
    "region": "us-east-1",
    "source_ami": "ami-146e2a7c",
    "instance_type": "t2.micro",
    "ssh_username": "ubuntu",
    "ami_name": "packer-example {{timestamp}}",

    # The following 2 lines don't appear in the tutorial.
    # But I had to add them because it said this source AMI
    # must be launched inside a VPC.
    "vpc_id": "vpc-98765432",
    "subnet_id": "subnet-12345678"
  }]
}

您会注意到,我不得不偏离说明,添加底部的两行(用于VPC和子网)。这是因为我一直收到以下错误:

==> amazon-ebs: Error launching source instance: The specified instance type 
                can only be used in a VPC. A subnet ID or network interface
                ID is required to carry out the request. 
                (VPCResourceNotSpecified)

那个VPC和子网是我手动创建的临时的。但为什么我必须这样做呢?为什么packer不像我看到它创建临时安全组和密钥对一样创建它们然后再删除它们呢?
此外,即使我添加了这两行代码,它仍然无法创建AMI,因为它遇到SSH超时。为什么?我手动SSH到此VPC中的其他实例没有任何问题。临时的packer实例具有"InstanceState=Running"、"StatusChecks=2/2"和允许来自世界各地的SSH的安全组。
请参见下面packer命令的调试输出:
$ packer build -debug -var 'aws_access_key=MY_ACCESS_KEY' -var 'aws_secret_key=MY_SECRET_KEY' packer_config_basic.json
Debug mode enabled. Builds will not be parallelized.
amazon-ebs output will be in this color.

==> amazon-ebs: Inspecting the source AMI...
==> amazon-ebs: Pausing after run of step 'StepSourceAMIInfo'. Press enter to continue.
==> amazon-ebs: Creating temporary keypair: packer 99999999-8888-7777-6666-555555555555
    amazon-ebs: Saving key for debug purposes: ec2_amazon-ebs.pem
==> amazon-ebs: Pausing after run of step 'StepKeyPair'. Press enter to continue.
==> amazon-ebs: Creating temporary security group for this instance...
==> amazon-ebs: Authorizing SSH access on the temporary security group...
==> amazon-ebs: Pausing after run of step 'StepSecurityGroup'. Press enter to continue.
==> amazon-ebs: Launching a source AWS instance...
    amazon-ebs: Instance ID: i-12345678
==> amazon-ebs: Waiting for instance (i-12345678) to become ready...
    amazon-ebs: Private IP: 10.0.2.204
==> amazon-ebs: Pausing after run of step 'StepRunSourceInstance'. Press enter to continue.
==> amazon-ebs: Waiting for SSH to become available...
==> amazon-ebs: Timeout waiting for SSH.
==> amazon-ebs: Pausing before cleanup of step 'StepRunSourceInstance'. Press enter to continue.
==> amazon-ebs: Terminating the source AWS instance...
==> amazon-ebs: Pausing before cleanup of step 'StepSecurityGroup'. Press enter to continue.
==> amazon-ebs: Deleting temporary security group...
==> amazon-ebs: Pausing before cleanup of step 'StepKeyPair'. Press enter to continue.
==> amazon-ebs: Deleting temporary keypair...
==> amazon-ebs: Pausing before cleanup of step 'StepSourceAMIInfo'. Press enter to continue.
Build 'amazon-ebs' errored: Timeout waiting for SSH.

==> Some builds didn't complete successfully and had errors:
--> amazon-ebs: Timeout waiting for SSH.

==> Builds finished but no artifacts were created.

当我在您的模板上运行 packer validate <filename> 时,哈希注释会出现错误。实际上,注释不是 JSON 规范的一部分。 - kmarsh
2个回答

32
  1. 您正在使用t2.micro实例类型,该类型仅可在VPC环境中运行(请参见T2 Instances)。

  2. 由于您在中,默认情况下所有流量都在防火墙后面,因此您需要设置一个安全组以允许您的IP访问该实例上的SSH端口。

更简单的方法是使用m3.medium实例类型,虽然有点贵但是它运行所有内容更快而且您无需设置。


3
关于t2.micro,为什么Packer会在他们的网站上发布一个不起作用的教程? 我将t2.micro更改为m3.medium。它要求我输入ssh_username。我输入了ubuntu。现在,这是我得到的错误:https://gist.github.com/anonymous/125cf22597cce8a73d5b - Saqib Ali
3
@SaqibAli,你模板中的AMI(ami-146e2a7c)是针对Amazon Linux的,其默认用户为ec2-user。 - number5
9
为什么不把采纳答案的标记给@number5呢? - moodboom

0

请确保以下两点:

  1. 已将互联网网关(活动状态,非黑洞)附加到我们正在启动实例的默认 VPC 上。
  2. 检查路由表,确保当前存在指向互联网网关(非旧的)的路由。

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