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

后端 未结 11 1335
南方客
南方客 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:33

    !/bin/bash

    Do not leave any white space at the start of the line . Use exact command. Otherwise it may run in AMAZON Linux AMI , but it’ll not run in RHEL.

    0 讨论(0)
  • 2020-12-08 02:36

    User_data is run only at the first start up. As your image is a custom one, I suppose it have already been started once and so user_data is desactivated.

    For windows, it can be done by checking a box in Ec2 Services Properties. I'm looking at the moment how to do that in an automated way at the end of the custom image creation.

    For linux, I suppose the mechanism is the same, and user_data needs to be re-activated on your custom image.

    The #cloud-boothook make it works because it changes the script from a user_data mechanism to a cloud-boothook one that runs on each start.


    EDIT :

    Here is the code to reactivate start on windows using powershell:

    $configFile = "C:\\Program Files\\Amazon\\Ec2ConfigService\\Settings\\Config.xml"
    [xml] $xdoc = get-content $configFile
    $xdoc.SelectNodes("//Plugin") |?{ $_.Name -eq "Ec2HandleUserData"} |%{ $_.State = "Enabled" }
    $xdoc.SelectNodes("//Plugin") |?{ $_.Name -eq "Ec2SetComputerName"} |%{ $_.State = "Enabled" }
    $xdoc.OuterXml | Out-File -Encoding UTF8 $configFile
    
    $configFile = "C:\\Program Files\\Amazon\\Ec2ConfigService\\Settings\\BundleConfig.xml"
    [xml] $xdoc = get-content $configFile
    $xdoc.SelectNodes("//Property") |?{ $_.Name -eq "AutoSysprep"} |%{ $_.Value = "Yes" }
    $xdoc.OuterXml | Out-File -Encoding UTF8 $configFile
    

    (I know the question focus linux, but it could help others ...)

    0 讨论(0)
  • 2020-12-08 02:39

    As I tested, there were some bootstrap data in /var/lib/cloud directory. After I cleared that directory, User Data script worked normally.

    rm -rf /var/lib/cloud/*
    
    0 讨论(0)
  • 2020-12-08 02:41

    this is the answer as an example: ensure that you have in the headline only #!/bin/bash

    #!/bin/bash
    yum update -y
    yum install httpd mod_ssl
    service httpd start
    chkconfig httpd on
    
    0 讨论(0)
  • 2020-12-08 02:42

    The only way I get it to work was to add the #cloud-boothook before the #!/bin/bash

    This is a typical user data script that installs Apache web server on a newly created instance

    #cloud-boothook
    #!/bin/bash
    
    yum update -y 
    yum install -y httpd.x86_64 
    systemctl start httpd.service 
    systemctl enable httpd.service 
    

    without the #cloud-boothook it does not work, but with it, it works. It seems that different users have different experiences. Some are able to get it to work without it, not sure why.

    0 讨论(0)
提交回复
热议问题