Skip to content

Instantly share code, notes, and snippets.

@abubaker417
Last active February 25, 2026 17:59
Show Gist options
  • Select an option

  • Save abubaker417/af6394768c0fb9d68f7778e2ca343563 to your computer and use it in GitHub Desktop.

Select an option

Save abubaker417/af6394768c0fb9d68f7778e2ca343563 to your computer and use it in GitHub Desktop.
  1. Give permission to user

    1. AWS Managed Policy: AWSLambda_FullAccess
    2. AWS Managed Policy: AmazonEventBridgeFullAccess
    3. AWS Managed Policy: CloudWatchLogsFullAccess
    
  2. Create a Lambda Execution Role => Name => lambda-ec2-start-stop-role

    AmazonEC2FullAccess
    AWSLambdaBasicExecutionRole
    and share the Role ARN with me.
    
  3. Cron Job Command

    Your Schedule

    Action Pakistan Time UTC Time
    Start 8:30 AM Mon-Fri 3:30 AM UTC
    Stop 7:30 PM Mon-Fri 2:30 PM UTC
  4. Cron Expressions

    Start Instances — 8:30 AM PKT (Mon-Fri)

    cron(30 3 ? * MON-FRI *)
    

    Stop Instances — 7:30 PM PKT (Mon-Fri)

    cron(30 14 ? * MON-FRI *)
    

    How to Read This Cron

    cron(Minutes  Hours  Day-of-Month  Month  Day-of-Week  Year)
          30       3         ?           *      MON-FRI      *
          
          
    30 → 30 minutes
    3 → 3 AM UTC (= 8:30 AM PKT)
    ? → any day of month (we're using day-of-week instead)
    * → every month
    MON-FRI → Monday to Friday only (Saturday & Sunday automatically skipped)
    * → every year
    
  5. Start Code

    import boto3
    from botocore.config import Config
    
    ec2_config = Config(region_name='us-east-1')
    ec2 = boto3.client('ec2', config=ec2_config)
    
    def get_instances():
        return [
            item["Instances"][0]["InstanceId"]
            for item in ec2.describe_instances(
                Filters=[
                    {'Name': 'instance-state-name', 'Values': ['stopped']},
                    {'Name': 'tag:AutoSchedule', 'Values': ['true']} 
                ]
            ).get("Reservations")
        ]
    
    def start_instances(instance_ids):
        ec2.start_instances(InstanceIds=instance_ids)
    
    def lambda_handler(event, context):
        instances = get_instances()
        print(f"Starting the instances {instances}")
        start_instances(instances)
    
  6. Stop

    import boto3
    from botocore.config import Config
    
    ec2_config = Config(region_name='us-east-1')
    ec2 = boto3.client('ec2', config=ec2_config)
    
    def get_instances():
        return [
            item["Instances"][0]["InstanceId"]
            for item in ec2.describe_instances(
                Filters=[
                    {'Name': 'instance-state-name', 'Values': ['running']},
                    {'Name': 'tag:AutoSchedule', 'Values': ['true']} 
                    ]
            ).get("Reservations")
        ]
    
    def stop_instances(instance_ids):
        ec2.stop_instances(InstanceIds=instance_ids, Force=True)
    
    def lambda_handler(event, context):
        instances = get_instances()
        print(f"Stopping the instances {instances}")
        stop_instances(instances)
    

"symfony/http-client": "^6.4", "symfony/mailgun-mailer": "^6.4"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment