Packer unable to pack aws image into VPC

Solution 1:

Exactly as you say, there is a vpc_id option that is pointed out in the documentation of the amazon-ebs builder. You have added this option to your Packer JSON file, however, you added it in the wrong place.

The vpc_id option should be added in your builder object and not in the variables object. So it should look something like this:

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

        [...]
    }],
}

Solution 2:

adding:

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

to the manifest worked for me. Here's an example file:

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

Solution 3:

You need to add both vpc_id and subnet_id fields in case you dont have default vpc in the account where you are trying to launch the EC2 during the AMI creation. Here is how I have achieved the same.

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