vavavr00m / boto

Automatically exported from code.google.com/p/boto
1 stars 0 forks source link

user_data argument does not work in run_instances #432

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I have written a piece of code to connect to EC2 and lunch a new instance and I 
want a bash script to run at start up of the instance.

ec2 = boto.connect_ec2(KEY, SECRET)
reservation = ec2.run_instances(image_id=AMI_ID,
                                key_name=KEY_NAME,
                                security_groups=SECURITY_GROUP,
                                user_data = """#!/bin/bash
cd /root
mkdir rozita
""",
                                instance_type=INSTANCE_TYPE)

As you can see it is a simple code to change directory to /root and make a 
directory there. As soon as instance becomes running, I connect to it via Putty 
and go to /root path but it seems the requested directory is not created there 
and the shell script is not executed at start up.

Would you please help me?

Thanks,
Rozita

Original issue reported on code.google.com by rozita.r...@gmail.com on 8 Aug 2010 at 5:58

GoogleCodeExporter commented 9 years ago
I have used user_data extensively with boto so I'm pretty sure the 
functionality should be working.  What are you using to try to retrieve the 
user_data on the instance?  The boto.utils module has a function called 
get_instance_userdata.  Have you tried using that?  

Original comment by Mitch.Ga...@gmail.com on 8 Aug 2010 at 10:44

GoogleCodeExporter commented 9 years ago
Thanks for you reply. 

I used get_instance_userdata() function from boto.utils but it did not work 
too. Here is my whole code that I run in my local. Would you please check it 
and let me know what is the problem with it that it does not work and my shell 
script passed as user_data to the instance does not work?

        import time
        try:
            import boto
            import boto.utils

            ec2 = boto.connect_ec2(KEY, SECRET)
            reservation = ec2.run_instances(image_id=AMI_ID,
                                        key_name=KEY_NAME,
                                        security_groups=SECURITY_GROUP,
                                        user_data = """#!/bin/bash
cd /root
mkdir rozita
""",
                                        instance_type=INSTANCE_TYPE)            

            instance = reservation.instances[0]
            instance.update()
            while not instance.state == "running":
                time.sleep(30)
                instance.update()

            print boto.utils.get_instance_userdata()

            instance.stop()

        except Exception,e:
            print str(e)

Original comment by rozita.r...@gmail.com on 10 Aug 2010 at 7:54

GoogleCodeExporter commented 9 years ago
Issue 437 has been merged into this issue.

Original comment by Mitch.Ga...@gmail.com on 14 Aug 2010 at 2:08

GoogleCodeExporter commented 9 years ago
 think there may be some confusion about the userdata.  It can only be access from the actual EC2 instance.  There is no way to access the special metadata server from another machine.  So, basically, you pass the userdata to the instance and then the instance can access it and do whatever you want to do with it.  If you want to pass in a script and have that run automatically you will either have to create a custom AMI that adds something to rc.local (or equivalent) to run your script or you could try using an Ubuntu image which has built in support for something they call CloudInit:

https://help.ubuntu.com/community/CloudInit

Does that help?

Mitch

Original comment by Mitch.Ga...@gmail.com on 22 Aug 2010 at 12:41

GoogleCodeExporter commented 9 years ago

Original comment by Mitch.Ga...@gmail.com on 22 Aug 2010 at 12:41