Hello folks, I am trying to run a script through u...
# aws
a
Hello folks, I am trying to run a script through userdata as per the example shown but look like it’s not successfully running.
Copy code
user_data = """
#!/bin/bash

## Checking if Splunk user exist ##
if getent passwd splunk > /dev/null 2>&1;
then
    echo "splunk user exist"
else
    useradd -m splunk
    echo 'splunk    ALL=(ALL:ALL)   ALL' >> /etc/sudoers
    echo 'splunk    ALL=(ALL:ALL)   NOPASSWD' >> /etc/sudoers
fi
"""
Instances = aws.ec2.Instance(
                ins_name,
                user_data = user_data,
                ami = ami_centos.value,
                iam_instance_profile = iam_profile,
                instance_type = self.instance_type,
                subnet_id = sbnet,
                tags={"Name": ins_name},
            )
            instance_info = {
                "id": Instances.id,
                "primary_network_interface_id": Instances.primary_network_interface_id,
                "instance_name": ins_name,
            }
)
b
you should check the cloud init logs on the instance
a
I checked those and no updates regarding userdata, i doubt it’s executing the userdata part.
b
can you see the userdata on the instance in the console?
a
No actually, it’s not running anyway, that’s the reason i am concerned if it’s right method to do so
b
can you print the output of
Copy code
aws ec2 describe-instance-attribute --attribute userData --instance-id <instance created with pulumi>
a
Sure, let me run the stack again .
Well it shows the value
g
If you base64 decode that, it looks like there's some binary data at the end, so I think your input value for userdata might be invalid.
Also, I think the absolute first line of the value must start with
#!
e.g.
#!/bin/bash
and it looks like you have an empty first line.
a
Ohh that make sense @gentle-diamond-70147 I agree on the first line should be #!/bin/bash
I tried to put /bin/bash on first line, yet not success.
Well i have taken reference from this example, where it shows bin/bash starts from next line https://www.pulumi.com/docs/tutorials/aws/ec2-webserver/
g
Can you try a simpler script with
echo "Hiiii!"
or similar to see if that works?
a
Still didn’t work, not sure what’s wrong here.
g
What does your cloud init log say?
p
You need to encode the script with base64. In Python, use
base64.standard_b64encode(...)
.
(I never set it to the EC2 instance directly, maybe Pulumi encodes it at that point)