Hi, I have the following code: user_data = """ #!...
# general
b
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
base64 encode it first, it’s usually an error with whitespace
b
just tried that... no change...
b
what are the cloud-init logs telling you?
b
2022-09-29 201146,180 - subp.py[DEBUG]: Exec format error. Missing #! in script?
b
I think it’s this line
Copy code
whoami >> /script_output
but your script isn’t valid, in any case
b
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
are you using python? or ts?
b
python
ok I worked it out... needs to be on the first line of user_data: user_data = """#!/bin/bash
k
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
I don't see a file by that name in that path...
k
b
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
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