I am trying to run a python script that can start an instance and then run some commands. I know it can be passed if I try to create an instance in the form of userdata. What I am trying to figure out is how to pass it if I am starting an already created instance. Following is the code using boto3 to pass simple hello world while creating an instance:
import boto3
userdata = """#cloud-config
repo_update: true
repo_upgrade: all
packages:
- s3cmd
runcmd:
- echo 'Hello S3!' > /tmp/hello.txt
- aws --region YOUR_REGION s3 cp /tmp/hello.txt s3://YOUR_BUCKET/hello.txt
"""
ec2 = boto3.resource('ec2')
instances = ec2.create_instances(
ImageId='ami-f5f41398', # default Amazon linux
InstanceType='t2.micro',
KeyName='YOUR_SSH_KEY_NAME',
MinCount=1,
MaxCount=1,
IamInstanceProfile={
'Arn': 'YOUR_ARN_ID'
},
SecurityGroupIds=['YOUR_SECURITY_GROUP_NAME'],
UserData=userdata
)
Something like
i = ec2.Instance(id='i-5fea4d42')
i.start('pass commands here: eg echo xx, mv a b/ etc')