题 Packer无法将aws映像打包到VPC中


我正在尝试制作一个包装图像,但在我们的亚马逊帐户中,我们没有默认的VPC。它已被删除。在尝试打包图像时出现此错误:

==> amazon-instance: Inspecting the source AMI...
==> amazon-instance: Creating temporary keypair: packer 54cfd9c9-61ef-5f8f-4091-d27e731a8a4d
==> amazon-instance: Creating temporary security group for this instance...
==> amazon-instance: No default VPC for this user (VPCIdNotSpecified)
==> amazon-instance: Deleting temporary keypair...
Build 'amazon-instance' finished.

因此,我应该指定默认的VPC ID或子网ID。

我试过两个,

{
  "variables": {
    "vpc_id ": "vpc-962438f4",
    "subnet_id": "subnet-1c5d5c68"
    },
  "builders": [{
    "type": "amazon-instance",
    "access_key": "somekey"
    "secret_key": "somekey"
    "account_id": "AccountIDNUMBER"
    "region": "ap-southeast-2",
    "source_ami": "ami-b7eb9e8d",
    "s3_bucket": "layer2amis",
    "x509_cert_path": "packer/cert-x509.pem",
    "x509_key_path": "packer/key-x509.pem",
    "instance_type": "t2.medium",
    "ssh_username": "ubuntu",
    "ssh_timeout": "5m",
    "ami_virtualization_type": "hvm",
    "ami_name": "layer2_stagingserver_{{timestamp}}",
    "bundle_vol_command": "sudo -n /usr/local/ec2/ec2-ami-tools-1.5.3/bin/ec2-bundle-vol -k {{.KeyPath}} -u {{.AccountId}} -c {{.CertPath}} -r {{.Architecture}} -e {{.PrivatePath}}/* -d {{.Destination}} -p {{.Prefix}} --batch --no-filter",
    "bundle_upload_command": "sudo -n /usr/local/ec2/ec2-ami-tools-1.5.3/bin/ec2-upload-bundle -b {{.BucketName}} -m {{.ManifestPath}} -a {{.AccessKey}} -s {{.SecretKey}} -d {{.BundleDirectory}} --region ap-southeast-2 --batch --retry"
  }],
}

打包器的Web上的文档只是说vpc_id(字符串) - 如果启动到VPC子网,Packer需要VPC ID才能在VPC中创建临时安全组。


5
2018-02-02 20:24




“vpc_id”中有一个错误的空间。 - jbird


答案:


正如你所说,有一个 vpc_id 在中指出的选项 amazon-ebs建设者的文档。您已将此选项添加到Packer JSON文件中,但是,您将其添加到错误的位置。

vpc_id 选项应添加到您的 建设者 对象而不是 变量 宾语。所以看起来应该是这样的:

{
    "variables": {},
    "builders": [{
        "vpc_id": "vpc-12345678",
        "subnet_id": "subnet-1c5d5c68",
        "type": "amazon-instance",
        "access_key": "somekey",
        "secret_key": "somekey",
        "account_id": "AccountIDNUMBER",

        [...]
    }],
}

5
2018-02-06 15:38



您好,感谢您的帮助。是的我首先在构建器部分尝试过它。它没有用。我也尝试在构建器部分使用subnet_id,并且打包器抱怨并说这些对于本节无效。我用Google搜索,有些人把它们放在变量部分,所以我尝试了,并没有得到错误说建设者属性不正确但我仍然得到了我的VPC错误。最后,我不得不联系亚马逊并让他们重新创建我已删除的DEFAULT VPC。我仍然无法将图像打包在默认VPC以外的VPC中。 - Daryl B
您需要指定associate_public_ip_address和subnet_id以使其在自定义VPC(非默认VPC)上工作 - David Roussel


添加:

    "associate_public_ip_address": "true",
    "ami_virtualization_type": "hvm",

清单为我工作。 这是一个示例文件:

{
  "variables": {
    "aws_access_key": "",
    "aws_secret_key": ""
  },
  "builders": [{
    "type": "amazon-ebs",
    "access_key": "{{user `aws_access_key`}}",
    "secret_key": "{{user `aws_secret_key`}}",
    "region": "eu-west-1",
    "source_ami": "ami-47a23a30",
    "instance_type": "t2.micro",
    "associate_public_ip_address": "true",
    "ami_virtualization_type": "hvm",
    "ssh_username": "ubuntu",
    "ami_name": "packer-exaple {{timestamp}}",
    "ami_description": "An example deployment built with Packer.io",
    "vpc_id": "vpc-XXXXX",
    "subnet_id": "subnet-XXXXX",
    "tags": {"Environment": "test",
             "name": "packer.io test"}
  }]
}

1
2018-04-15 09:07





如果在创建AMI期间尝试启动EC2的帐户中没有默认vpc,则需要添加vpc_id和subnet_id字段。这是我如何实现同样的目标。

"variables": {
    "aws_region": "us-west-2",
    "aws_subnet_id": "subnet-xxxxx",
    "aws_vpc_id": "vpc-xxxxx",
    "aws_ami_name": "CentOS-7-HVM-EBS-{{timestamp}}",
}

"builders": [{
    "vpc_id": "{{user `aws_vpc_id`}}",
    "subnet_id": "{{user `aws_subnet_id`}}",
    "type": "amazon-ebs",
    "region": "{{user `aws_region`}}",
    "instance_type": "t2.micro",
    "ssh_username": "centos",
     "ssh_timeout" : "10m",
    "ami_name": "{{user `aws_ami_name`}}",
    "ami_description": "Latest CentOS AMI with EBS backend on HVM",
    "source_ami_filter": {
        "filters": {
             "virtualization-type": "hvm",
              "name": "ops_aws_cent_7_*",
              "root-device-type": "ebs"
                    },
        "owners": ["xxxxxxxxxxx"],
        "most_recent": true
    }
}]

1
2018-06-12 14:08