User-data scripts is not running on my custom AMI, but working in standard Amazon linux

后端 未结 11 1357
南方客
南方客 2020-12-08 02:15

I searched a lot of topic about \"user-data script is not working\" in these few days, but until now, I haven\'t gotten any idea about my case yet, please help me to figure

11条回答
  •  一整个雨季
    2020-12-08 02:21

    I was having a lot of trouble with this. I'll provide detailed walk-though.

    My added wrinkle is that I'm using terraform to instantiate the hosts via a launch configuration and autoscaling group.

    I could NOT get it to work by adding the script inline in lc.tf

    user_data                   = DATA <<
    "
    #cloud-boothook
    #!/bin/bash
    echo 'some crap'\'
    "
    DATA
    

    I could fetch it from user data,

    wget http://169.254.169.254/latest/user-data
    

    but noticed I was getting it with the quotes still in it.

    This is how I got it to work: I moved to pulling it from a template instead since what you see is what you get.

    user_data                   = "${data.template_file.bootscript.rendered}"
    

    This means I also need to declare my template file like so:

    data "template_file" "bootscript" {
      template = "${file("bootscript.tpl")}"
    
    }
    

    But I was still getting an error in the cloud init logs /var/log/cloud-init.log [WARNING]: Unhandled non-multipart (text/x-not-multipart) userdata: 'Content-Type: text/cloud...'

    Then I found this article about user data formatting user That makes sense, if user-data can come in multiple parts, maybe cloud-init needs cloud-init commands in one place and the script in the other.

    So my bootscript.tpl looks like this:

    Content-Type: multipart/mixed; boundary="//"
    MIME-Version: 1.0
    
    --//
    Content-Type: text/cloud-config; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    Content-Disposition: attachment; filename="cloud-config.txt"
    
    #cloud-config
    cloud_final_modules:
    - [scripts-user, always]
    
    --//
    Content-Type: text/x-shellscript; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    Content-Disposition: attachment; filename="userdata.txt"
    
    #!/bin/bash
    echo "some crap"
    --//
    

提交回复
热议问题