Search code examples
amazon-web-servicesamazon-ec2chef-infrachef-recipepacker

unable to create folder on /mnt via chef recipe


I am using the following chef command to create a folder on /mnt

directory '/mnt/node/deploy' do
  owner 'ubuntu'
  group 'ubuntu'
  mode '0755'
  recursive true
  action :create
end

This is a part of a recipe which is invoked via packer to create an AWS AMI. ubuntu is the user that I use to deploy my code to a provisioned machine. When I launch an EC2 instance using the AMI, this folder is not created on the machine. What could be the problem? I see no errors when the AMI is created.

Update -1

These are the logs. I tried using root.

`amazon-ebs: * directory[/mnt/node/deploy] action create`
`amazon-ebs: - create new directory /mnt/node/deploy`
`amazon-ebs: - change mode from '' to '0755'`
`amazon-ebs: - change owner from '' to 'root'`
`amazon-ebs: - change group from '' to 'root'`

I see that EC2 is mounting ephemeral storage on /mnt. I want to create these folders on the ephemeral storage. I unmounted /mnt, but did not see the folders there.


Solution

  • Packer runs Chef before creating the image. So, if I understand you correctly:

    1. Chef creates the directory on an instance ephemeral storage.
    2. Packer creates the AMI.
    3. You start the AMI and the directory does not exist in the ephemeral storage.

    AFAIK that's an expected behavior. The directory is created in a partition that is ephemeral and this kind of partitions are not expected to endure.

    Summarizing, when you create an AWS AMI image, it does not include the ephemeral storage. Only the EBS volumes. Ephemeral partitions are always empty at startup. If you want to retain that directory, it must be in a EBS partition.

    If you still want to use the /mnt directory, you can avoid mounting the ephemeral storage with the ami_block_device_mappings option:

      "ami_block_device_mappings": [
        {
          "device_name": "/dev/sdb",
          "no_device": true
        }
      ],
    

    And the same for the launch_block_device_mappings

    Another solution could be to run your Chef cookbook again in the newly created instance.