Skip to content

Commit 3db64ea

Browse files
committed
Lazy load boto client when using datadogpy for metrics.
1 parent 5a55fe4 commit 3db64ea

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

datadog_lambda/api.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ def get_api_key() -> str:
5757
if api_key:
5858
return api_key
5959

60-
import boto3
61-
6260
DD_API_KEY_SECRET_ARN = os.environ.get("DD_API_KEY_SECRET_ARN", "")
6361
DD_API_KEY_SSM_NAME = os.environ.get("DD_API_KEY_SSM_NAME", "")
6462
DD_KMS_API_KEY = os.environ.get("DD_KMS_API_KEY", "")
@@ -85,7 +83,7 @@ def get_api_key() -> str:
8583
if is_gov_region
8684
else None
8785
)
88-
secrets_manager_client = boto3.client(
86+
secrets_manager_client = _boto3_client(
8987
"secretsmanager", endpoint_url=endpoint_url, region_name=secrets_region
9088
)
9189
api_key = secrets_manager_client.get_secret_value(
@@ -96,7 +94,7 @@ def get_api_key() -> str:
9694
fips_endpoint = (
9795
f"https://ssm-fips.{LAMBDA_REGION}.amazonaws.com" if is_gov_region else None
9896
)
99-
ssm_client = boto3.client("ssm", endpoint_url=fips_endpoint)
97+
ssm_client = _boto3_client("ssm", endpoint_url=fips_endpoint)
10098
api_key = ssm_client.get_parameter(
10199
Name=DD_API_KEY_SSM_NAME, WithDecryption=True
102100
)["Parameter"]["Value"]
@@ -105,7 +103,7 @@ def get_api_key() -> str:
105103
fips_endpoint = (
106104
f"https://kms-fips.{LAMBDA_REGION}.amazonaws.com" if is_gov_region else None
107105
)
108-
kms_client = boto3.client("kms", endpoint_url=fips_endpoint)
106+
kms_client = _boto3_client("kms", endpoint_url=fips_endpoint)
109107
api_key = decrypt_kms_api_key(kms_client, DD_KMS_API_KEY)
110108
else:
111109
api_key = DD_API_KEY
@@ -133,3 +131,9 @@ def init_api():
133131

134132
# Unmute exceptions from datadog api client, so we can catch and handle them
135133
api._mute = False
134+
135+
136+
def _boto3_client(*args, **kwargs):
137+
import botocore.session
138+
139+
return botocore.session.get_session().create_client(*args, **kwargs)

pyproject.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ datadog = ">=0.51.0,<1.0.0"
3030
wrapt = "^1.11.2"
3131
ddtrace = ">=2.20.0,<4"
3232
ujson = ">=5.9.0"
33-
boto3 = { version = "^1.34.0", optional = true }
33+
botocore = { version = "^1.34.0", optional = true }
3434
requests = { version ="^2.22.0", optional = true }
3535
pytest = { version= "^8.0.0", optional = true }
3636
pytest-benchmark = { version = "^4.0", optional = true }
3737
flake8 = { version = "^5.0.4", optional = true }
3838

3939
[tool.poetry.extras]
4040
dev = [
41-
"boto3",
41+
"botocore",
4242
"flake8",
4343
"pytest",
4444
"pytest-benchmark",

0 commit comments

Comments
 (0)