Skip to content

Commit d137d98

Browse files
authored
Better error handling for AWS token fetching (#884)
* Better error handling for AWS token fetching It turns out for docker containers on AWS elastic beanstalk, the PUT for the token fails, and fails slowly. This is not the case for the ec2 host on which the docker container is running. This was causing issues, compounded by the fact that I didn't have `retries=False` on that call. * Add changelog * Add comments about IMDSv2
1 parent c3e2841 commit d137d98

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

CHANGELOG.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ endif::[]
4242
===== Bug fixes
4343
4444
* Updated CLOUD_PROVIDER config to allow for new options defined in https://github.com/elastic/apm/issues/289[#289] {pull}878[#878]
45+
* Fixed a bug in AWS metadata collection on docker containers in AWS Elastic Beanstalk {pull}884[#884]
4546
4647
4748
[[release-notes-5.x]]

elasticapm/utils/cloud.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,26 @@ def aws_metadata():
4747
# and will be quiet in the logs, unlike urllib3
4848
socket.create_connection(("169.254.169.254", 80), 0.1)
4949

50-
ttl_header = {"X-aws-ec2-metadata-token-ttl-seconds": "300"}
51-
token_url = "http://169.254.169.254/latest/api/token"
52-
token_request = http.request("PUT", token_url, headers=ttl_header, timeout=3.0)
53-
token = token_request.data.decode("utf-8")
54-
aws_token_header = {"X-aws-ec2-metadata-token": token} if token else {}
50+
try:
51+
# This whole block is almost unnecessary. IMDSv1 will be supported
52+
# indefinitely, so the only time this block is needed is if a
53+
# security-conscious user has set the metadata service to require
54+
# IMDSv2. Thus, the very expansive try:except: coverage.
55+
56+
# TODO: should we have a config option to completely disable IMDSv2 to reduce overhead?
57+
ttl_header = {"X-aws-ec2-metadata-token-ttl-seconds": "300"}
58+
token_url = "http://169.254.169.254/latest/api/token"
59+
token_request = http.request("PUT", token_url, headers=ttl_header, timeout=1.0, retries=False)
60+
token = token_request.data.decode("utf-8")
61+
aws_token_header = {"X-aws-ec2-metadata-token": token} if token else {}
62+
except Exception:
63+
aws_token_header = {}
5564
metadata = json.loads(
5665
http.request(
5766
"GET",
5867
"http://169.254.169.254/latest/dynamic/instance-identity/document",
5968
headers=aws_token_header,
60-
timeout=3.0,
69+
timeout=1.0,
6170
retries=False,
6271
).data.decode("utf-8")
6372
)
@@ -94,7 +103,7 @@ def gcp_metadata():
94103
"GET",
95104
"http://metadata.google.internal/computeMetadata/v1/?recursive=true",
96105
headers=headers,
97-
timeout=3.0,
106+
timeout=1.0,
98107
retries=False,
99108
).data.decode("utf-8")
100109
)
@@ -135,7 +144,7 @@ def azure_metadata():
135144
"GET",
136145
"http://169.254.169.254/metadata/instance/compute?api-version=2019-08-15",
137146
headers=headers,
138-
timeout=3.0,
147+
timeout=1.0,
139148
retries=False,
140149
).data.decode("utf-8")
141150
)

0 commit comments

Comments
 (0)