This repository packages a scheduled AWS Lambda job built with lambdacron. The job scans EC2 instances for a configurable tag key whose value is the maximum allowed uptime in hours, then either notifies, stops, or terminates instances that have exceeded that limit.
The scheduled Lambda lives in lambda/docker/lambda_handler.py. The root Terraform module in this repository is meant to be consumed by a separate deployment repository that owns the deployment configuration for a specific AWS account.
- Instances are discovered with the configured tag key. By default that key is
max_hours_uptime. - The tag value must be a positive number of hours.
- When the elapsed time since
LaunchTimemeets or exceeds that value, the Lambda performs the configured action:notify: publish an overdue result and take no EC2 actionstop: requestStopInstancesfor overdue running instancesterminate: requestTerminateInstancesfor overdue non-terminal instances
- Notification payloads include the instance ID, name, state, launch time, deadline, elapsed hours, configured max hours, and action status.
The root module exposes project-specific inputs for the runtime contract:
max_uptime_tag_keyenforcement_actionscheduled_lambda_additional_policy_arns
It also injects the required EC2 permissions into the scheduled Lambda role:
ec2:DescribeInstancesec2:StopInstanceswhenenforcement_action = "stop"ec2:TerminateInstanceswhenenforcement_action = "terminate"
Example inputs for local interface development are in terraform.tfvars.example, lambda/terraform.tfvars.example, and bootstrap-deploy-repo/terraform.tfvars.example.
- Python tests:
python3 -m unittest discover -s tests -p 'test_*.py' -q - Terraform/OpenTofu validation:
tofu init -backend=false && tofu validatetofu -chdir=lambda init -backend=false && tofu -chdir=lambda validatetofu -chdir=bootstrap-deploy-repo init -backend=false && tofu -chdir=bootstrap-deploy-repo validate
See README-lambdacron.md for the underlying template model and repository structure.