Skip to content

Commit c02b687

Browse files
committed
PERF: Improve speed and simplify lambda api key authorizer
This moves the table instantiation into the main body of the code to cache it in the lambda execution environment. Also inline the simple functions and remove the unnecessary logger.
1 parent 7164d7a commit c02b687

1 file changed

Lines changed: 8 additions & 19 deletions

File tree

sds_data_manager/lambda_code/authorization/lambda_api_key_authorizer.py

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,11 @@
11
"""Authorization for API Keys within the SDS."""
22

3-
import logging
4-
53
import boto3
64

7-
logger = logging.getLogger(__name__)
8-
logger.setLevel(logging.INFO)
9-
105
# Initialize DynamoDB resource
11-
TABLE_NAME = "imap-sdc-api-keys"
12-
13-
14-
def get_api_key_metadata(api_key):
15-
"""Retrieve API key metadata from DynamoDB."""
16-
try:
17-
dynamodb = boto3.resource("dynamodb")
18-
table = dynamodb.Table(TABLE_NAME)
19-
response = table.get_item(Key={"api_key": api_key})
20-
return response.get("Item")
21-
except Exception as e:
22-
logger.error(f"Error retrieving API key metadata: {e}")
23-
return None
6+
# Specifically outside of the handler to be cached in the lambda execution environment
7+
dynamodb = boto3.resource("dynamodb")
8+
table = dynamodb.Table("imap-sdc-api-keys")
249

2510

2611
def lambda_handler(event, context):
@@ -31,7 +16,11 @@ def lambda_handler(event, context):
3116
return {"isAuthorized": False}
3217

3318
# Retrieve metadata from DynamoDB
34-
metadata = get_api_key_metadata(api_key)
19+
try:
20+
metadata = table.get_item(Key={"api_key": api_key}).get("Item")
21+
except Exception:
22+
# Log? print(f"Error retrieving API key metadata: {e}")
23+
return {"isAuthorized": False}
3524
if not metadata:
3625
return {"isAuthorized": False}
3726

0 commit comments

Comments
 (0)