Skip to content

Commit

Permalink
Env Detection (#507)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Graeb <graebm@amazon.com>
Co-authored-by: Waqar Ahmed Khan <waahm7@gmail.com>
  • Loading branch information
3 people committed Oct 30, 2023
1 parent b83949d commit d62491f
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 5 deletions.
31 changes: 30 additions & 1 deletion awscrt/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from awscrt import NativeResource
from awscrt.http import HttpRequest
from awscrt.io import ClientBootstrap, TlsConnectionOptions
from awscrt.auth import AwsCredentialsProvider, AwsSignatureType, AwsSignedBodyHeaderType, AwsSignedBodyValue, AwsSigningAlgorithm, AwsSigningConfig
from awscrt.auth import AwsCredentialsProvider, AwsSignatureType, AwsSignedBodyHeaderType, AwsSignedBodyValue, \
AwsSigningAlgorithm, AwsSigningConfig
import awscrt.exceptions
from dataclasses import dataclass
import threading
Expand Down Expand Up @@ -178,6 +179,7 @@ def __init__(

def on_shutdown():
shutdown_event.set()

self._region = region
self.shutdown_event = shutdown_event

Expand Down Expand Up @@ -503,3 +505,30 @@ def create_default_s3_signing_config(*, region: str, credential_provider: AwsCre
use_double_uri_encode=False,
should_normalize_uri_path=False,
)


def get_ec2_instance_type():
"""
First this function will check it's running on EC2 via. attempting to read DMI info to avoid making IMDS calls.
If the function detects it's on EC2, and it was able to detect the instance type without a call to IMDS
it will return it.
Finally, it will call IMDS and return the instance type from there.
Note that in the case of the IMDS call, a new client stack is spun up using 1 background thread. The call is made
synchronously with a 1 second timeout: It's not cheap. To make this easier, the underlying result is cached
internally and will be freed when this module is unloaded is called.
Returns:
A string indicating the instance type or None if it could not be determined.
"""
return _awscrt.s3_get_ec2_instance_type()


def is_optimized_for_system():
"""
Returns:
true if the current build of this module has an optimized configuration
for the current system.
"""
return _awscrt.s3_is_crt_s3_optimized_for_system()
2 changes: 1 addition & 1 deletion crt/aws-c-auth
2 changes: 1 addition & 1 deletion crt/aws-lc
2 changes: 2 additions & 0 deletions source/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,8 @@ static PyMethodDef s_module_methods[] = {
AWS_PY_METHOD_DEF(s3_client_new, METH_VARARGS),
AWS_PY_METHOD_DEF(s3_client_make_meta_request, METH_VARARGS),
AWS_PY_METHOD_DEF(s3_meta_request_cancel, METH_VARARGS),
AWS_PY_METHOD_DEF(s3_get_ec2_instance_type, METH_NOARGS),
AWS_PY_METHOD_DEF(s3_is_crt_s3_optimized_for_system, METH_NOARGS),

/* WebSocket */
AWS_PY_METHOD_DEF(websocket_client_connect, METH_VARARGS),
Expand Down
3 changes: 3 additions & 0 deletions source/s3.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

#include "module.h"

PyObject *aws_py_s3_get_ec2_instance_type(PyObject *self, PyObject *args);
PyObject *aws_py_s3_is_crt_s3_optimized_for_system(PyObject *self, PyObject *args);

PyObject *aws_py_s3_client_new(PyObject *self, PyObject *args);
PyObject *aws_py_s3_client_make_meta_request(PyObject *self, PyObject *args);

Expand Down
27 changes: 27 additions & 0 deletions source/s3_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,33 @@

static const char *s_capsule_name_s3_client = "aws_s3_client";

PyObject *aws_py_s3_get_ec2_instance_type(PyObject *self, PyObject *args) {
(void)self;
(void)args;

const struct aws_s3_platform_info *platform_info = aws_s3_get_current_platform_info();

if (platform_info->instance_type.len) {
PyObject *ret_value = PyUnicode_FromAwsByteCursor(&platform_info->instance_type);
return ret_value;
}

Py_RETURN_NONE;
}

PyObject *aws_py_s3_is_crt_s3_optimized_for_system(PyObject *self, PyObject *args) {
(void)self;
(void)args;

const struct aws_s3_platform_info *platform_info = aws_s3_get_current_platform_info();

if (platform_info->has_recommended_configuration) {
Py_RETURN_TRUE;
}

Py_RETURN_FALSE;
}

struct s3_client_binding {
struct aws_s3_client *native;

Expand Down

0 comments on commit d62491f

Please sign in to comment.