I am trying to launch an instance, have a script run the first time it launches as part of userdata. The following code was used (python boto3 library):
import boto3
ec2 = boto3.resource('ec2')
instance = ec2.create_instances(DryRun=False, ImageId='ami-abcd1234', MinCount=1, MaxCount=1, KeyName='tde', Placement={'AvailabilityZone': 'us-west-2a'}, SecurityGroupIds=['sg-abcd1234'], UserData=user_data, InstanceType='c3.xlarge', SubnetId='subnet-abcd1234')
I have been playing around with the user_data and have had no success. I have been trying to echo some string to a new file in an existing directory. Below is the latest version I attempted.
user_data = '''
#!/bin/bash
echo 'test' > /home/ec2/test.txt
'''
The ami is a CentOS based private AMI. I have tested the commands locally on the server and gotten them to work. But when I put the same command on the userdata (tweaked slightly to match the userdata format), it does not work. Instance launches successfully but the file I specified is not present.
I looked at other examples (https://github.com/coresoftwaregroup/boto-examples/blob/master/32-create-instance-enhanced-with-user-data.py) and even copied their commands.
Your help is appreciated! Thanks :)
For User Data to be recognized as a script, the very first characters MUST be #!
(at least for Linux instances).
However, your user_data
variable is being defined as:
"\n #!/bin/bash\n echo 'test' > /home/ec2/test.txt\n "
You should define it like this:
user_data = '''#!/bin/bash
echo 'test' > /tmp/hello'''
Which produces:
"#!/bin/bash\necho 'test' > /tmp/hello"
That works correctly.
So, here's the final product:
import boto3
ec2 = boto3.resource('ec2')
user_data = '''#!/bin/bash
echo 'test' > /tmp/hello'''
instance = ec2.create_instances(ImageId='ami-abcd1234', MinCount=1, MaxCount=1, KeyName='my-key', SecurityGroupIds=['sg-abcd1234'], UserData=user_data, InstanceType='t2.nano', SubnetId='subnet-abcd1234')
After logging in:
[ec2-user@ip-172-31-2-151 ~]$ ls /tmp
hello hsperfdata_root
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With