diff --git a/s3-age-metric/src/lambda_function.py b/s3-age-metric/src/lambda_function.py index 1b9b4a2..211aa63 100644 --- a/s3-age-metric/src/lambda_function.py +++ b/s3-age-metric/src/lambda_function.py @@ -42,12 +42,12 @@ def lambda_handler(event, context): logger.fatal(str(e)) raise e - s3_contents = sorted(s3_objects["Contents"], key= lambda i: i['LastModified'] ) - - oldest_item_in_set = s3_contents[0]['LastModified'] - if oldest_item_in_set < oldest_item: - oldest_item = oldest_item_in_set - oldest_item_key = s3_contents[0]['Key'] + if 'Contents' in s3_objects: + s3_contents = sorted(s3_objects['Contents'], key= lambda i: i['LastModified'] ) + oldest_item_in_set = s3_contents[0]['LastModified'] + if oldest_item_in_set < oldest_item: + oldest_item = oldest_item_in_set + oldest_item_key = s3_contents[0]['Key'] if s3_objects['IsTruncated']: continuation_token = s3_objects['NextContinuationToken'] diff --git a/s3-age-metric/src/test_lambda_function.py b/s3-age-metric/src/test_lambda_function.py index 5605f02..74df6ec 100644 --- a/s3-age-metric/src/test_lambda_function.py +++ b/s3-age-metric/src/test_lambda_function.py @@ -9,6 +9,24 @@ from lambda_function import lambda_handler +@mock_cloudwatch +@mock_s3 +def test_no_object_in_bucket(): + + bucket_name = 'test_bucket' + bucket_prefix = '' + oldest_object_key = 'single_object' + + s3_client = boto3.client('s3', region_name='us-east-1') + s3_client.create_bucket(Bucket=bucket_name) + + with mock.patch.dict(os.environ, {'BUCKET_NAME': bucket_name, 'BUCKET_PREFIX': bucket_prefix}) as env: + result = lambda_handler({}, {}) + + assert result['bucket_name'] == 'test_bucket' + assert result['bucket_prefix'] == '' + assert result['oldest_item_key'] == None + @mock_cloudwatch @mock_s3 def test_single_object_in_bucket(): @@ -37,6 +55,41 @@ def test_single_object_in_bucket(): assert result['oldest_item_key'] == 'single_object' +@mock_cloudwatch +@mock_s3 +def test_small_set_of_objects_in_bucket(): + + bucket_name = 'test_bucket' + bucket_prefix = '' + + s3_client = boto3.client('s3', region_name='us-east-1') + s3_client.create_bucket(Bucket=bucket_name) + + # Create a single item first which should be the oldest item + oldest_object_key = 'oldest_object' + s3_client.put_object( + Body='test content', + Bucket=bucket_name, + Key=oldest_object_key + ) + + time.sleep(1) + + # Create a lot of items to ensure pagination works as expected + for i in range(10): + s3_client.put_object( + Body='test content', + Bucket=bucket_name, + Key=f'object_{i}' + ) + + with mock.patch.dict(os.environ, {'BUCKET_NAME': bucket_name, 'BUCKET_PREFIX': bucket_prefix}) as env: + result = lambda_handler({}, {}) + + assert result['bucket_name'] == 'test_bucket' + assert result['bucket_prefix'] == '' + assert result['oldest_item_key'] == 'oldest_object' + @mock_cloudwatch @mock_s3 def test_large_set_of_objects_in_bucket():