Skip to content

Commit

Permalink
fix case insensitivity for EC2_ACTION env var
Browse files Browse the repository at this point in the history
remove double-processing of EC2_ACTION env var and only rely on
processing the value in set_instance_state.
  • Loading branch information
ConsoleCatzirl committed Apr 17, 2024
1 parent aa56f56 commit dcdf435
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
46 changes: 24 additions & 22 deletions ec2_terminator/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,18 @@ def set_instance_state(instances, state, region):
function = None
processed = []

if state.lower() == 'stop':
_state = state.lower()
LOG.debug(f"State action: {_state}")

if _state == 'stop':
function = client.stop_instances
resp_key = 'StoppingInstances'
action = 'Stopped'
action = 'stopped'

elif state.lower() == 'terminate':
elif _state == 'terminate':
function = client.terminate_instances
resp_key = 'TerminatingInstances'
action = 'Terminated'
action = 'terminated'

else:
raise ValueError(f"Unknown instance state: {state}")
Expand All @@ -99,8 +102,10 @@ def set_instance_state(instances, state, region):
if resp[resp_key]:
processed = [i['InstanceId'] for i in resp[resp_key]]

LOG.debug(f"{action}: {processed}")
return processed
message = f"Instances {action}: {processed}"
LOG.debug(message)

return processed, message


def lambda_handler(event, context):
Expand All @@ -127,18 +132,19 @@ def lambda_handler(event, context):

try:
# Are we stopping or terminating instances?
ec2_action = 'stop'
if os.environ.get('EC2_ACTION', '') == 'TERMINATE':
ec2_action = 'terminate'
ec2_action = os.environ.get('EC2_ACTION', '')
if ec2_action == '':
ec2_action = 'stop'

# Get the list of regions
regions = list_regions()
if not regions:
raise ValueError("No available regions")

# List of stopped or terminated instances
found = False
found = 0
processed = []
message = 'No running or stopped instances found'

# Iterate over every region
for region in regions:
Expand All @@ -147,23 +153,19 @@ def lambda_handler(event, context):

# Stop or terminate any instances found
if ec2_instances:
found = True
processed.extend(set_instance_state(ec2_instances,
ec2_action,
region))
found = len(ec2_instances)
_processed, message = set_instance_state(ec2_instances,
ec2_action,
region)
processed.extend(_processed)
else:
LOG.debug("No instances found")

# Report results
if found and not processed:
# Raise an error if some instances failed to stop or terminate
if found > 0 and found > len(processed):
raise RuntimeError("Some instances failed to stop or terminate")
elif not found:
message = "No running or stopped instances found"
elif ec2_action == 'terminate':
message = f"Instances terminated: {processed}"
else:
message = f"Instances stopped: {processed}"

# Report results
LOG.info(message)
return {
"statusCode": 200,
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def test_terminate(mocker,
"""Test terminating instances"""

env_vars = {
'EC2_ACTION': 'TERMINATE',
'EC2_ACTION': 'TeRmInAtE',
}
mocker.patch.dict(os.environ, env_vars)

Expand Down Expand Up @@ -230,6 +230,7 @@ def test_ignore_age_instance(mocker,
assert "message" in ret["body"]
assert data["message"] == "No running or stopped instances found"


def test_terminate_failed(mocker,
stub_ec2_client,
mock_region_response,
Expand Down

0 comments on commit dcdf435

Please sign in to comment.