Niko
07/29/2025, 9:30 PMMarvin
07/29/2025, 9:30 PMMarvin
07/29/2025, 9:31 PMpython
import boto3
import requests
def get_ecs_metadata():
# Get the task ID from ECS container metadata
# This endpoint is available in all ECS tasks
task_metadata_uri = "<http://169.254.170.2/v2/metadata>"
try:
response = requests.get(task_metadata_uri)
metadata = response.json()
# Extract task ID from ARN
task_id = metadata.get("TaskARN", "").split("/")[-1]
cluster_arn = metadata.get("Cluster", "")
cluster_name = cluster_arn.split("/")[-1]
return {
"task_id": task_id,
"cluster_name": cluster_name,
"full_metadata": metadata
}
except Exception as e:
return f"Failed to get ECS metadata: {str(e)}"
# Use within your flow
from prefect import flow
@flow
def my_flow():
ecs_info = get_ecs_metadata()
print(f"Running on ECS task {ecs_info['task_id']} in cluster {ecs_info['cluster_name']}")
# Your flow logic here
Key points:
1. This uses the ECS container metadata endpoint (169.254.170.2) which is automatically available within ECS tasks
2. The metadata endpoint provides comprehensive information about the task, including:
- Task ID
- Cluster ARN
- Container information
- Network configuration
- And more
Alternative approach using boto3 (if you have AWS credentials configured):
python
import boto3
import requests
def get_ecs_info_boto3():
# Get task ID from metadata service
task_metadata_uri = "<http://169.254.170.2/v2/metadata>"
response = requests.get(task_metadata_uri)
metadata = response.json()
task_arn = metadata["TaskARN"]
cluster_arn = metadata["Cluster"]
# Use boto3 to get more details if needed
ecs_client = boto3.client('ecs')
task_details = ecs_client.describe_tasks(
cluster=cluster_arn,
tasks=[task_arn]
)
return task_details
@flow
def my_flow():
ecs_info = get_ecs_info_boto3()
# Work with the detailed ECS information
Note that the metadata endpoint approach (first example) is more reliable as it doesn't require AWS credentials and will always work within an ECS task. The boto3 approach provides more detailed information but requires proper AWS IAM permissions.
Remember that this code will only work when running within an ECS task. If you run the flow locally or in a different environment, you'll need to handle those cases appropriately.