How do I set up cloud-init on custom AMIs in AWS? (CentOS)

后端 未结 3 664
死守一世寂寞
死守一世寂寞 2020-12-04 10:31

Defining userdata for instances in AWS seems really useful for doing all kinds of bootstrap-type actions. Unfortunately, I have to use a custom CentOS AMI that didn\'t origi

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-04 11:08

    Expanding on the prior answer for anyone trying to create a CentOS AMI that is cloud-init enabled (and capable of actually executing your CloudFormation scripts), you might have some success by doing the following:

    1. launch a marketplace CentOS AMI w/Updates - make sure cloud-init is present or sudo yum install -y cloud-init
    2. rm -rf /var/lib/cloud/data
    3. rm -rf /var/lib/cloud/instance
    4. rm -rf /var/lib/cloud/instances/*
    5. replace /etc/cloud/cloud.cfg with the configuration in the answer above but make sure you set distro: rhel
    6. Add the CloudFormation helpers (http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-helper-scripts-reference.html)
    7. create an AMI image from this instance

    Had a heck of a time trying to figure out why my UserData was not being invoked until I realized that the images in the marketplace naturally only run your UserData once per instance AND of course they had already run. Removing the indicators that those had already been executed along with changing the distro: rhel in the cloud.cfg file did the trick.

    For the curious, the distro: value should correspond to one of the python scripts in /usr/lib/python2.6/site-packages/cloudinit/distros. As it turns out the AMI I launched had no amazon.py, so you need to use rhel for CentOS. Depending on the AMI you launch and the version of cloud-init, YMMV.

提交回复
热议问题