79271899

Date: 2024-12-11 13:35:11
Score: 0.5
Natty:
Report link

I want to move my production secrets to be in AWS secrets manager instead of environment variables.

import boto3
import os
import json
from botocore.exceptions import NoCredentialsError, PartialCredentialsError
from datetime import datetime

# Initialize the ECS client using credentials from environment variables
def initialize_client():
    try:
        ecs_client = boto3.client(
            'ecs',
            region_name=os.getenv('AWS_REGION', 'us-east-1')  # Default to 'us-east-1' if not set
        )
        return ecs_client
    except (NoCredentialsError, PartialCredentialsError):
        print("Error: AWS credentials are not properly set in environment variables.")
        exit(1)

# Function to list all ECS task definitions
def list_task_definitions(ecs_client):
    task_definitions = []
    paginator = ecs_client.get_paginator('list_task_definitions')
    page_iterator = paginator.paginate()

    for page in page_iterator:
        task_definitions.extend(page['taskDefinitionArns'])

    return task_definitions

# Function to describe a single task definition
def describe_task_definition(ecs_client, task_definition_arn):
    try:
        response = ecs_client.describe_task_definition(taskDefinition=task_definition_arn)
        return response['taskDefinition']
    except Exception as e:
        print(f"Error describing task definition {task_definition_arn}: {e}")
        return None

# Helper function to serialize datetime objects
def json_serializer(obj):
    if isinstance(obj, datetime):
        return obj.isoformat()
    raise TypeError(f"Type {type(obj)} not serializable")

if __name__ == "__main__":
    # Initialize ECS client
    ecs_client = initialize_client()

    # Fetch ECS task definitions
    task_defs = list_task_definitions(ecs_client)
    if task_defs:
        print("ECS Task Definitions (JSON):")
        for task_def_arn in task_defs:
            task_definition = describe_task_definition(ecs_client, task_def_arn)
            if task_definition:
                try:
                    # Pretty print the task definition JSON, handling datetime serialization
                    print(json.dumps(task_definition, indent=4, default=json_serializer))
                except Exception as e:
                    print(f"Error serializing JSON for task definition {task_def_arn}: {e}")
    else:
        print("No ECS task definitions found.")


## AKIA4VDBMA2N2WZPKOEL, QoFPDxtcij7sk4ylde5ZVq0U/c3pqXp68ynYTXJj
Reasons:
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Experimental Experiment