在AWS EC2上运行Python脚本

时间:2017-02-10 20:05:41

标签: amazon-web-services amazon-ec2 boto3

道歉,如果它重复,但我找不到任何有价值的东西来完成我的任务。 我有一个实例,我已经想出使用boto3启动和停止它并且它可以工作,但真正的问题是当实例启动时运行脚本。我想等待脚本完成然后停止实例。

python /home/ubuntu/MyProject/TechInd/EuropeRun.py &
python /home/ubuntu/FTDataCrawlerEU/EuropeRun.py &

阅读相当多的文章会引导Lambda和AWS Beanstalk的方向,但这些看起来并不简单。

非常感谢任何建议。

此致 DC

3 个答案:

答案 0 :(得分:1)

您可以使用以下代码。

import boto3
import botocore
import os
from termcolor import colored
import paramiko


def stop_instance(instance_id, region_name):
    client = boto3.client('ec2', region_name=region_name)
while True:
    try:
        client.stop_instances(
            InstanceIds=[
                instance_id,
            ],
            Force=False
        )

    except Exception, e:
        print e

    else:
        break

# Waiter to wait till instance is stopped
waiter = client.get_waiter('instance_stopped')
try:
    waiter.wait(
        InstanceIds=[
            instance_id,
        ]
    )
except Exception, e:
    print e


def ssh_connect(public_ip, cmd):

# Join the paths using directory name and file name, to avoid OS conflicts
key_path = os.path.join('path_to_aws_pem', 'file_name.pem')

key = paramiko.RSAKey.from_private_key_file(key_path)
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# Connect/ssh to an instance
while True:
    try:
        client.connect(hostname=public_ip, username="ubuntu", pkey=key)

        # Execute a command after connecting/ssh to an instance
        stdin, stdout, stderr = client.exec_command(cmd)
        print stdout.read()

        # close the client connection once the job is done
        client.close()
        break

    except Exception, e:
        print e

# Main/Other module where you're doing other jobs:

# Get the public IP address of EC2 instance, I assume you should have handle to the ec2 instance already
# You can use any alternate method to fetch/get public ip of your ec2 instance
public_ip = ec2_instance.public_ip_address


# Get the instance ID of EC2 instance, I assume you should have handle to the ec2 instance already
instance_id = ec2_instance.instance_id

# Command to Run/Execute python scripts
cmd = "nohup python /home/ubuntu/MyProject/TechInd/EuropeRun.py & python /home/ubuntu/FTDataCrawlerEU/EuropeRun.py &"
ssh_connect(public_ip, cmd)
print colored('Script execution finished !!!', 'green')

# Shut down/Stop the instance
stop_instance(instance_id, region_name)

答案 1 :(得分:0)

脚本完成后,您可以通过python代码执行shutdown命令。

使用ls

的示例
from subprocess import call
call(["ls", "-l"])

但是对于简单的lambda更容易和资源有效的东西。您只需要将脚本上传到s3并通过boto3执行lambda函数。 实际上,如果您没有任何依赖关系,您可以将脚本代码复制粘贴到lambda控制台。

答案 2 :(得分:0)

在系统启动时自动运行脚本的一些选项:

  • 通过EC2 User-Data
  • 调用脚本
  • 配置AMI以通过init.d脚本或@reboot cron作业启动脚本。

要在脚本完成后关闭实例,请在脚本末尾添加一些代码以启动操作系统关闭,或调用AWS API(通过Boto)关闭实例。