25

Is it possible to run python script through AWSCLI (user-data). I tried but it didn't run and i have following in my logs

boot.log:2015-08-07 10:08:30,660 - __init__.py[WARNING]: Unhandled non-multipart (text/x-not-multipart) userdata: './step-1
cloud-init.log:2015-08-07 10:08:30,660 - __init__.py[WARNING]: Unhandled non-multipart (text/x-not-multipart) userdata: './step-1'
cloud-init-output.log:2015-08-07 10:08:30,660 - __init__.py[WARNING]: Unhandled non-multipart (text/x-not-multipart) userdata: './step-1'

Note step-1 is my script which i am trying to pass as user-data . Also my script is present in same directory from where i am running command so it should pick

2
  • 2
    Can you post the actual command and parameters? Commented Aug 7, 2015 at 21:21
  • 3
    It seems that your script does not specify the shell it is supposed running in. I mean, #!/bin/bash e.t.c. Commented Oct 26, 2021 at 20:13

4 Answers 4

27

The default interpreter seems to be Python. So if you simply want to execute a shell script you'll need to start with a hash-bang, for example:

#!/bin/bash 
/yourpath/step-1

Please note, in order to debug this, try: cat /var/log/cloud-init-output.log And see: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html#user-data-shell-scripts

Sign up to request clarification or add additional context in comments.

1 Comment

The note on debug was very helpful!
3

You can use any command to be run under user-data. I have used user-data to bootstrap Windows Instances with Domain Controller setup or domain join using PowerShell; of course given that it is on EC2 - the properties are extensible whether you are running in Unix based or Windows Based.

So you have specified, Python - so please ensure the following

  1. Python already installed and then take an image - use that image to bootstrap
  2. You enable User-Data and pass the user-data commands during the launch time

6 Comments

"aws ec2 run-instances --image-id ami-XXXXX --count 1 --instance-type t2.micro --key-name PPPPPPP --security-group-ids LLLLL --subnet-id KKKKKK --user-data file://script.py" works on linux but not on windows any idea about it??
where did u place the script.py - which location ? Or trying giving the full path [ absolute path of the script.py ]
I have a doubt that if the AMI is enabled with User Data - can launch a plain ec2 instance and enable the user data and then take the ami and try using that AMI ?
enabling user-data is only for windows ? how do we enable it ?
|
1

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

the document from aws say that only shell and cloud-init directive that are supported

3 Comments

I believe userData is handled by cloud-init.
My case, I forgot start with "#!/bin/bash"
Like @rmsys, in my case, I missed the '#' character on the shebang.
0
Unhandled non-multipart (text/x-not-multipart) userdata: 'b'IyE

If you encounter this error in AL 2023, you might be double encoding the user data in base64. This was the case for me. See https://github.com/amazonlinux/amazon-linux-2023/issues/401

Boto docs also warn that you don't have to encode the user data

This value will be base64 encoded automatically. Do not base64 encode this value prior to performing the operation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.