I want to monitor a bucket and when two files got added in to the bucket that should trigger the dragen tool instance and run the dragen command in the instance using those two files. Here in my specific command I'm using in the instance it takes around 1 hour to process two files and store the results in output bucket I mentioned in the command. I tried to use lambda functions in aws but the time out is 15 minutes and while waiting for the execution of command I have sent to instance through SSM service the lambda function getting timed out. I want to know are there any alternative AWS services that provide same lambda functionality but for long processing?
My code:
import boto3
import time
def check_instance_status(instance_id):
ec2_client = boto3.client('ec2')
response = ec2_client.describe_instances(InstanceIds=[instance_id])
if len(response['Reservations']) > 0:
instance_state = response['Reservations'][0]['Instances'][0]['State']['Name']
print(instance_state)
if instance_state == 'running':
print("Instance is running.")
else:
print("Instance is not running.")
else:
print("Instance not found.")
def wait_for_instance_start(instance_id):
ec2_client = boto3.client('ec2')
waiter = ec2_client.get_waiter('instance_running')
try:
waiter.wait(InstanceIds=[instance_id])
print("Instance has started successfully.")
except Exception as e:
print("Error occurred while waiting for instance start:", str(e))
def lambda_handler(event, context):
bucket = event['Records'][0]['s3']['bucket']['name']
file_key = event['Records'][0]['s3']['object']['key']
print(bucket)
print(file_key)
print("lambda function started")
try:
ec2_client = boto3.client('ec2')
ssm_client = boto3.client('ssm')
print("Event triggered")
instance_id = 'i-0ca693219ebbd0a07'
print("InstanceId = ", instance_id)
ec2_client.start_instances(InstanceIds=[instance_id])
wait_for_instance_start(instance_id)
check_instance_status(instance_id)
bash_command = f'/opt/edico/bin/dragen -f -1 s3://{bucket}/input-samples/{file_key} -2 s3://{bucket}/input-samples/test_03.fastq.gz --ref-dir=/home/centos/hg19 --output-directory s3://priyanshu-test-exome/output --enable-map-align=true --RGID="A.A.1" --RGSM=CNTRL-0000131 --output-file-prefix="samples" --intermediate-results-dir=/ephemeral/intermediate-output'
print("bash_Command = ", bash_command)
print("ssm client = ",ssm_client)
response = ssm_client.send_command(
InstanceIds=[instance_id],
DocumentName='AWS-RunShellScript',
Parameters={'commands': [bash_command]},
CloudWatchOutputConfig={
'CloudWatchLogGroupName': '/aws/lambda/solutions-team-dragen-lambda',
'CloudWatchOutputEnabled': True
}
)
command_id = response['Command']['CommandId']
print('Command sent successfully. Command ID:', command_id)
ec2_waiter = ec2_client.get_waiter('instance_status_ok')
ec2_waiter.wait(InstanceIds=[instance_id])
output = ssm_client.get_command_invocation(CommandId=command_id, InstanceId=instance_id)
print("output = ",output)
ec2_client.stop_instances(InstanceIds=[instance_id])
print("stopped instance successfully")
except Exception as e:
print(e)
print("Error in execution")
raise e