Confused about ami_root_device option for amazon-ebssurrogate

1,123 views
Skip to first unread message

Hasan Saqib

unread,
Jul 9, 2018, 6:10:14 PM7/9/18
to Packer
Hello everyone,

I'm trying to create an EC2 AMI in Packer that is based off an existing AWS provided image which will be modified by my hardening/provision script. I want to use the amazon-ebssurrogate builder because I will be partitioning and mounting some directories. What is a little confusing is how I can supply the snapshot ID of the existing AWS AMI in the ami_root_device parameter. Currently, my template file has this:

    "ami_root_device": {
         "delete_on_termination": true,
         "device_name": "/dev/xvda",
         "volume_size" : 80,
         "volume_type": "gp2",
         "source_device_name": "/dev/xvdn"
    },


 I provide the existing AMI in the "source_ami" parameter. However, when the instance comes up, I can see that the root AMI device is based on the existing AMI id I provided BUT, the attached EBS volume (/dev/xvdn) is created as an empty volume. When I tried to include the "snapshot_id" parameter in the ami_root_device, I see 

* unknown configuration key: "ami_root_device.snapshot_id"

Can anyone see what I am messing up on and how I can get the attached volume to be the base existing AMI as well?

Many thanks.

John Roh

unread,
Jul 10, 2018, 1:10:52 AM7/10/18
to Packer

Please check the example showing at "AMI Block Device Mappings Example" @ https://www.packer.io/docs/builders/amazon-ebs.html


I think your device_name mapping should be followed shown at here, https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/device_naming.html
Based on your hard drive virtualization types, whether it is hvm or Paravirtual, your device name needs to be mapped. For example, I use the packer to build as below.

"source_ami_filter": {
"filters": {
"virtualization-type": "hvm",
"name": "your ami image name",
"root-device-type": "ebs"
},
"owners": ["your account id"],
"most_recent": true
},
"ami_block_device_mappings" : [
{
"device_name": "/dev/sdf",
"delete_on_termination": true,
"encrypted": true,
"volume_size": 50,
"volume_type": "gp2"
}

Hope that it helps you a bit.

John.

Rickard von Essen

unread,
Jul 10, 2018, 4:23:14 AM7/10/18
to packe...@googlegroups.com
I haven't used amazon-ebssurrogate but I think you should use launch_block_device_mappings to add the snapshot you want to a device. And then reference that device as source_device in your root_device_mapping.

This will launch an instance with the snapshot and save it as the root device for the new AMI after provisioning. 

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/packer/issues
IRC: #packer-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Packer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/packer-tool/9629f436-2277-4a71-b4a1-0e4e2b5db1c9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hasan Saqib

unread,
Jul 10, 2018, 6:06:17 PM7/10/18
to Packer
Thank you John and Rickard for your replies. I am indeed able to get the snapshot I want by adding the launch block device mapping, like so:

    "launch_block_device_mappings": [
        {
             "device_name": "/dev/xvdn",
             "snapshot_id": "snap-069e44139fea1ae25",
             "delete_on_termination": true,
             "volume_size": 80,
             "volume_type": "gp2"
        }
    ],
    "ami_root_device": {
         "delete_on_termination": true,
         "device_name": "/dev/xvdn",
         "volume_size" : 80,
         "volume_type": "gp2",
         "source_device_name": "/dev/xvdn"
    },

The issue now is that Packer launches the instance with /dev/xvdn as the root device of the instance and I see /dev/xvda as another attached volume like below:

NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvdn    202:208  0  80G  0 disk
└─xvdn1 202:209  0  80G  0 part /
xvda    202:0    0   8G  0 disk
└─xvda1 202:1    0   8G  0 part



What I want is the /dev/xvdn as the additional attached volume to run my script on and have Packer take an AMI of /dev/xvdn when I'm done. Is there a way of achieving that?

Thanks .

Rickard von Essen

unread,
Jul 11, 2018, 10:41:06 AM7/11/18
to packe...@googlegroups.com
What's the out put of describe-image for the source ami?
> --
> This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
>
> GitHub Issues: https://github.com/mitchellh/packer/issues
> IRC: #packer-tool on Freenode
> ---
> You received this message because you are subscribed to the Google Groups "Packer" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/packer-tool/d9937bf3-c435-487e-ad74-c67a04e42331%40googlegroups.com.

Hasan Saqib

unread,
Jul 11, 2018, 12:08:49 PM7/11/18
to Packer
Hi Rickard,

If you're talking about the AMI I use in the "source_ami" parameter, then it is an AWS provided Beanstalk AMI. Here is the output:

{
    "Images": [
        {
            "VirtualizationType": "hvm",
            "Name": "aws-elasticbeanstalk-amzn-2018.03.0.x86_64-tomcat8java8-hvm-201806212038",
            "Hypervisor": "xen",
            "ImageOwnerAlias": "amazon",
            "SriovNetSupport": "simple",
            "ImageId": "ami-822671fd",
            "State": "available",
            "BlockDeviceMappings": [
                {
                    "DeviceName": "/dev/xvda",
                    "Ebs": {
                        "DeleteOnTermination": true,
                        "SnapshotId": "snap-069e44139fea1ae25",
                        "VolumeSize": 8,
                        "VolumeType": "gp2",
                        "Encrypted": false
                    }
                }
            ],
            "Architecture": "x86_64",
            "ImageLocation": "amazon/aws-elasticbeanstalk-amzn-2018.03.0.x86_64-tomcat8java8-hvm-201806212038",
            "RootDeviceType": "ebs",
            "OwnerId": "102837901569",
            "RootDeviceName": "/dev/xvda",
            "CreationDate": "2018-06-21T20:40:41.000Z",
            "Public": true,
            "ImageType": "machine"
        }
    ]
}




Thanks

Rickard von Essen

unread,
Jul 15, 2018, 4:17:19 PM7/15/18
to packe...@googlegroups.com
I haven't had any time to look further into this but I think you are doing everything the right way. The only thing I don't understand is why this happens:

The issue now is that Packer launches the instance with /dev/xvdn as the root device of the instance and I see /dev/xvda as another attached volume like below:

The root device should only change after you have provisioning have been completed and the new AMI is created. 

Reply all
Reply to author
Forward
0 new messages