https://pulumi.com logo
#general
Title
# general
b

brash-restaurant-84207

09/29/2022, 8:02 PM
Hi, I have the following code: user_data = """ #!/bin/bash whoami >> /script_output apt-get update -y >> script_output apt-get install awscli -y >> script_output apt-get install nginx -y >> script_output mkdir -p /var/lib/cloud/scripts/per-boot/ """ ec2_instance = aws.ec2.Instance("ec2_instance", ami=ubuntu.id, instance_type = "t3.medium", subnet_id = vpc.public_subnet_ids[0], key_name = "my-kp", vpc_security_group_ids = [my_sec_group.id], user_data=user_data, user_data_replace_on_change=True, tags={ "Name": "ec2_instance", }) pulumi creates the instance as defined but does not run the user_data for some reason. why would this be?
b

billowy-army-68599

09/29/2022, 8:04 PM
base64 encode it first, it’s usually an error with whitespace
b

brash-restaurant-84207

09/29/2022, 8:13 PM
just tried that... no change...
b

billowy-army-68599

09/29/2022, 8:14 PM
what are the cloud-init logs telling you?
b

brash-restaurant-84207

09/29/2022, 8:18 PM
2022-09-29 201146,180 - subp.py[DEBUG]: Exec format error. Missing #! in script?
b

billowy-army-68599

09/29/2022, 8:21 PM
I think it’s this line
Copy code
whoami >> /script_output
but your script isn’t valid, in any case
b

brash-restaurant-84207

09/29/2022, 8:23 PM
this script works when passed as user_data to terraform... I'll try creating a text file before that line to check...
I tried inserting 'touch /testfile.txt' to the start of the script but it didn't work either....
tried this but still not working:
user_data = """ #!/bin/bash echo "Hello, world!" > index.html """
I opened the file: /var/lib/cloud/instances/i-0b74ab797ee4ba6b6/scripts/part-001
which included the new script above, however the first line was blank and the second line was: '#!/bin/bash'
this appears to be the reason that the script is not running... but I'm not sure why the script is being read with a leading carriage return
b

billowy-army-68599

09/29/2022, 8:43 PM
are you using python? or ts?
b

brash-restaurant-84207

09/29/2022, 8:46 PM
python
ok I worked it out... needs to be on the first line of user_data: user_data = """#!/bin/bash
k

kind-jelly-61624

09/29/2022, 9:25 PM
out of curiosity why are you writing the stdout to script_output? user_data redirects all output to /var/log/user-data.log by default
b

brash-restaurant-84207

09/29/2022, 10:01 PM
I don't see a file by that name in that path...
k

kind-jelly-61624

09/29/2022, 10:04 PM
b

brash-restaurant-84207

10/02/2022, 8:07 AM
doesn't show the same level of info as I'm writing to a separate log and I also add additional comments in the log to help follow the flow
🙌 1
p

proud-art-41399

10/02/2022, 11:58 AM
I encountered this as well, but only when using Ubuntu AMI, it works OK with Amazon Linux. I had to use
Copy code
user_data = """\
#!/bin/bash
...
👍 1