Packer 无法将 AWS 镜像打包到 VPC 中

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"
  }],
}

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

答案1

正如你所说,有一个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",

        [...]
    }],
}

答案2

添加:

    "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"}
  }]
}

答案3

如果您在创建 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
    }
}]

相关内容