Can I plan the execution of a lambda function using a lambda function?

I am looking for an opportunity to programmatically plan a lambda function to run at one time using another lambda function. For example, I made a request myFirstFunctionwith the parameters dateand time, and then executed this date and time mySecondFunction. Is this only possible with stateless AWS? I try to avoid an ec2 instance always.

Most of the results that I find for planning lambda functions are related to cloud and regular events, not individual events.

+4
source share
4 answers

- . , TTL-, TTL. , TTL. @Mentor .

2 AWS TTL DynamoDB, , . , .

DynamoDB - DynamoDB. TTL , , . , . , , .

- .

DynamoDB TTL: https://aws.amazon.com/blogs/aws/new-manage-dynamodb-items-using-time-to-live-ttl/

+1

SQS, myFirstFunction.

SQS Lambda, mySecondFunction CloudWatch ( , ), CloudWatch ApproximateNumberOfMessagesVisible, SNS- , .

0

( ), AWS.

, ec2 , :

1) AWS . , , ApproximateNumberOfMessagesVisible CPUUtilization ( ). , ( , ).

, (AWS , , 15 ).

2). , , , , , .

import boto3
from datetime import datetime

def lambda_handler(event, context):
    print('ManageInstances function executed.')
    environments = [['instance-id-1', 'SQS-queue-url-1'], ['instance-id-2', 'SQS-queue-url-2'], ...]
    ec2_client = boto3.client('ec2')
    for environment in environments:
        instance_id = environment[0]
        queue_url = environment[1]
        print 'Instance:', instance_id
        print 'Queue:', queue_url
        rsp = ec2_client.describe_instances(InstanceIds=[instance_id])
        if rsp:
            status = rsp['Reservations'][0]['Instances'][0]
            if status['State']['Name'] == 'running':
                current_time = datetime.now()
                diff = current_time - status['LaunchTime'].replace(tzinfo=None)
                total_minutes = divmod(diff.total_seconds(), 60)[0]
                minutes_to_complete_hour = 60 - divmod(total_minutes, 60)[1]
                print 'Started time:', status['LaunchTime']
                print 'Current time:', str(current_time)
                print 'Minutes passed:', total_minutes
                print 'Minutes to reach a full hour:', minutes_to_complete_hour
                if(minutes_to_complete_hour <= 2):
                    sqs_client = boto3.client('sqs')
                    response = sqs_client.get_queue_attributes(QueueUrl=queue_url, AttributeNames=['All'])
                    messages_in_flight = int(response['Attributes']['ApproximateNumberOfMessagesNotVisible'])
                    messages_available = int(response['Attributes']['ApproximateNumberOfMessages'])
                    print 'Messages in flight:', messages_in_flight
                    print 'Messages available:', messages_available
                    if(messages_in_flight + messages_available == 0):
                        ec2_resource = boto3.resource('ec2')
                        instance = ec2_resource.Instance(instance_id)
                        instance.stop()
                        print('Stopping instance.')
            else:
                print('Status was not running. Nothing is done.')
        else:
            print('Problem while describing instance.')
0
source

I think you can use these instructions .

But I tried these instructions, and it did not work unless I manually entered the console to associate the rule with the target. (Despite sending an API call put_target)

In addition, I do not understand what the last step in these instructions is. I did not do this because it seemed inapplicable, but perhaps why mine is not working.

0
source

Source: https://habr.com/ru/post/1670354/


All Articles