Hello folks, I am trying to run the user_data whil...
# aws
a
Hello folks, I am trying to run the user_data while provisioning the instance, but it doesn’t run anything. The format i am using is
Copy code
user_data = """
#!/bin/bash
set -eux
sudo aws s3 cp s3://<bucket-name>/<script>.sh /tmp/
"""

Instances = aws.ec2.Instance(
                instance_name,
                ami=ami,
                instance_type=size,
                subnet_id=subnets[1],
                user_data_base64=user_data,
                security_groups=[securitygroup]
            )
If anyone can help to review!
l
You seem to be passing a simple UTF string in to the base64 version of the parameter?
You could either base64 encode your string, or use the
user_data
parameter.
a
I tried to use
user_data
parameter as well, but it’s the same thing
it doesn’t run anything from user_data
l
And the AMI you're using has bash installed at /bin?
a
Yes it does have bash preinstalled into the image.
b
i'm with tenwit, you need to base64 encoded your
user_data
string
and then check your cloud-init logs
if you look in the ec2 console can you see the user data set there?
a
Yes i logged into the instance and check but no data available.
l
a
No when i do curl to http://169.254.169.254/latest/user-data, it shows the userdata as of the userdata format. but doesn’t run anything.
Even i tried the userdata given as example
Copy code
user_data = """
#!/bin/bash
echo "Hello, World!" > index.html
nohup python -m SimpleHTTPServer 80 &
"""
it appears on metadata curl but it doesn’t run anything.
l
Hmmm.. as far as I know, there's nothing extra you need for user-data to be run. Can you run /bin/bash?
Maybe it's installed somewhere else on this AMI?
a
Well, i tried to search the contents using
find
but seems to be nowhere.
is there any specific ports needs to be open for ec2 to call this api? only the ports i have open is 443.
l
Search what contents?
a
contents of what i am installing through userdata
l
No afaik it's magic, and always accessible.. you can access it via curl, that's what happens on startup.
You won't be able to use
find
to find it, it's not stored locally. It's magically "beside" your machine.
a
Ah interesting,
l
I'd try using no shebang, or use /bin/sh. Have you tried running
/bin/bash
from the command line?
a
No well i am finding the script which is being copied from s3,
Copy code
user_data = """
#!/bin/bash
set -eux
sudo aws s3 cp s3://<bucket-name>/<script>.sh /tmp/
"""
it’s appearing nowhere.
l
But you did the
curl
from the instance, right?
So getting the script isn't a problem. It's likely that running the script is the problem.
a
Also in this example, it should create
index.html
however i am seeing it using curl metadata but can’t see
index.htmml
being created anywhere.
Copy code
user_data = """
#!/bin/bash
echo "Hello, World!" > index.html
nohup python -m SimpleHTTPServer 80 &
"""
l
And one possibility is that
/bin/bash
is failing to run.
Each of these scripts is reliant on
/bin/bash
. Have you checked that you can invoke
/bin/bash
?
a
Yes, i am able to invoke it from the instance. I checked that earlier.
l
Ok that's good to know.
a
Everything looks fine when i run the same script from within in instance, it just doesn’t work with userdata script.
l
Have you had a look in /var/log/cloud-init.log?
And in /var/lib/cloud/scripts/*... maybe there's something odd in there...
a
Sure, checking in.
p
I had the same problem when using the Ubuntu AMI. When you write
user_data
like this:
Copy code
user_data = """
...
"""
it leaves a leading new line which for some reason doesn't work with Ubuntu (at least 20.04). It works with Amazon Linux, though. I had to remove the empty new line by writing the
user_data
like:
Copy code
user_data = """\
...
"""
(note the trailing
\
backslash).
😮 2
🆒 1
a
@proud-art-41399 That seems interesting, I should try that way.