Create Lambda Function

Create Lambda Function

In this section, we will write code (using Python) and configure 2 AWS Lambda functions that act as automation executors:

  • Stop instances Lambda Function: Automatically scans and stops EC2 instances tagged with environment_auto after a certain period (e.g., every 9 hours).
  • Start instances Lambda Function: Automatically scans and turns those EC2 instances back on after another regular period (e.g., every 8 hours).

Note on Scheduling:

  • In this lab: We use a Rate-based schedule (e.g., run every 8 hours / 9 hours). These two offset frequencies are intentionally set to create random overlapping intervals, helping the server turn on/off continuously (staggered). Thus, you can easily verify whether the Lambda functions and IAM permissions are working correctly in a short time.
  • In practice (Production): Enterprises do not use Rate-based schedules for this purpose. Instead, they use a Cron-based schedule to precisely start/stop at fixed hours (e.g., the Start schedule runs exactly at 08:00 AM Monday - Friday, and the Stop schedule runs exactly at 06:00 PM Monday - Friday). You can apply a similar setup once you understand the mechanism.