lablink_allocator_service.utils.aws_utils¶
lablink_allocator_service.utils.aws_utils
¶
Attributes¶
IMDS_BASE = 'http://169.254.169.254/latest'
module-attribute
¶
IMDS_TIMEOUT = 2.0
module-attribute
¶
logger = logging.getLogger(__name__)
module-attribute
¶
Classes¶
NotOnEC2Error
¶
Bases: RuntimeError
IMDSv2 unreachable (running outside EC2).
Functions:¶
check_support_nvidia(machine_type)
¶
Check if a given EC2 instance type supports NVIDIA GPUs. Args: machine_type (str): The EC2 instance type to check. Returns: bool: True if the instance type supports NVIDIA GPUs, False otherwise.
Source code in packages/allocator/src/lablink_allocator_service/utils/aws_utils.py
current_instance_security_group(region='us-west-2')
¶
Return the primary SG ID of the EC2 this process runs on.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region
|
str
|
AWS region. Caller should pass cfg.app.region — boto3 won't auto-detect inside the allocator container even though the instance metadata is reachable, because the container's env doesn't have AWS_REGION set. |
'us-west-2'
|
Raises NotOnEC2Error if IMDSv2 is unreachable.
Source code in packages/allocator/src/lablink_allocator_service/utils/aws_utils.py
get_instance_id_by_name(name, region='us-west-2')
¶
Look up an EC2 instance ID by its Name tag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The Name tag value of the EC2 instance. |
required |
region
|
str
|
The AWS region to search in. |
'us-west-2'
|
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The instance ID if found, None otherwise. |
Source code in packages/allocator/src/lablink_allocator_service/utils/aws_utils.py
get_instance_private_ip(instance_id, region='us-west-2')
¶
Get the private IP address of an EC2 instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance_id
|
str
|
The EC2 instance ID. |
required |
region
|
str
|
The AWS region where the instance is located. |
'us-west-2'
|
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The private IP address if available, None otherwise. |
Source code in packages/allocator/src/lablink_allocator_service/utils/aws_utils.py
get_instance_public_ip(instance_id, region='us-west-2')
¶
Get the public IP address of an EC2 instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance_id
|
str
|
The EC2 instance ID. |
required |
region
|
str
|
The AWS region where the instance is located. |
'us-west-2'
|
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The public IP address if available, None otherwise. |
Source code in packages/allocator/src/lablink_allocator_service/utils/aws_utils.py
stop_start_ec2_instance(instance_id, region='us-west-2')
¶
Stop and start an EC2 instance (last-resort fallback).
Calls stop_instances, waits for stopped state, then starts again. Note: This does NOT clear cloud-init state, so user_data.sh will not re-run. This is a best-effort restart for cases where SSH is unreachable (e.g., hung processes, OOM).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance_id
|
str
|
The EC2 instance ID. |
required |
region
|
str
|
The AWS region where the instance is located. |
'us-west-2'
|
Returns:
| Type | Description |
|---|---|
bool
|
True if stop/start completed successfully, False otherwise. |
Source code in packages/allocator/src/lablink_allocator_service/utils/aws_utils.py
upload_to_s3(local_path, env, bucket_name, region, deployment_name='lablink', kms_key_id=None)
¶
Uploads a file to an S3 bucket.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
local_path
|
Path
|
The local file path to upload. |
required |
env
|
str
|
The environment (e.g., dev, test, prod) for the upload. |
required |
bucket_name
|
str
|
The name of the S3 bucket to upload to. |
required |
region
|
str
|
The AWS region where the S3 bucket is located. |
required |
deployment_name
|
str
|
The deployment name for S3 key scoping. Defaults to "lablink". |
'lablink'
|
kms_key_id
|
Optional[str]
|
The KMS key ID for server-side encryption. Defaults to None. |
None
|