Amazon ec2 user-data, how does it work?

前端 未结 5 901
别那么骄傲
别那么骄傲 2020-12-23 13:11

We are starting instances, and accessing the user-data we place. But does anybody understand the internals of this operation (from Amazon\'s side)? When we pass in the user-

相关标签:
5条回答
  • 2020-12-23 13:42

    Sorry to post to such an old question, but this seems like the best place to put this additional piece of information.

    Most all the AWS documents describe User Data as a property in which to put instance lifecycle startup scripting, that is, the stuff you want to run only when the instance first launches.

    This is usually the case, but there has been at least one other person besides myself wanting to perform different scripting on restart, say to fix a broken key or something. And guess what... you can do that using User Data.

    Here is the code and the link to the AWS document...

    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
    /bin/echo "Hello World." >> /tmp/sdksdfjsdlf
    --//
    

    I can find no documentation on this formatting of User Data that allows this to happen. I've tried it out and it works. I have tried to see if it runs on every startup, and it does.

    So, if you think you need to do this, I recommend that you backup. Make sure you have a copy of the original User Data, and use the code provided modified to suite, and remove the code upon the next time you stop the instance (to avoid multiple runs of the script).

    0 讨论(0)
  • 2020-12-23 13:55

    An easy example for everyone's understanding: If you want to create the file /tmp/testfile.txt when the machine gets started, you can simply add these two lines on the User data field.

    #!/bin/bash
    touch /tmp/testfile.txt
    

    Remember to put the #!/bin/bash at the top before your commands.

    When you run the instance (Linux AMI), you can see the User data field content at /var/lib/cloud/instance/user-data.txt

    0 讨论(0)
  • 2020-12-23 13:56
    #!/bin/bash
    yum update -y
    yum install httpd -y
    echo "<html><h1>webpage 1(whatever you want, give the page name here)</h1></html>"
    /var/www/html/index.html
    service httpd start
    chkconfig httpd on
    
    0 讨论(0)
  • 2020-12-23 13:58

    The user-data is available to the instance with a simple HTTP request at this URL:

    http://169.254.169.254/latest/user-data
    

    Amazon EC2 does not put this user-data on the instance directly, though many AMIs have code that instructs the instance to download and process the user-data automatically.

    See also:

    • http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html

    • http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html

    0 讨论(0)
  • 2020-12-23 14:01

    AWS userdata is the set of commands/data you can provide to a instance at launch time. For example if you are launching an ec2 instance and want to have docker installed on the newly launched ec2, than you can provide set of bash commands in the userdata field of aws ec2 config page.

    Usecase

    • Automated deployments

    • Orchestrating newly launched instance

    • Bootstrapping newly launched instance with chef

    • AWS Autoscaling

    Here is a well explained example of AWS userdata with video tutorial

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