ds_*_*ser 10 python amazon-ec2 amazon-web-services
我正在尝试在AWS中安排我的python脚本,但是我不希望实例一直在运行.因此,尝试自动化以下过程:
我无法直接将此脚本作为Lambda函数运行,因为脚本执行一些需要更多RAM的并行处理,因此选择更大的AWS实例而不是将其编写为lambda函数.此外,不希望此实例一直在运行,因为它很昂贵.
到目前为止,我跟着使用Lambda和CloudWatch自动启动和停止AWS EC2实例·matoski.com并创建了一个Lambda函数来在特定时间启动和停止实例,但是我找不到运行python脚本的方法实例已启动.
谁能指出我正确的方向?
我的应用程序每天运行一个实例@ 13:39 UST,并在处理完成后自行关闭.它使用下面
import boto3
def lambda_handler(event, context):
ec2 = boto3.client('ec2', region_name='ap-south-1')
ec2.start_instances(InstanceIds=['i-xxxxxxx'])
print('started your instances: ' + str('i-xxxxxx'))
returnRun Code Online (Sandbox Code Playgroud)
这会触发一个运行cron以执行Python脚本的实例
@reboot python /home/Init.py
一旦脚本完成,python job就会使用下面的代码段来关闭它自己
import boto.ec2
import boto.utils
import logging
logger=logging.getLogger()
def stop_ec2():
conn = boto.ec2.connect_to_region("ap-south-1") # or your region
# Get the current instance's id
my_id = boto.utils.get_instance_metadata()['instance-id']
logger.info(' stopping EC2 :'+str(my_id))
conn.stop_instances(instance_ids=[my_id])Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5001 次 |
| 最近记录: |