Auto Start & Stop EC2 Instance at a Specific Time Using AWS EventBridge by awsmasterchef

Поделиться
HTML-код
  • Опубликовано: 28 окт 2024

Комментарии • 2

  • @marloncantillo
    @marloncantillo 2 месяца назад

    So helpful. Thanks for the video.

  • @uganrajoo8379
    @uganrajoo8379 4 месяца назад +1

    This is a great way to start / stop an EC2 instance. Thank you for this, however you should put the Lambda function in your video description to avoid people needing to manually type it out.
    I have typed out the function here in case anyone needs it:
    import boto3
    def lambda_handler(event, context):
    ec2 = boto3.client('ec2')
    action = event['action']
    instances = ec2.describe_instances()
    instance_ids = [instance['InstanceId'] for reservation in instances['Reservations'] for instance in reservation['Instances']]
    if action == 'start':
    if instance_ids:
    ec2.start_instances(InstanceIds=instance_ids)
    print('Starting instances;", instance_ids)
    else:
    print("No instances found to start")
    elif action == 'stop':
    if instance_ids:
    ec2.stop_instances(InstanceIds=instance_ids)
    print("Stopping instances:", instance_ids)
    else:
    print("No instances found to stop")
    else:
    print("Invalid action provided in the event")