Python script runs exactly for 1 hour when I send a run command to EC2 through lambda function when it supposed to run more than 1 hour

0

Hello, communit guys!

I have a simple test.py Python script (see below) inside EC2 (t2.xlarge, Amazon Linux 2). I'm using VSCode to connect to EC2 for testing/debuging. When I run this code through VSCode or through linux terminal (sudo python3 /home/ec2-user/tmp/test.py) , this code runs until the end

But when I use the lambda to send the same execution command, after exactly 1 hour this code stops running (no python3 task running)! And I want it to keep running longer than 1 hour!

My objective is to send a command line to run my python script through lambda to EC2 and ensure that script run untill the end

test.py

import logging
from time import sleep

logging.basicConfig(filename="/home/ec2-user/tmp/test.log",
                    format='%(asctime)s %(message)s',
                    filemode='w')
logger = logging.getLogger()
logger.setLevel(logging.INFO)
var= True
count = 0
while var==True:
    logger.info('=)')
    sleep(1)
    count+=1
    if count>100000:
        var=False

lambda function

import json
import boto3

comand_shell    = 'sudo python3 /home/ec2-user/tmp/test.py' 
instance_id_ec2  = 'my_ec2_instance_id'

def lambda_handler(event, context):
    client = boto3.client("ec2")
    ssm = boto3.client("ssm")
    
    try:
        describeInstance = client.describe_instances()
        for r in describeInstance['Reservations']:
            for inst in r['Instances']:
                if inst['InstanceId'] == instance_id_ec2:
                    status = inst['State']['Name']
        if status == 'running': 
            response = ssm.send_command(
                InstanceIds=[instance_id_ec2],
                DocumentName="AWS-RunShellScript",
                Parameters={"commands": [comand_shell]},
                )
            print(f'{comand_shell } has sent to {instance_id_ec2} successfuly')
        else:
            print(f'Failed')
    except Exception as e:
        print("Unexpected Exception: %s" % str(e))  
    return None

First/last 5 lines' log file

2022-12-21 00:39:48,379 =)
2022-12-21 00:39:49,381 =)
2022-12-21 00:39:50,382 =)
2022-12-21 00:39:51,383 =)
2022-12-21 00:39:52,385 =)
...
...
...
2022-12-21 01:39:30,908 =)
2022-12-21 01:39:31,765 =)
2022-12-21 01:39:32,302 =)
2022-12-21 01:39:33,286 =)
2022-12-21 01:39:34,853 =)

Thanks

Arita
gefragt vor einem Jahr1007 Aufrufe
1 Antwort
1
Akzeptierte Antwort

I think you're hitting the default timeout for Run Command as per the documentation. You can change the default when you submit the request. There's a short discussion of this on re:Post as well.

profile pictureAWS
EXPERTE
beantwortet vor einem Jahr

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen