https://pulumi.com logo
#aws
Title
a

alert-raincoat-81485

11/30/2020, 6:20 PM
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

billowy-army-68599

11/30/2020, 6:22 PM
you should check the cloud init logs on the instance
a

alert-raincoat-81485

11/30/2020, 6:24 PM
I checked those and no updates regarding userdata, i doubt it’s executing the userdata part.
b

billowy-army-68599

11/30/2020, 6:27 PM
can you see the userdata on the instance in the console?
a

alert-raincoat-81485

11/30/2020, 6:30 PM
No actually, it’s not running anyway, that’s the reason i am concerned if it’s right method to do so
b

billowy-army-68599

11/30/2020, 6:36 PM
can you print the output of
Copy code
aws ec2 describe-instance-attribute --attribute userData --instance-id <instance created with pulumi>
a

alert-raincoat-81485

11/30/2020, 6:38 PM
Sure, let me run the stack again .
Well it shows the value
g

gentle-diamond-70147

11/30/2020, 7:45 PM
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

alert-raincoat-81485

11/30/2020, 7:48 PM
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

gentle-diamond-70147

11/30/2020, 8:00 PM
Can you try a simpler script with
echo "Hiiii!"
or similar to see if that works?
a

alert-raincoat-81485

11/30/2020, 11:59 PM
Still didn’t work, not sure what’s wrong here.
g

gentle-diamond-70147

12/01/2020, 3:22 PM
What does your cloud init log say?
p

purple-arm-63328

12/16/2020, 6:10 PM
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)