amazon-ec2terraformcloud-init

cloud-init: delay disk_setup and fs_setup


I have a cloud-init file that sets up all requirements for our AWS instances, and part of those requirements is formating and mounting an EBS volume. The issue is that on some instances volume attachment occurs after the instance is up, so when cloud-init executes the volume /dev/xvdf does not yet exist and it fails.

I have something like:

#cloud-config

resize_rootfs: false
disk_setup:
    /dev/xvdf:
        table_type: 'gpt'
        layout: true
        overwrite: false

fs_setup:
    - label: DATA
      filesystem: 'ext4'
      device: '/dev/xvdf'
      partition: 'auto'

mounts:
    - [xvdf, /data, auto, "defaults,discard", "0", "0"]

And would like to have something like a sleep 60 or something like that before the disk configuration block.

If the whole cloud-init execution can be delayed, that would also work for me.

Also, I'm using terraform to create the infrastructure.

Thanks!


Solution

  • I was able to resolve the issue with two changes:

    1. Changed the mount options, adding nofail option.
    2. Added a line to the runcmd block, deleting the semaphore file for disk_setup.

    So my new cloud-init file now looks like this:

    #cloud-config
    
    resize_rootfs: false
    disk_setup:
        /dev/xvdf:
            table_type: 'gpt'
            layout: true
            overwrite: false
    
    fs_setup:
        - label: DATA
          filesystem: 'ext4'
          device: '/dev/xvdf'
          partition: 'auto'
    
    mounts:
        - [xvdf, /data, auto, "defaults,discard", "0", "0"]
        
    runcmd:
        - [rm, -f, /var/lib/cloud/instances/*/sem/config_disk_setup]
    
    power_state:
        mode: reboot
        timeout: 30
    

    It will reboot, then it will execute the disk_setup module once more. By this time, the volume will be attached so the operation won't fail.

    I guess this is kind of a hacky way to solve this, so if someone has a better answer (like how to delay the whole cloud-init execution) please share it.