2016-11-17 84 views
0

我想運行一個python腳本,可以啓動一個實例,然後運行一些命令。我知道如果我嘗試以userdata的形式創建一個實例,它可以通過。我想弄清楚的是,如果我正在啓動一個已經創建的實例,該如何傳遞它。以下是使用boto3代碼通過簡單的Hello World,同時創建一個實例:從Python腳本啓動實例後,如何通過EC2的命令?

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 
) 

喜歡的東西

i = ec2.Instance(id='i-5fea4d42') 
i.start('pass commands here: eg echo xx, mv a b/ etc') 
+0

你想每次運行不同的命令或者在用戶數據中使用相同的命令嗎? – helloV

+0

我正試圖在ec2 shel上執行命令。他們會有所不同,我試圖使用博託和命令外殼,這是沒有結轉到博託3鏈接。可能我會使用paramiko和boto3的組合。 –

回答

0

我能夠使用paramiko以及boto3和awscli以自動方式在雲上運行所有內容。

import paramiko 
import boto3 
import time 
import subprocess 
import awscli 
import os 

# Starting instance, copying data to instance, running model, copying output to s3 
print ("\nCreating ssh session") 
session = boto3.Session() 
ec2 = session.resource('ec2', region_name='us-east-1') 
i = ec2.Instance(id='instance id') # instance id 


print('\nstarting deeplearning_ami instance') 
i.start() 
i.wait_until_running() 
i.load() 
print ("Waiting for the checks to finish..") 
time.sleep(45) 

k = paramiko.RSAKey.from_private_key_file(key_path) #your private key for ssh 
c = paramiko.SSHClient() 
c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
print ("\nConnecting to shell using ssh") 
c.connect(hostname = i.public_dns_name, username = "ec2-user", pkey = k) 

print ("\nExecuting commands on EC2 instance\n") 

stdin , stdout, stderr = c.exec_command(""" mkdir -p model; 
             printf 'Creating directory structure, if it does not exists \n'; 
             cd model; 
             mkdir -p data out cp; 
             printf '\nCopying code from s3 bucket to ec2 instance \n'; 
             aws s3 cp {0} .; 
             printf '\nDownloading data from s3 bucket to ec2 instance'; 
             aws s3 sync {1} data; 
             printf '\n Download complete, running model..\n\n'; 
             python theano_test.py; 
             echo 'Hello S3!' > out/hello.txt; 
             printf '\n Copying output to s3 bucket \n'; 
             aws s3 sync out {2} """.format(s3_code,s3_data,s3_out), get_pty=True) 

for line in iter(lambda: stdout.readline(2048), ""): print(line, end="") 
print ("\n \n Processingfinished") 

#print ('Stopping instance') 
i.stop() 


print ('Copying output from s3 bucket to local') 
os.system('aws s3 sync ' + s3_out + ' ' + local_out) 
0

您可以設置的UserData和啓動實例,它會拿起變化: http://boto3.readthedocs.io/en/latest/reference/services/ec2.html#EC2.Client.modify_instance_attribute

然而,的UserData腳本通常只設置運行一次,所以你必須修改它在每次啓動時運行: How do I make cloud-init startup scripts run every time my EC2 instance boots?

另一種方法是在啓動後設置SSH並運行腳本,但在我看來,在UserData中推送它更清潔。

+0

我正在嘗試使用boto和命令外殼程序,它並沒有結轉到boto3 [link](http://stackoverflow.com/questions/34028219/how-to-execute-commands-on-aws-instance-using- boto3)。可能我會使用paramiko和boto3的組合。 –

相關問題