Python Boto created cloud-init #include script not running on EC2
Question: I've run out of ideas, would appreciate some help.

I'm starting and EC2 Ubuntu 12.04 instance and adding the following script to
the user data:

    
    
    #!/usr/bin/env python
    
    import sys
    
    from boto.s3.connection import S3Connection
    
    AWS_BOOTSTRAP_BUCKET = 'myBucket'
    AWS_ACCESS_KEY_ID = 'MyAccessId'
    AWS_SECRET_ACCESS_KEY = 'MySecretKey'
    
    s3 = S3Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
    install = s3.generate_url(300, 'GET', bucket=AWS_BOOTSTRAP_BUCKET, key='bash1.txt',     force_http=True)
    config = s3.generate_url(300, 'GET', bucket=AWS_BOOTSTRAP_BUCKET, key='cloud-config.txt', force_http=True)
    start = s3.generate_url(300, 'GET', bucket=AWS_BOOTSTRAP_BUCKET, key='bash2.txt', force_http=True)
    
    sys.stdout.write("#include\n")
    sys.stdout.write(install+"\n")
    sys.stdout.write(config+"\n")
    sys.stdout.write(start+"\n")
    

After the instance has started, I can right click on the instance and View Sys
Log.

I can see the following near the bottom:

    
    
    Generating locales...
    en_US.UTF-8... done
    Generation complete.
        #include
    http://nerdcloudinit.s3.amazon...
    http://nerdcloudinit.s3.amazon...
    http://nerdcloudinit.s3.amazon...
    

I can run wget from the instance on the provided url's and see the contents of
the txt files.

Why aren't the scripts added via #include working? Any help would be
appreciated.

Kind regards, C


Answer: You've got the right idea, you're just one layer too deep :)

With Ubuntu and cloud-init, your user data can be an executable script OR it
can be a CloudConfig file.

In your question, you are passing a python executable as your userdata. On the
Ubuntu machine, it sees an executable program (because it starts with `#!`)
and it runs it. And that's all it will do. It won't process the output of your
program.

What you want, is to run the python script on YOUR computer, and use the
OUTPUT of the program as your userdata.

So in your example, the userdata passed to Amazon should be:

    
    
    #include
    http://nerdcloudinit.s3.amazon...
    http://nerdcloudinit.s3.amazon...
    http://nerdcloudinit.s3.amazon...
    

cloud-init on Ubuntu will see the first line `#include` and know that it needs
to download the files. Then, it will look at each file it downloads to process
each one.

The mime-multipart is only useful if you want to lump multiple files together
into one. For example instead of downloading three files from S3, you could
put them all together into one file and `#include` a single URL. You don't
need that here though.

The link you included for cloud-init docs
<http://help.ubuntu.com/community/CloudInit> has more examples of what your
userdata can contain.

Enjoy!

