Skip to content

Commit 52df7b1

Browse files
committed
Lazy load boto client when using datadogpy for metrics.
1 parent b64be3b commit 52df7b1

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

datadog_lambda/api.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ def init_api():
5454
from datadog import api
5555

5656
if not api._api_key:
57-
import boto3
58-
5957
DD_API_KEY_SECRET_ARN = os.environ.get("DD_API_KEY_SECRET_ARN", "")
6058
DD_API_KEY_SSM_NAME = os.environ.get("DD_API_KEY_SSM_NAME", "")
6159
DD_KMS_API_KEY = os.environ.get("DD_KMS_API_KEY", "")
@@ -64,15 +62,15 @@ def init_api():
6462
)
6563

6664
if DD_API_KEY_SECRET_ARN:
67-
api._api_key = boto3.client("secretsmanager").get_secret_value(
65+
api._api_key = _boto3_client("secretsmanager").get_secret_value(
6866
SecretId=DD_API_KEY_SECRET_ARN
6967
)["SecretString"]
7068
elif DD_API_KEY_SSM_NAME:
71-
api._api_key = boto3.client("ssm").get_parameter(
69+
api._api_key = _boto3_client("ssm").get_parameter(
7270
Name=DD_API_KEY_SSM_NAME, WithDecryption=True
7371
)["Parameter"]["Value"]
7472
elif DD_KMS_API_KEY:
75-
kms_client = boto3.client("kms")
73+
kms_client = _boto3_client("kms")
7674
api._api_key = decrypt_kms_api_key(kms_client, DD_KMS_API_KEY)
7775
else:
7876
api._api_key = DD_API_KEY
@@ -87,3 +85,7 @@ def init_api():
8785

8886
# Unmute exceptions from datadog api client, so we can catch and handle them
8987
api._mute = False
88+
89+
def _boto3_client(*args, **kwargs):
90+
import botocore
91+
return botocore.session.get_session().create_client(*args, **kwargs)

0 commit comments

Comments
 (0)