https://pulumi.com logo
Title
a

alert-raincoat-81485

02/08/2021, 9:35 PM
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
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

little-cartoon-10569

02/08/2021, 9:37 PM
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

alert-raincoat-81485

02/08/2021, 9:40 PM
I tried to use
user_data
parameter as well, but it’s the same thing
it doesn’t run anything from user_data
l

little-cartoon-10569

02/08/2021, 9:41 PM
And the AMI you're using has bash installed at /bin?
a

alert-raincoat-81485

02/08/2021, 9:44 PM
Yes it does have bash preinstalled into the image.
b

billowy-army-68599

02/08/2021, 9:45 PM
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

alert-raincoat-81485

02/08/2021, 9:45 PM
Yes i logged into the instance and check but no data available.
l

little-cartoon-10569

02/08/2021, 9:46 PM
a

alert-raincoat-81485

02/09/2021, 1:40 AM
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
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

little-cartoon-10569

02/09/2021, 1:41 AM
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

alert-raincoat-81485

02/09/2021, 1:43 AM
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

little-cartoon-10569

02/09/2021, 1:44 AM
Search what contents?
a

alert-raincoat-81485

02/09/2021, 1:44 AM
contents of what i am installing through userdata
l

little-cartoon-10569

02/09/2021, 1:44 AM
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

alert-raincoat-81485

02/09/2021, 1:45 AM
Ah interesting,
l

little-cartoon-10569

02/09/2021, 1:45 AM
I'd try using no shebang, or use /bin/sh. Have you tried running
/bin/bash
from the command line?
a

alert-raincoat-81485

02/09/2021, 1:46 AM
No well i am finding the script which is being copied from s3,
user_data = """
#!/bin/bash
set -eux
sudo aws s3 cp s3://<bucket-name>/<script>.sh /tmp/
"""
it’s appearing nowhere.
l

little-cartoon-10569

02/09/2021, 1:46 AM
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

alert-raincoat-81485

02/09/2021, 1:47 AM
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.
user_data = """
#!/bin/bash
echo "Hello, World!" > index.html
nohup python -m SimpleHTTPServer 80 &
"""
l

little-cartoon-10569

02/09/2021, 1:47 AM
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

alert-raincoat-81485

02/09/2021, 1:48 AM
Yes, i am able to invoke it from the instance. I checked that earlier.
l

little-cartoon-10569

02/09/2021, 1:48 AM
Ok that's good to know.
a

alert-raincoat-81485

02/09/2021, 1:49 AM
Everything looks fine when i run the same script from within in instance, it just doesn’t work with userdata script.
l

little-cartoon-10569

02/09/2021, 1:50 AM
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

alert-raincoat-81485

02/09/2021, 1:52 AM
Sure, checking in.
p

proud-art-41399

02/09/2021, 6:42 AM
I had the same problem when using the Ubuntu AMI. When you write
user_data
like this:
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:
user_data = """\
...
"""
(note the trailing
\
backslash).
😮 2
🆒 1
a

alert-raincoat-81485

03/05/2021, 7:50 PM
@proud-art-41399 That seems interesting, I should try that way.