Skip to content

Repository files navigation

EC2 Zombie Killer

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.

Runtime behavior

  • 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 LaunchTime meets or exceeds that value, the Lambda performs the configured action:
    • notify: publish an overdue result and take no EC2 action
    • stop: request StopInstances for overdue running instances
    • terminate: request TerminateInstances for overdue non-terminal instances
  • Notification payloads include the instance ID, name, state, launch time, deadline, elapsed hours, configured max hours, and action status.

Terraform interface

The root module exposes project-specific inputs for the runtime contract:

  • max_uptime_tag_key
  • enforcement_action
  • scheduled_lambda_additional_policy_arns

It also injects the required EC2 permissions into the scheduled Lambda role:

  • ec2:DescribeInstances
  • ec2:StopInstances when enforcement_action = "stop"
  • ec2:TerminateInstances when enforcement_action = "terminate"

Example inputs for local interface development are in terraform.tfvars.example, lambda/terraform.tfvars.example, and bootstrap-deploy-repo/terraform.tfvars.example.

Validation

  • Python tests: python3 -m unittest discover -s tests -p 'test_*.py' -q
  • Terraform/OpenTofu validation:
    • tofu init -backend=false && tofu validate
    • tofu -chdir=lambda init -backend=false && tofu -chdir=lambda validate
    • tofu -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.

About

Kills EC2 instances that run too long

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages