aws-ec2-runtime-checker

by rayselfs

Helm chartv0.1.4App v0.1.5Verified publisher

AWS EC2 Long-Running Checker

About this chart

AWS EC2 Runtime Checker Helm Chart

A Helm chart for deploying the AWS EC2 Runtime Checker on Kubernetes. This tool monitors EC2 instances and automatically terminates those exceeding configured runtime thresholds.

Prerequisites

  • Kubernetes 1.19+
  • Helm 3.0+
  • AWS credentials configured (IRSA recommended for EKS)

Installing the Chart

To install the chart with the release name aws-ec2-runtime-checker:

helm repo add rayselfs https://rayselfs.github.io/helm-charts
helm repo update

# Install the chart
helm install aws-ec2-runtime-checker rayselfs/aws-ec2-runtime-checker

Install with custom values

helm install aws-ec2-runtime-checker rayselfs/aws-ec2-runtime-checker \
  --set aws.region=us-west-2 \
  --set dryRun=false \
  --set 'targets[0].instanceType=t3.micro' \
  --set 'targets[0].maxRuntimeHours=24'

Configuration

See values.yaml for the full list of configuration options.

Parameters

Global Parameters

ParameterDescriptionDefault
kindDeployment type: Deployment or CronJobDeployment
scheduleCron schedule expression (required for both modes)"0 * * * *"
replicaCountNumber of replicas (Deployment mode only)1
nameOverrideOverride the name of the chart""
fullnameOverrideOverride the full name of the release""

Image Parameters

ParameterDescriptionDefault
image.repositoryContainer image repositoryrayselfs/aws-ec2-runtime-checker
image.pullPolicyImage pull policyIfNotPresent
image.tagImage tag (overrides chart appVersion)""
imagePullSecretsImage pull secrets for private registries[]

AWS Configuration

ParameterDescriptionDefault
aws.regionAWS region where EC2 instances are locatedus-east-1
aws.accessKeyIdAWS Access Key ID (not recommended, use IRSA)""
aws.secretAccessKeyAWS Secret Access Key (not recommended, use IRSA)""
snsTopicArnSNS topic ARN for notifications (optional)""
dryRunEnable dry run mode (no actual termination)true

Target Configuration

ParameterDescriptionDefault
targetsList of instance types and max runtime hoursSee values.yaml
targets[].instanceTypeEC2 instance type to monitort2.micro
targets[].maxRuntimeHoursMaximum allowed runtime in hours24

Service Account Parameters

ParameterDescriptionDefault
serviceAccount.createCreate a service accounttrue
serviceAccount.annotationsAnnotations for the service account (e.g., IRSA){}
serviceAccount.nameName of the service account""

Leader Election Parameters

ParameterDescriptionDefault
leaderElection.enabledEnable leader election (required for replicas > 1)false
leaderElection.leaseNameName of the lease resource""

Pod Disruption Budget Parameters

ParameterDescriptionDefault
pdb.createCreate a PodDisruptionBudgetfalse
pdb.minAvailableMinimum number of pods that must be available1
pdb.maxUnavailableMaximum number of pods that can be unavailablenil

Security Parameters

ParameterDescriptionDefault
podSecurityContextPod security context{}
securityContextContainer security context{}
podAnnotationsAnnotations to add to pods{}

Resource Parameters

ParameterDescriptionDefault
resources.limits.cpuCPU limitnil
resources.limits.memoryMemory limitnil
resources.requests.cpuCPU requestnil
resources.requests.memoryMemory requestnil

Scheduling Parameters

ParameterDescriptionDefault
nodeSelectorNode selector for pod assignment{}
tolerationsTolerations for pod assignment[]
affinityAffinity rules for pod assignment{}

Example: Setting Parameters via CLI

helm install aws-ec2-runtime-checker rayselfs/aws-ec2-runtime-checker \
  --set aws.region=us-west-2 \
  --set dryRun=false \
  --set replicaCount=2 \
  --set leaderElection.enabled=true \
  --set 'targets[0].instanceType=t3.micro' \
  --set 'targets[0].maxRuntimeHours=24'

Deployment Configuration

Use Deployment mode for continuous operation with internal cron scheduling. Recommended for high availability scenarios.

# deployment-values.yaml
kind: Deployment
replicaCount: 2
schedule: "*/30 * * * *" # Every 30 minutes

aws:
  region: us-east-1

dryRun: false # Enable actual termination

snsTopicArn: "arn:aws:sns:us-east-1:123456789012:ec2-alerts"

targets:
  - instanceType: "t2.micro"
    maxRuntimeHours: 24
  - instanceType: "t3.micro"
    maxRuntimeHours: 48
  - instanceType: "c5.large"
    maxRuntimeHours: 72

leaderElection:
  enabled: true
  leaseName: "ec2-checker-leader"

pdb:
  create: true
  minAvailable: 1

resources:
  limits:
    cpu: 200m
    memory: 256Mi
  requests:
    cpu: 100m
    memory: 128Mi

serviceAccount:
  create: true
  annotations:
    eks.amazonaws.com/role-arn: arn:aws:iam::123456789012:role/ec2-checker-role

CronJob Configuration

Use CronJob mode for Kubernetes-managed scheduling. Recommended for simpler deployments without high availability requirements.

# cronjob-values.yaml
kind: CronJob
schedule: "*/30 * * * *" # Every 30 minutes

aws:
  region: us-east-1

dryRun: false # Enable actual termination

snsTopicArn: "arn:aws:sns:us-east-1:123456789012:ec2-alerts"

targets:
  - instanceType: "t2.micro"
    maxRuntimeHours: 24
  - instanceType: "t3.micro"
    maxRuntimeHours: 48
  - instanceType: "c5.large"
    maxRuntimeHours: 72

resources:
  limits:
    cpu: 200m
    memory: 256Mi
  requests:
    cpu: 100m
    memory: 128Mi

serviceAccount:
  create: true
  annotations:
    eks.amazonaws.com/role-arn: arn:aws:iam::123456789012:role/ec2-checker-role

AWS IAM Permissions

The application requires specific IAM permissions to function properly.

Required IAM Policy

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "EC2DescribeInstances",
      "Effect": "Allow",
      "Action": ["ec2:DescribeInstances"],
      "Resource": "*"
    },
    {
      "Sid": "EC2TerminateInstances",
      "Effect": "Allow",
      "Action": ["ec2:TerminateInstances"],
      "Resource": "arn:aws:ec2:*:*:instance/*",
      "Condition": {
        "StringEquals": {
          "ec2:InstanceType": ["t2.micro", "t3.micro"]
        }
      }
    }
  ]
}

Optional: SNS Notification Policy

If using SNS notifications, add:

{
  "Sid": "SNSPublish",
  "Effect": "Allow",
  "Action": ["sns:Publish"],
  "Resource": "arn:aws:sns:*:*:ec2-checker-*"
}

Setting up IRSA (IAM Roles for Service Accounts) on EKS

  1. Create IAM Policy
aws iam create-policy \
  --policy-name EC2RuntimeCheckerPolicy \
  --policy-document file://iam-policy.json
  1. Create IAM Role with OIDC Provider
eksctl create iamserviceaccount \
  --name ec2-checker \
  --namespace default \
  --cluster YOUR_CLUSTER_NAME \
  --attach-policy-arn arn:aws:iam::ACCOUNT_ID:policy/EC2RuntimeCheckerPolicy \
  --approve \
  --override-existing-serviceaccounts
  1. Install Helm Chart with IRSA
serviceAccount:
  create: true
  annotations:
    eks.amazonaws.com/role-arn: arn:aws:iam::ACCOUNT_ID:role/eksctl-CLUSTER-addon-iamserviceaccount-Role
  name: ec2-checker

Alternative: Using AWS Access Keys (Not Recommended)

For non-EKS clusters or testing:

aws:
  region: us-east-1
  accessKeyId: "AKIAIOSFODNN7EXAMPLE"
  secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"

⚠️ Security Warning: Using access keys is not recommended for production. Use IRSA or IAM instance profiles instead.

Deployment Modes

Deployment Mode (Recommended)

Runs continuously with internal cron scheduling:

kind: Deployment
replicaCount: 2
schedule: "*/30 * * * *"
leaderElection:
  enabled: true

Advantages:

  • High availability with leader election
  • Immediate feedback on deployment
  • Better for frequent checks

CronJob Mode

Kubernetes manages the scheduling:

kind: CronJob
schedule: "0 */6 * * *" # Every 6 hours

Advantages:

  • Simpler resource management
  • Better for infrequent checks
  • No need for leader election

Uninstalling the Chart

helm uninstall aws-ec2-runtime-checker

Upgrading the Chart

helm upgrade aws-ec2-runtime-checker rayselfs/aws-ec2-runtime-checker \
  -f values.yaml

Troubleshooting

Check Pod Logs

kubectl logs -l app.kubernetes.io/name=aws-ec2-runtime-checker

Verify AWS Permissions

kubectl exec -it deployment/aws-ec2-runtime-checker -- \
  aws ec2 describe-instances --max-results 1

Test Dry Run Mode

Always test with dryRun: true first to verify the configuration without terminating instances.

License

Apache License 2.0

Deploy aws-ec2-runtime-checker with OpenTenant

Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has aws-ec2-runtime-checker on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.

  1. Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
  2. Place the chartaws-ec2-runtime-checker is pinned at v0.1.4 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
  3. ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below
opentenant validate opentenant.yaml
terraform init && terraform apply